-
Notifications
You must be signed in to change notification settings - Fork 0
/
structtagger.go
383 lines (346 loc) · 10.4 KB
/
structtagger.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
// Copyright 2022 Mathew Crenshaw. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
// StructTagger is a tool to automate the creation of constants that match the tag value
// of a struct. Given the name of a struct type T and that has tag Y defined for at least
// one of its fields Z, structtagger will create a new self-contained Go source file implementing
// const TZY = "Yvalue"
// The file is created in the same package and directory as the package that defines T, unless
// the target is an import path, in which the package of the current directory will be used.
// It has helpful defaults designed for use with go generate.
//
// For example, given this snippet,
//
// package autos
//
// type Car struct {
// Make string `json:"make"`
// Model string `json:"model"`
// }
//
// running this command
//
// structtagger -type=Car -tag=json
//
// in the same directory will create the file car_structtags.go, in package autos,
// containing
//
// const (
// CarMakeJson = "make"
// CarModelJson = "model"
// )
//
// Typically this process would be run using go generate, like this:
//
// //go:generate structtagger -type=Car -tag=json
//
// With no arguments, it processes the package in the current directory for the
// tag "json". Otherwise, the arguments must name a single directory holding a Go
// package, the import path of a Go package, or a set of Go source files that
// represent a single Go package.
//
// The -type flag accepts a comma-separated list of types so a single run can
// generate methods for multiple types. The default output file is t_structtags.go,
// where t is the lower-cased name of the first type listed. It can be overridden
// with the -output flag.
//
// The -tag flag accepts a comma-separated list of struct tags so a single run
// can generate constants for multiple struct tags. The default tag is json.
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"go/token"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"golang.org/x/tools/go/packages"
)
var (
typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
tagNames = flag.String("tag", "", "comma-separated list of struct tags")
output = flag.String("output", "", "output file name; default srcdir/<type>_structtags.go")
)
// Usage is a replacement usage function for the flags package.
func Usage() {
fmt.Fprintf(os.Stderr, "Usage of structtagger:\n")
fmt.Fprintf(os.Stderr, "\tstructtagger [flags] -type T [import path]\n")
fmt.Fprintf(os.Stderr, "\tstructtagger [flags] -type T [directory]\n")
fmt.Fprintf(os.Stderr, "\tstructtagger [flags] -type T files... # Must be a single package\n")
fmt.Fprintf(os.Stderr, "For more information, see:\n")
fmt.Fprintf(os.Stderr, "\thttps://pkg.go.dev/github.com/sgtsquiggs/tools/cmd/structtagger\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
log.SetFlags(0)
log.SetPrefix("structtagger: ")
flag.Usage = Usage
flag.Parse()
if len(*typeNames) == 0 {
flag.Usage()
os.Exit(2)
}
types := strings.Split(*typeNames, ",")
tags := []string{"json"}
if len(*tagNames) > 0 {
tags = strings.Split(*tagNames, ",")
}
// Check tags
for _, tag := range tags {
if !validateTag(tag) {
log.Fatal("-tag option only supports json, bson, yaml, toml, protobuf")
}
}
// We accept either one directory, one package, or a list of files. Which do we have?
args := flag.Args()
if len(args) == 0 {
// Default: process whole package in current directory.
args = []string{"."}
}
// Parse the package once.
var pkg string
var dir string
g := Generator{}
if len(args) == 1 && isPackage(args[0]) {
pkg = args[0]
dir = "."
} else if len(args) == 1 && isDirectory(args[0]) {
dir = args[0]
} else {
dir = filepath.Dir(args[0])
}
g.parsePackage(args)
var pkgName string
if pkg != "" {
pkgs, err := packages.Load(&packages.Config{}, ".")
if err != nil {
log.Fatal(err)
}
if len(pkgs) != 1 {
log.Fatalf("error: %d packages found", len(pkgs))
}
pkgName = pkgs[0].Name
} else {
pkgName = g.pkg.name
}
// Print the header and package clause.
g.Printf("// Code generated by \"structtagger %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " "))
g.Printf("\n")
g.Printf("package %s", pkgName)
sort.Strings(types)
// Run generate for each type.
for _, typeName := range types {
g.generate(typeName, tags)
}
// Format the output.
src := g.format()
// Write to file.
outputName := *output
if outputName == "" {
baseName := fmt.Sprintf("%s_structtags.go", types[0])
outputName = filepath.Join(dir, strings.ToLower(baseName))
}
err := ioutil.WriteFile(outputName, src, 0644)
if err != nil {
log.Fatalf("writing output: %s", err)
}
}
// isDirectory reports whether the named file is a directory.
func isDirectory(name string) bool {
info, err := os.Stat(name)
if err != nil {
log.Fatal(err)
}
return info.IsDir()
}
// isPackage reports whether the named package is valid.
func isPackage(name string) bool {
p, loadErr := packages.Load(&packages.Config{Dir: "."}, name)
if loadErr == nil && len(p) > 0 {
return true
}
return false
}
// Generator holds the state of the analysis. Primarily used to buffer
// the output for format.Source.
type Generator struct {
buf bytes.Buffer // Accumulated output.
pkg *Package // Package we are scanning.
}
func (g *Generator) Printf(format string, args ...interface{}) {
fmt.Fprintf(&g.buf, format, args...)
}
// File holds a single parsed file and associated data.
type File struct {
pkg *Package // Package to which this file belongs.
file *ast.File // Parsed AST.
// These fields are reset for each type being generated.
typeName string // Name of the struct type.
values []Value // Accumulator for struct tag values of that type.
}
type Package struct {
name string
files []*File
}
// parsePackage analyzes the single package constructed from the patterns.
// parsePackage exits if there is an error.
func (g *Generator) parsePackage(patterns []string) {
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax,
Tests: false,
}
pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
log.Fatal(err)
}
if len(pkgs) != 1 {
log.Fatalf("error: %d packages found", len(pkgs))
}
g.addPackage(pkgs[0])
}
// addPackage adds a type checked Package and its syntax files to the generator.
func (g *Generator) addPackage(pkg *packages.Package) {
g.pkg = &Package{
name: pkg.Name,
files: make([]*File, len(pkg.Syntax)),
}
for i, file := range pkg.Syntax {
g.pkg.files[i] = &File{
file: file,
pkg: g.pkg,
}
}
}
// generate produces the String method for the named type.
func (g *Generator) generate(typeName string, tags []string) {
values := make([]Value, 0, 100)
for _, file := range g.pkg.files {
// Set the state for this run of the walker.
file.typeName = typeName
file.values = nil
if file.file != nil {
ast.Inspect(file.file, file.genDecl(tags))
values = append(values, file.values...)
}
}
sort.Slice(values, func(i, j int) bool {
return values[i].name < values[j].name
})
if len(values) == 0 {
log.Fatalf("no values defined for type %s", typeName)
}
g.Printf("\n")
g.Printf("const (\n")
for _, value := range values {
g.Printf("\t%s%s%s = \"%s\"\n", strings.Title(typeName), strings.Title(value.name), strings.Title(value.tag), value.value)
}
g.Printf(")\n")
}
// format returns the gofmt-ed contents of the Generator's buffer.
func (g *Generator) format() []byte {
src, err := format.Source(g.buf.Bytes())
if err != nil {
// Should never happen, but can arise when developing this code.
// The user can compile the output to see the error.
log.Printf("warning: internal error: invalid Go generated: %s", err)
log.Printf("warning: compile the package to analyze the error")
return g.buf.Bytes()
}
return src
}
// Value represents a declared constant.
type Value struct {
name string // The name of the field.
tag string // The name of the tag.
value string // The tag value.
}
// genDecl processes one declaration clause.
func (f *File) genDecl(tags []string) func(ast.Node) bool {
return func(node ast.Node) bool {
decl, ok := node.(*ast.GenDecl)
if !ok || decl.Tok != token.TYPE {
// We only care about type declarations.
return true
}
// Loop over the elements of the declaration. Each element is a ValueSpec:
// a list of names possibly followed by a type, possibly followed by values.
// If the type and value are both missing, we carry down the type (and value,
// but the "go/types" package takes care of that).
for _, spec := range decl.Specs {
vspec := spec.(*ast.TypeSpec) // Guaranteed to succeed as this is TYPE.
if vspec.Name.Name != f.typeName {
// This is not the type we're looking for.
continue
}
structType, ok := vspec.Type.(*ast.StructType)
if !ok {
continue
}
for _, field := range structType.Fields.List {
if field.Tag == nil {
continue
}
for _, name := range field.Names {
// ignore placeholder and protobuf private fields
if name.Name == "_" || strings.HasPrefix(name.Name, "XXX_") {
continue
}
tag := reflect.StructTag(field.Tag.Value)
for _, tagName := range tags {
tagValue, ok := tagValueGetter(tag, tagName)
if !ok || tagValue == "" {
tagValue = name.Name
}
if tagValue == "-" {
continue
}
v := Value{
name: name.Name,
tag: tagName,
value: tagValue,
}
f.values = append(f.values, v)
}
}
}
}
return false
}
}
// Helpers
func validateTag(name string) bool {
switch name {
case "json", "bson", "yaml", "toml", "protobuf":
return true
default:
return false
}
}
// tagValueGetter returns the value of known struct tags.
func tagValueGetter(tag reflect.StructTag, name string) (string, bool) {
if !validateTag(name) {
return "", false
}
value, ok := tag.Lookup(name)
if !ok {
return "", false
}
switch name {
case "json", "bson", "yaml", "toml": // ex: "field_name,omitempty"
return strings.SplitN(value, ",", 2)[0], true
case "protobuf": // ex: "bytes,1,opt,name=content,proto3"
name := strings.SplitN(value, ",", 5)[3]
return strings.SplitN(name, "=", 2)[1], true
default:
log.Fatalf("internal error: unsupported tag %s", name)
panic("unreachable")
}
}