-
Notifications
You must be signed in to change notification settings - Fork 249
/
rewritejsontag.go
86 lines (82 loc) · 2.15 KB
/
rewritejsontag.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
package astutils
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/unionj-cloud/go-doudou/toolkit/caller"
"go/ast"
"go/format"
"go/parser"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"regexp"
"strings"
"unicode"
)
func isExport(field string) bool {
return unicode.IsUpper([]rune(field)[0])
}
func extractJsonPropName(tag string) string {
re := regexp.MustCompile(`json:"(.*?)"`)
if re.MatchString(tag) {
subs := re.FindAllStringSubmatch(tag, -1)
return strings.TrimSpace(strings.Split(subs[0][1], ",")[0])
}
return ""
}
// RewriteJSONTag overwrites json tag by convert function and return formatted source code
func RewriteJSONTag(file string, omitempty bool, convert func(old string) string) (string, error) {
fset := token.NewFileSet()
root, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
return "", errors.Wrap(err, caller.NewCaller().String())
}
re := regexp.MustCompile(`json:"(.*?)"`)
astutil.Apply(root, func(cursor *astutil.Cursor) bool {
return true
}, func(cursor *astutil.Cursor) bool {
structSpec, ok := cursor.Node().(*ast.StructType)
if !ok {
return true
}
for _, field := range structSpec.Fields.List {
if field.Names == nil {
continue
}
fname := field.Names[0].Name
if !isExport(fname) {
continue
}
tag := convert(field.Names[0].Name)
if omitempty {
tag += ",omitempty"
}
tag = fmt.Sprintf(`json:"%s"`, tag)
if field.Tag != nil {
if re.MatchString(field.Tag.Value) {
if extractJsonPropName(field.Tag.Value) != "-" {
field.Tag.Value = re.ReplaceAllLiteralString(field.Tag.Value, tag)
}
} else {
lastindex := strings.LastIndex(field.Tag.Value, "`")
if lastindex < 0 {
panic(errors.New("call LastIndex() error"))
}
field.Tag.Value = field.Tag.Value[:lastindex] + fmt.Sprintf(" %s`", tag)
}
} else {
field.Tag = &ast.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf("`%s`", tag),
}
}
}
return true
})
buf := &bytes.Buffer{}
err = format.Node(buf, fset, root)
if err != nil {
return "", fmt.Errorf("error formatting new code: %w", err)
}
return buf.String(), nil
}