-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode.go
235 lines (211 loc) · 7.3 KB
/
code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package gopoet
import (
"bytes"
"fmt"
"go/types"
"reflect"
"text/template"
)
// CodeBlock represents one or more Go statements or expressions, such as for
// initializing a value (e.g const or var) or for the body of a function or
// method.
//
// Code block contents can be constructed from fmt-like methods on the CodeBlock
// or by rendering a template via the RenderCode method. The format arguments
// supplied to Printf or Printlnf methods can be references to other Go Poet
// types (Package, Symbol, MethodReference, TypeName) including file elements
// (*ConstSpec, *VarSpec, *TypeSpec, *FuncSpec, etc). Such references will be
// properly qualified when the CodeBlock is rendered into generated Go code, so
// that references use the correct package names and aliases.
type CodeBlock struct {
elements []codeElement
}
// codeElement represents code to render. It will either be a message and format
// arguments, rendered using the "fmt" package, or a template and value,
// rendered using the "text/template" package.
type codeElement interface {
isCodeElement()
writeTo(b *bytes.Buffer)
qualify(imports *Imports)
}
// Print adds the given text to the code block. It returns the code block, for
// method chaining.
func (cb *CodeBlock) Print(text string) *CodeBlock {
return cb.Printf("%s", text)
}
// Println adds the given text to the code block, along with a trailing newline.
// It returns the code block, for method chaining.
func (cb *CodeBlock) Println(text string) *CodeBlock {
return cb.Printlnf("%s", text)
}
// Printf adds the given text to the code block and will properly qualify any
// supported types in the argument list when rendered. It returns the code
// block, for method chaining.
func (cb *CodeBlock) Printf(fmt string, args ...interface{}) *CodeBlock {
cb.elements = append(cb.elements, line{format: fmt, args: args})
return cb
}
// Printlnf adds the given text to the code block and will properly qualify any
// supported types in the argument list when rendered. Similar to Println, it
// adds a trailing newline. It returns the code block, for method chaining.
func (cb *CodeBlock) Printlnf(fmt string, args ...interface{}) *CodeBlock {
return cb.Printf(fmt+"\n", args...)
}
// RenderCode uses the given template and data object to render contents. The
// given data object will be re-created during rendering, if necessary, so that
// any supported types therein are updated to use properly qualified references.
// For example, a struct that includes a *TypeSpec will be re-created so that
// the *TypeSpec field is modified to include a package name that matches the
// proper import name/alias for the render context.
//
// This method returns the code block, for method chaining.
func (cb *CodeBlock) RenderCode(template *template.Template, data interface{}) *CodeBlock {
cb.elements = append(cb.elements, &templ{templ: template, data: data})
return cb
}
// AddCode adds the contents of the given CodeBlock to this one. It returns the
// code block, for method chaining.
func (cb *CodeBlock) AddCode(code *CodeBlock) *CodeBlock {
cb.elements = append(cb.elements, code.elements...)
return cb
}
// Print is a convenience function that allocates a new CodeBlock and calls its
// Print method in one step.
func Print(text string) *CodeBlock {
return (&CodeBlock{}).Print(text)
}
// Println is a convenience function that allocates a new CodeBlock and calls
// its Println method in one step.
func Println(text string) *CodeBlock {
return (&CodeBlock{}).Println(text)
}
// Printf is a convenience function that allocates a new CodeBlock and calls its
// Printf method in one step.
func Printf(fmt string, args ...interface{}) *CodeBlock {
return (&CodeBlock{}).Printf(fmt, args...)
}
// Printlnf is a convenience function that allocates a new CodeBlock and calls
// its Printlnf method in one step.
func Printlnf(fmt string, args ...interface{}) *CodeBlock {
return (&CodeBlock{}).Printlnf(fmt, args...)
}
// RenderCode is a convenience function that allocates a new CodeBlock and calls
// its RenderCode method in one step.
func RenderCode(template *template.Template, data interface{}) *CodeBlock {
return (&CodeBlock{}).RenderCode(template, data)
}
func (cb *CodeBlock) qualify(imports *Imports) {
for _, e := range cb.elements {
e.qualify(imports)
}
}
func (cb *CodeBlock) writeTo(b *bytes.Buffer) {
for _, e := range cb.elements {
e.writeTo(b)
}
}
// line represents a message and format arguments, which will be rendered to a
// code block using the "fmt" package.
type line struct {
format string
args []interface{}
}
func (l line) isCodeElement() {}
func (l line) writeTo(b *bytes.Buffer) {
fmt.Fprintf(b, l.format, l.args...)
}
func (l line) qualify(imports *Imports) {
for i := range l.args {
switch a := l.args[i].(type) {
case Package:
p := imports.registerPackage(a)
if p != a.Name {
l.args[i] = Package{Name: p, ImportPath: a.ImportPath}
}
case *Package:
p := imports.registerPackage(*a)
if p != a.Name {
l.args[i] = Package{Name: p, ImportPath: a.ImportPath}
}
case Symbol:
l.args[i] = imports.EnsureImported(a)
case *Symbol:
l.args[i] = imports.EnsureImported(*a)
case MethodReference:
sym := imports.EnsureImported(a.Type)
if sym != a.Type {
l.args[i] = &MethodReference{Type: sym, Method: a.Method}
}
case *MethodReference:
sym := imports.EnsureImported(a.Type)
if sym != a.Type {
l.args[i] = &MethodReference{Type: sym, Method: a.Method}
}
case Signature:
l.args[i] = imports.EnsureAllTypesImported(&a)
case *Signature:
l.args[i] = imports.EnsureAllTypesImported(a)
case TypeName:
l.args[i] = imports.EnsureTypeImported(a)
case *ConstSpec:
l.args[i] = imports.EnsureImported(a.ToSymbol())
case *VarSpec:
l.args[i] = imports.EnsureImported(a.ToSymbol())
case *TypeSpec:
l.args[i] = imports.EnsureImported(a.ToSymbol())
case *InterfaceMethod:
mr := a.ToMethodRef()
sym := imports.EnsureImported(mr.Type)
if sym != mr.Type {
l.args[i] = &MethodReference{Type: sym, Method: mr.Method}
}
case *FuncSpec:
symOrMr := a.ToSymbol()
switch sm := symOrMr.(type) {
case Symbol:
l.args[i] = imports.EnsureImported(sm)
case MethodReference:
sym := imports.EnsureImported(sm.Type)
if sym != sm.Type {
l.args[i] = &MethodReference{Type: sym, Method: sm.Method}
}
}
case reflect.Type:
t := TypeNameForReflectType(a)
l.args[i] = imports.EnsureTypeImported(t)
case types.Type:
t := TypeNameForGoType(a)
l.args[i] = imports.EnsureTypeImported(t)
case types.Object:
symOrMr := SymbolForGoObject(a)
switch sm := symOrMr.(type) {
case Symbol:
l.args[i] = imports.EnsureImported(sm)
case MethodReference:
sym := imports.EnsureImported(sm.Type)
if sym != sm.Type {
l.args[i] = &MethodReference{Type: sym, Method: sm.Method}
}
}
case *types.Package:
p := PackageForGoType(a)
l.args[i] = imports.registerPackage(p)
}
}
}
// templ represents a template and data value, which will be rendered to a code
// block using the "text/template" package.
type templ struct {
templ *template.Template
data interface{}
}
func (t *templ) isCodeElement() {}
func (t *templ) writeTo(b *bytes.Buffer) {
err := t.templ.Execute(b, t.data)
if err != nil {
panic(err.Error())
}
}
func (t *templ) qualify(imports *Imports) {
t.data = imports.QualifyTemplateData(t.data)
}