-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
213 lines (189 loc) · 4.83 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
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
package main
import (
"bytes"
"errors"
"fmt"
"go/ast"
"go/token"
"go/types"
"html/template"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/analysis/singlechecker"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/imports"
)
func main() {
singlechecker.Main(analyzer)
}
var analyzer = &analysis.Analyzer{
Name: "encjsongen",
Doc: `Generate MarshalJSON() and UnmarshalJSON() from customjson tag.
Tag format => customjson:"NAME=EXPR;ASSIGN"
- NAME: Used in place of json tag
- EXPR: Expression to represent alias type(for MarshalJSON)
- ASSIGN: Expression to assign to the actual type(for UnmarshalJSON)
Note: "$" in EXPR and ASSIGN is a special character that is converted to
the field name with receiver on the right hand side.
// Example:
type v struct {
CreateTime time.Time ` + "`" + `json:"-" customjson:"createTime=$.Unix();time.Unix($, 0)"` + "`" + `
}
`,
Requires: []*analysis.Analyzer{inspect.Analyzer},
RunDespiteErrors: true,
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.TypeSpec)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
ts := n.(*ast.TypeSpec)
s, ok := ts.Type.(*ast.StructType)
if !ok {
return
}
si := newStructInfo(pass.Fset, pass.Pkg, ts)
for _, f := range s.Fields.List {
if f.Tag == nil {
continue
}
customjson := reflect.StructTag(f.Tag.Value).Get("customjson")
if customjson == "" {
continue
}
if err := si.AddAlias(f.Names[0].Name, customjson); err != nil {
pass.Reportf(f.Pos(), "%v", err)
return
}
}
if si.HasAlias() {
if err := si.Output(); err != nil {
pass.Reportf(ts.Pos(), "failed to generate: %v", err)
}
}
})
return nil, nil
}
type alias struct {
Target string
JSONKey string
Type string
Expr string
Assign string
}
func newStructInfo(fset *token.FileSet, pkg *types.Package, ts *ast.TypeSpec) *structInfo {
return &structInfo{
fset: fset,
pkg: pkg,
path: filepath.Dir(fset.File(ts.Pos()).Name()),
Receiver: ts.Name.Name,
}
}
type structInfo struct {
fset *token.FileSet
pkg *types.Package
path string
Receiver string
Aliases []alias
}
func (si *structInfo) AddAlias(name, tag string) error {
i := strings.Index(tag, "=")
if i < 1 {
return errors.New("invalid tag")
}
exprs := strings.Split(tag[i+1:], ";")
if len(exprs) != 2 {
return errors.New("invalid tag")
}
typ, err := types.Eval(si.fset, si.pkg, 0, strings.Replace(exprs[0], "$", si.Receiver+"{}."+name, -1))
if err != nil {
return err
}
if typ.Type == nil {
return errors.New("invalid expr")
}
si.Aliases = append(si.Aliases, alias{
Target: name,
JSONKey: tag[:i],
Type: typ.Type.String(),
Expr: strings.Replace(exprs[0], "$", "v."+name, -1),
Assign: strings.Replace(exprs[1], "$", "aux.Alias"+name, -1),
})
return nil
}
func (si *structInfo) HasAlias() bool {
return len(si.Aliases) > 0
}
func (si *structInfo) Output() error {
b := new(bytes.Buffer)
fmt.Fprintf(b, "// Code generated by encjsongen. DO NOT EDIT.\n\n")
fmt.Fprintf(b, "package %s\n\n", si.pkg.Name())
if err := template.Must(template.New("marshal").Parse(tmplMarshalJSON)).Execute(b, si); err != nil {
return err
}
fmt.Fprintf(b, "\n")
if err := template.Must(template.New("unmarshal").Parse(tmplUnmarshalJSON)).Execute(b, si); err != nil {
return err
}
filename := filepath.Join(si.path, strings.ToLower(si.Receiver)+"_json.go")
src, err := imports.Process(filename, b.Bytes(), nil)
if err != nil {
return err
}
return ioutil.WriteFile(filename, src, 0644)
}
func (si *structInfo) Exprs() []string {
exprs := make([]string, len(si.Aliases))
for i, a := range si.Aliases {
exprs[i] = fmt.Sprintf("Alias%s: %s,", a.Target, a.Expr)
}
return exprs
}
func (si *structInfo) Assigns() []string {
exprs := make([]string, len(si.Aliases))
for i, a := range si.Aliases {
exprs[i] = fmt.Sprintf("v.%s = %s", a.Target, a.Assign)
}
return exprs
}
const tmplMarshalJSON = `func (v *{{.Receiver}}) MarshalJSON() ([]byte, error) {
type Alias {{.Receiver}}
return json.Marshal(&struct {
*Alias
{{- range .Aliases }}
Alias{{.Target}} {{.Type}} ` + "`json:" + `"{{.JSONKey}}"` + "`" + `
{{- end }}
}{
Alias: (*Alias)(v),
{{- range .Exprs }}
{{.}}
{{- end }}
})
}
`
const tmplUnmarshalJSON = `func (v *{{.Receiver}}) UnmarshalJSON(b []byte) error {
type Alias {{.Receiver}}
aux := &struct {
*Alias
{{- range .Aliases }}
Alias{{.Target}} {{.Type}} ` + "`json:" + `"{{.JSONKey}}"` + "`" + `
{{- end }}
}{
Alias: (*Alias)(v),
}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
{{- range .Assigns }}
{{.}}
{{- end }}
return nil
}
`