-
Notifications
You must be signed in to change notification settings - Fork 56
/
gen-methods.go
477 lines (427 loc) · 12.5 KB
/
gen-methods.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2017 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
// gen-methods generates accessor methods for structs with pointer fields.
//
// This code has been copied from https://github.com/google/go-github, and it's subject to its licence.
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"regexp"
"sort"
"strings"
"text/template"
"time"
)
const (
fileSuffix = ".gen.go"
)
var (
verbose = flag.Bool("v", false, "Print verbose log messages")
sourceTmpl = template.Must(template.New("source").Parse(source))
testTmpl = template.Must(template.New("test").Parse(test))
// skipStructMethods lists "struct.method" combos to skip.
skipStructMethods = map[string]bool{}
// skipStructs lists structs to skip in regex format.
skipStructs = []string{
"Management",
".*Manager",
}
)
func logf(fmt string, args ...interface{}) {
if *verbose {
log.Printf(fmt, args...)
}
}
func main() {
flag.Parse()
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, ".", sourceFilter, 0)
if err != nil {
log.Fatal(err)
return
}
for pkgName, pkg := range pkgs {
t := &templateData{
filename: pkgName + fileSuffix,
Year: time.Now().Year(),
Package: pkgName,
Imports: map[string]string{},
}
for filename, f := range pkg.Files {
logf("Processing %v...", filename)
if err := t.processAST(f); err != nil {
log.Fatal(err)
}
}
if err := t.dump(); err != nil {
log.Fatal(err)
}
}
logf("Done.")
}
func (t *templateData) processAST(f *ast.File) error {
for _, decl := range f.Decls {
gd, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
specLoop:
for _, spec := range gd.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
// Skip unexported identifiers.
if !ts.Name.IsExported() {
logf("Struct %v is unexported; skipping.", ts.Name)
continue
}
// Check if the struct should be skipped.
for _, pattern := range skipStructs {
match, err := regexp.Match(pattern, []byte(ts.Name.String()))
if err != nil {
return err
}
if match {
logf("Struct %v is skip list; skipping.", ts.Name)
continue specLoop
}
}
st, ok := ts.Type.(*ast.StructType)
if !ok {
continue
}
// Add stringer method
t.addStringer(ts.Name.String())
for _, field := range st.Fields.List {
if len(field.Names) == 0 {
continue
}
fieldName := field.Names[0]
// Skip unexported identifiers.
if !fieldName.IsExported() {
logf("Field %v is unexported; skipping.", fieldName)
continue
}
// Check if "struct.method" should be skipped.
if key := fmt.Sprintf("%v.Get%v", ts.Name, fieldName); skipStructMethods[key] {
logf("Method %v is skip list; skipping.", key)
continue
}
se, ok := field.Type.(*ast.StarExpr)
if !ok {
switch x := field.Type.(type) {
case *ast.MapType:
t.addMapType(x, ts.Name.String(), fieldName.String(), false)
continue
}
logf("Skipping field type %T, fieldName=%v", field.Type, fieldName)
continue
}
switch x := se.X.(type) {
case *ast.ArrayType:
t.addArrayType(x, ts.Name.String(), fieldName.String())
case *ast.Ident:
t.addIdent(x, ts.Name.String(), fieldName.String())
case *ast.MapType:
t.addMapType(x, ts.Name.String(), fieldName.String(), true)
case *ast.SelectorExpr:
t.addSelectorExpr(x, ts.Name.String(), fieldName.String())
default:
logf("processAST: type %q, field %q, unknown %T: %+v", ts.Name, fieldName, x, x)
}
}
}
}
return nil
}
func sourceFilter(fi os.FileInfo) bool {
return !strings.HasSuffix(fi.Name(), "_test.go") && !strings.HasSuffix(fi.Name(), fileSuffix)
}
func (t *templateData) dump() error {
if len(t.Getters) == 0 {
logf("No getters for %v; skipping.", t.filename)
return nil
}
// Sort getters by ReceiverType.FieldName.
sort.Sort(byName(t.Getters))
processTemplate := func(tmpl *template.Template, filename string) error {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, t); err != nil {
return err
}
clean, err := format.Source(buf.Bytes())
if err != nil {
return fmt.Errorf("format.Source:\n%v\n%v", buf.String(), err)
}
logf("Writing %v...", filename)
if err := os.Chmod(filename, 0644); err != nil {
return fmt.Errorf("os.Chmod(%q, 0644): %v", filename, err)
}
if err := ioutil.WriteFile(filename, clean, 0444); err != nil {
return err
}
if err := os.Chmod(filename, 0444); err != nil {
return fmt.Errorf("os.Chmod(%q, 0444): %v", filename, err)
}
return nil
}
if err := processTemplate(sourceTmpl, t.filename); err != nil {
return err
}
return processTemplate(testTmpl, strings.ReplaceAll(t.filename, ".go", "_test.go"))
}
func newGetter(receiverType, fieldName, fieldType, zeroValue string, namedStruct bool) *getter {
return &getter{
sortVal: strings.ToLower(receiverType) + "." + strings.ToLower(fieldName),
ReceiverVar: strings.ToLower(receiverType[:1]),
ReceiverType: receiverType,
FieldName: fieldName,
FieldType: fieldType,
ZeroValue: zeroValue,
NamedStruct: namedStruct,
}
}
func getMapKeyType(key ast.Expr) (valueType string, unknownType ast.Expr) {
switch key := key.(type) {
case *ast.Ident:
return key.String(), nil
default:
return "", key
}
}
func getMapValueType(value ast.Expr) (valueType string, unknownType ast.Expr) {
switch value := value.(type) {
case *ast.Ident:
return value.String(), nil
case *ast.InterfaceType:
return "interface{}", nil
default:
return "", value
}
}
func (t *templateData) addStringer(receiverType string) {
t.Getters = append(t.Getters, &getter{
sortVal: strings.ToLower(receiverType) + ".zzz",
ReceiverVar: strings.ToLower(receiverType[:1]),
ReceiverType: receiverType,
Stringer: true,
})
}
func (t *templateData) addArrayType(x *ast.ArrayType, receiverType, fieldName string) {
var eltType string
var zeroValue string
switch elt := x.Elt.(type) {
case *ast.Ident:
eltType = elt.String()
zeroValue = "nil"
case *ast.MapType:
keyType, unknownType := getMapKeyType(elt.Key)
if unknownType != nil {
logf("addArrayType MapType: type %q, field %q: unknown key type: %T %+v; skipping.", receiverType, fieldName, unknownType, unknownType)
return
}
valueType, unknownType := getMapValueType(elt.Value)
if unknownType != nil {
logf("addArrayType MapType: type %q, field %q: unknown value type: %T %+v; skipping.", receiverType, fieldName, unknownType, unknownType)
return
}
eltType = "map[" + keyType + "]" + valueType
zeroValue = "[]map[" + keyType + "]" + valueType + "{}"
default:
logf("addArrayType: type %q, field %q: unknown elt type: %T %+v; skipping.", receiverType, fieldName, elt, elt)
return
}
t.Getters = append(t.Getters, newGetter(receiverType, fieldName, "[]"+eltType, zeroValue, false))
}
func (t *templateData) addIdent(x *ast.Ident, receiverType, fieldName string) {
var zeroValue string
var namedStruct = false
switch x.String() {
case "int", "int64":
zeroValue = "0"
case "float32", "float64":
zeroValue = "0"
case "string":
zeroValue = `""`
case "bool":
zeroValue = "false"
case "Timestamp":
zeroValue = "Timestamp{}"
default:
zeroValue = "nil"
namedStruct = true
}
t.Getters = append(t.Getters, newGetter(receiverType, fieldName, x.String(), zeroValue, namedStruct))
}
func (t *templateData) addMapType(x *ast.MapType, receiverType, fieldName string, isAPointer bool) {
keyType, unknownType := getMapKeyType(x.Key)
if unknownType != nil {
logf("addMapType: type %q, field %q: unknown key type: %T %+v; skipping.", receiverType, fieldName, unknownType, unknownType)
return
}
valueType, unknownType := getMapValueType(x.Value)
if unknownType != nil {
logf("addMapType: type %q, field %q: unknown value type: %T %+v; skipping.", receiverType, fieldName, unknownType, unknownType)
return
}
fieldType := fmt.Sprintf("map[%v]%v", keyType, valueType)
zeroValue := fmt.Sprintf("map[%v]%v{}", keyType, valueType)
ng := newGetter(receiverType, fieldName, fieldType, zeroValue, false)
ng.MapType = !isAPointer
t.Getters = append(t.Getters, ng)
}
func (t *templateData) addSelectorExpr(x *ast.SelectorExpr, receiverType, fieldName string) {
if strings.ToLower(fieldName[:1]) == fieldName[:1] { // Non-exported field.
return
}
var xX string
if xx, ok := x.X.(*ast.Ident); ok {
xX = xx.String()
}
switch xX {
case "time", "json":
if xX == "json" {
t.Imports["encoding/json"] = "encoding/json"
} else {
t.Imports[xX] = xX
}
fieldType := fmt.Sprintf("%v.%v", xX, x.Sel.Name)
zeroValue := fmt.Sprintf("%v.%v{}", xX, x.Sel.Name)
if xX == "time" && x.Sel.Name == "Duration" {
zeroValue = "0"
}
t.Getters = append(t.Getters, newGetter(receiverType, fieldName, fieldType, zeroValue, false))
default:
logf("addSelectorExpr: xX %q, type %q, field %q: unknown x=%+v; skipping.", xX, receiverType, fieldName, x)
}
}
type templateData struct {
filename string
Year int
Package string
Imports map[string]string
Getters []*getter
}
type getter struct {
sortVal string // Lower-case version of "ReceiverType.FieldName".
ReceiverVar string // The one-letter variable name to match the ReceiverType.
ReceiverType string
FieldName string
FieldType string
ZeroValue string
NamedStruct bool // Getter for named struct.
MapType bool
Stringer bool // Used for the structs String method.
}
type byName []*getter
func (b byName) Len() int { return len(b) }
func (b byName) Less(i, j int) bool { return b[i].sortVal < b[j].sortVal }
func (b byName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
const source = `// Code generated by gen-methods; DO NOT EDIT.
// Please run "go generate ./..." instead.
package {{.Package}}
{{with .Imports}}
import (
{{- range . -}}
"{{.}}"
{{end -}}
)
{{end}}
{{range .Getters}}
{{if .NamedStruct}}
// Get{{.FieldName}} returns the {{.FieldName}} field.
func ({{.ReceiverVar}} *{{.ReceiverType}}) Get{{.FieldName}}() *{{.FieldType}} {
if {{.ReceiverVar}} == nil {
return {{.ZeroValue}}
}
return {{.ReceiverVar}}.{{.FieldName}}
}
{{else if .MapType}}
// Get{{.FieldName}} returns the {{.FieldName}} map if it's non-nil, an empty map otherwise.
func ({{.ReceiverVar}} *{{.ReceiverType}}) Get{{.FieldName}}() {{.FieldType}} {
if {{.ReceiverVar}} == nil || {{.ReceiverVar}}.{{.FieldName}} == nil {
return {{.ZeroValue}}
}
return {{.ReceiverVar}}.{{.FieldName}}
}
{{else if .Stringer}}
// String returns a string representation of {{.ReceiverType}}.
func ({{.ReceiverVar}} *{{.ReceiverType}}) String() string {
return Stringify({{.ReceiverVar}})
}
{{else}}
// Get{{.FieldName}} returns the {{.FieldName}} field if it's non-nil, zero value otherwise.
func ({{.ReceiverVar}} *{{.ReceiverType}}) Get{{.FieldName}}() {{.FieldType}} {
if {{.ReceiverVar}} == nil || {{.ReceiverVar}}.{{.FieldName}} == nil {
return {{.ZeroValue}}
}
return *{{.ReceiverVar}}.{{.FieldName}}
}
{{end}}
{{end}}
`
const test = `// Code generated by gen-methods; DO NOT EDIT.
// Please run "go generate ./..." instead.
package {{.Package}}
{{with .Imports}}
import (
"encoding/json"
"testing"
{{range . -}}
"{{.}}"
{{end -}}
)
{{end}}
{{range .Getters}}
{{if .NamedStruct}}
func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
{{.ReceiverVar}} := &{{.ReceiverType}}{}
{{.ReceiverVar}}.Get{{.FieldName}}()
{{.ReceiverVar}} = nil
{{.ReceiverVar}}.Get{{.FieldName}}()
}
{{else if .MapType}}
func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
zeroValue := {{.FieldType}}{}
{{.ReceiverVar}} := &{{.ReceiverType}}{ {{.FieldName}}: zeroValue }
{{.ReceiverVar}}.Get{{.FieldName}}()
{{.ReceiverVar}} = &{{.ReceiverType}}{}
{{.ReceiverVar}}.Get{{.FieldName}}()
{{.ReceiverVar}} = nil
{{.ReceiverVar}}.Get{{.FieldName}}()
}
{{else if .Stringer}}
func Test{{ .ReceiverType }}_String(t *testing.T) {
var rawJSON json.RawMessage
v := &{{ .ReceiverType }}{}
if err := json.Unmarshal([]byte(v.String()), &rawJSON); err != nil {
t.Errorf("failed to produce a valid json")
}
}
{{else}}
func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
var zeroValue {{.FieldType}}
{{.ReceiverVar}} := &{{.ReceiverType}}{ {{.FieldName}}: &zeroValue }
{{.ReceiverVar}}.Get{{.FieldName}}()
{{.ReceiverVar}} = &{{.ReceiverType}}{}
{{.ReceiverVar}}.Get{{.FieldName}}()
{{.ReceiverVar}} = nil
{{.ReceiverVar}}.Get{{.FieldName}}()
}
{{end}}
{{end}}
`