-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.go
294 lines (267 loc) · 6.89 KB
/
template.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package codegen
import (
"bytes"
"go/token"
"go/types"
"path/filepath"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/jinzhu/inflection"
"github.com/pkg/errors"
)
func RunTemplate(
template *template.Template,
aStruct *types.Named,
args map[string]string,
info TypeInfo,
) (string, error) {
c := &TemplateContext{
Args: args,
StructName: aStruct.Obj().Name(),
TemplateName: template.Name(),
PackageName: aStruct.Obj().Pkg().Name(),
PackagePath: aStruct.Obj().Pkg().Path(),
Struct: aStruct,
info: info,
}
var result bytes.Buffer
if err := template.Execute(&result, c); err != nil {
return "", err
}
return result.String(), nil
}
type TypeInfo interface {
AddImport(pkg string)
GetType(fullName string) (types.Type, error)
}
type TemplateContext struct {
Args map[string]string
StructName string
TemplateName string
PackageName string
PackagePath string
Struct *types.Named
info TypeInfo
}
// For a function to be callable from a template, it must return something.
func (c *TemplateContext) AddImport(name string) string {
c.info.AddImport(name)
return ""
}
func (c *TemplateContext) AddImportType(t types.Type) (string, error) {
switch t := t.(type) {
case *types.Named:
pkg := t.Obj().Pkg()
if pkg != nil {
c.AddImport(pkg.Path())
}
case *types.Map:
if _, err := c.AddImportType(t.Key()); err != nil {
return "", errors.Wrapf(err, "importing map key type '%s' of type %T", t, t)
}
if _, err := c.AddImportType(t.Elem()); err != nil {
return "", errors.Wrapf(err, "importing element type '%s' of type %T", t, t)
}
case *types.Interface:
// A bare `interface{}` we don't need to import, but for an interface
// literal, we need to import its embedded interfaces and function
// parameters and return types.
for i := 0; i < t.NumEmbeddeds(); i++ {
e := t.EmbeddedType(i)
if _, err := c.AddImportType(e); err != nil {
return "", errors.Wrapf(
err,
"importing embedded type '%s' of type %T from interface '%s'",
e, e, t,
)
}
}
for i := 0; i < t.NumExplicitMethods(); i++ {
m := t.ExplicitMethod(i)
mt := m.Type().(*types.Signature)
if _, err := c.AddImportType(mt); err != nil {
return "", errors.Wrapf(
err,
"importing method of type %T from interface method '%s'",
mt, m.Name(),
)
}
}
case *types.Struct:
// A named struct type will be handled above by `types.Named`, for a struct
// type literal, we need to import its field types.
for i := 0; i < t.NumFields(); i++ {
f := t.Field(i)
if _, err := c.AddImportType(f.Type()); err != nil {
return "", errors.Wrapf(
err,
"importing struct field %s, '%s' of type %T",
f.Name(), f.Type(), f.Type(),
)
}
}
case *types.Tuple:
// Used for function parameters and results
for i := 0; i < t.Len(); i++ {
if _, err := c.AddImportType(t.At(i).Type()); err != nil {
return "", errors.Wrapf(
err,
"importing tuple index %d, '%s' of type %T",
i, t, t,
)
}
}
case *types.Signature:
p := t.Params()
if _, err := c.AddImportType(p); err != nil {
return "", errors.Wrapf(
err,
"importing params '%s' of type %T from function '%s'",
p, p, t.String(),
)
}
r := t.Results()
if _, err := c.AddImportType(r); err != nil {
return "", errors.Wrapf(
err,
"importing results '%s' of type %T from function '%s'",
r, r, t.String(),
)
}
case interface{ Elem() types.Type }: // Array, Slice, Pointer, Channel
return c.AddImportType(t.Elem())
case *types.Basic:
// No need to import
default:
return "", errors.Errorf("couldn't add import for '%s' of type %T", t, t)
}
return "", nil
}
func (c *TemplateContext) Arg(name string) string {
return c.Args[name]
}
func (c *TemplateContext) HasArg(name string) bool {
_, has := c.Args[name]
return has
}
func (c *TemplateContext) RequireArg(name string) (string, error) {
arg, has := c.Args[name]
if !has {
return "", errors.Errorf("required arg %s not found", name)
}
return arg, nil
}
func (c *TemplateContext) DefaultArg(name, defaultVal string) string {
arg, has := c.Args[name]
if !has {
return defaultVal
}
return arg
}
func (c *TemplateContext) Implements(aType types.Type, interfaceName string) (bool, error) {
t, err := c.info.GetType(interfaceName)
if err != nil {
return false, err
}
i, ok := t.(*types.Interface)
if !ok {
return false, errors.Errorf("%s is not an interface", interfaceName)
}
return types.Implements(aType, i), nil
}
func (c *TemplateContext) TypeString(t types.Type) string {
return types.TypeString(t, func(p *types.Package) string {
if p == nil {
return ""
} else if p.Path() == c.PackagePath {
return ""
} else {
return p.Name()
}
})
}
func ParseTemplate(path string) (*template.Template, error) {
name := filepath.Base(path)
return template.New(name).Funcs(templateFunctions).ParseFiles(path)
}
var templateFunctions template.FuncMap
func init() {
templateFunctions = sprig.TxtFuncMap()
templateFunctions["singular"] = inflection.Singular
templateFunctions["plural"] = inflection.Plural
templateFunctions["catNoSpace"] = catNoSpace
templateFunctions["typeName"] = typeName
templateFunctions["isExported"] = isExported
templateFunctions["pointerType"] = pointerType
templateFunctions["structFields"] = structFields
templateFunctions["structField"] = structField
}
func catNoSpace(ss ...string) string {
return strings.Join(ss, "")
}
func typeName(t types.Type) string {
switch t := t.(type) {
case *types.Named:
pkg := t.Obj().Pkg()
if pkg != nil {
return t.Obj().Pkg().Name() + "." + t.Obj().Name()
} else {
return t.Obj().Name()
}
case interface{ Elem() types.Type }:
return typeName(t.Elem())
default:
return t.String()
}
}
func isExported(name string) bool {
return token.IsExported(name)
}
func structFields(t types.Type) []*types.Var {
s := structFromType(t)
if s == nil {
return nil
}
var fields []*types.Var
for i := 0; i < s.NumFields(); i++ {
fields = append(fields, s.Field(i))
}
return fields
}
func structField(t types.Type, fieldName string) *types.Var {
s := structFromType(t)
if s == nil {
return nil
}
for i := 0; i < s.NumFields(); i++ {
f := s.Field(i)
if f.Name() == fieldName {
return f
}
}
// If we can't find it on the struct directly try checking nested structs with
// a lookup (will only be able to find exported fields in nested structs).
obj, _, _ := types.LookupFieldOrMethod(s, true, nil, fieldName)
switch obj := obj.(type) {
case *types.Var:
return obj
default:
return nil
}
}
func pointerType(t types.Type) *types.Pointer {
return types.NewPointer(t)
}
func structFromType(t types.Type) *types.Struct {
switch t := t.(type) {
case *types.Struct:
return t
case *types.Named:
return structFromType(t.Underlying())
case interface{ Elem() types.Type }:
return structFromType(t.Elem())
default:
return nil
}
}