-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (140 loc) · 3.04 KB
/
main.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
package main
import (
"bytes"
"fmt"
"github.com/fatih/structtag"
"github.com/spf13/pflag"
"github.com/the4thamigo-uk/conflate"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"os"
)
type structTags map[string]fieldTags
type fieldTags map[string]string
func main() {
err := run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run() error {
var dir string
pflag.StringVarP(&dir, "dir", "d", ".", `directory containing go code where you want the tags to be applied`)
var files []string
pflag.StringArrayVarP(&files, "tags", "t", nil, `list of filenames (JSON, YAML or TOML) containing tags to add to struct fields`)
pflag.Parse()
if len(files) == 0 {
return fmt.Errorf("no tags files specified")
}
newTags, err := loadConfig(files...)
if err != nil {
return err
}
err = appendTags(dir, *newTags)
if err != nil {
return err
}
return nil
}
func loadConfig(files ...string) (*structTags, error) {
c, err := conflate.FromFiles(files...)
if err != nil {
return nil, err
}
var newTags structTags
err = c.Unmarshal(&newTags)
if err != nil {
return nil, err
}
return &newTags, nil
}
func appendTags(dir string, newTags structTags) error {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, dir, nil, parser.ParseComments)
if err != nil {
return err
}
for _, pkg := range pkgs {
for fileName, file := range pkg.Files {
structs := findStructs(file)
var modified bool
for strName, str := range structs {
strTags, ok := newTags[strName]
if !ok {
continue
}
for _, fld := range str.Fields.List {
fldTag, ok := strTags[fld.Names[0].Name]
if !ok {
continue
}
if fld.Tag == nil {
fld.Tag = &ast.BasicLit{}
}
newTag, err := addTag(unquoteTag(fld.Tag.Value), unquoteTag(fldTag))
if err != nil {
return err
}
fld.Tag.Value = quoteTag(newTag)
modified = true
}
}
if modified {
var buf bytes.Buffer
err = format.Node(&buf, fset, file)
if err != nil {
return err
}
err = ioutil.WriteFile(fileName, buf.Bytes(), 0)
if err != nil {
return err
}
}
}
}
return nil
}
func unquoteTag(tag string) string {
if len(tag) > 2 && tag[0] == '`' && tag[len(tag)-1] == '`' {
return tag[1 : len(tag)-1]
}
return tag
}
func quoteTag(tag string) string {
if len(tag) > 2 && tag[0] == '`' && tag[len(tag)-1] == '`' {
return tag
}
return "`" + tag + "`"
}
func addTag(tag string, newTag string) (string, error) {
tags, err := structtag.Parse(tag)
if err != nil {
return "", err
}
newTags, err := structtag.Parse(newTag)
if err != nil {
return "", err
}
for _, t := range newTags.Tags() {
tags.Set(t)
}
return tags.String(), nil
}
func findStructs(node ast.Node) map[string]*ast.StructType {
structs := make(map[string]*ast.StructType, 0)
findStructs := func(n ast.Node) bool {
switch t := n.(type) {
case *ast.TypeSpec:
if s, ok := t.Type.(*ast.StructType); ok {
structs[t.Name.Name] = s
}
}
return true
}
ast.Inspect(node, findStructs)
return structs
}