-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenx.go
314 lines (275 loc) · 7.76 KB
/
genx.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
package genx
import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/printer"
"go/token"
"log"
"path/filepath"
"reflect"
"regexp"
"strings"
"github.com/OneOfOne/xast"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/imports"
)
type procFunc func(*xast.Node) *xast.Node
type GenX struct {
pkgName string
rewriters map[string]string
irepl *strings.Replacer
imports map[string]string
zeroTypes map[string]bool
curReturnTypes []string
visited map[ast.Node]bool
BuildTags []string
CommentFilters []func(string) string
rewriteFuncs map[reflect.Type][]procFunc
}
func New(pkgName string, rewriters map[string]string) *GenX {
g := &GenX{
pkgName: pkgName,
rewriters: map[string]string{},
imports: map[string]string{},
visited: map[ast.Node]bool{},
irepl: geireplacer(rewriters, true),
zeroTypes: map[string]bool{},
BuildTags: []string{"genx"},
}
g.rewriteFuncs = map[reflect.Type][]procFunc{
reflect.TypeOf((*ast.TypeSpec)(nil)): {g.rewriteTypeSpec},
reflect.TypeOf((*ast.Ident)(nil)): {g.rewriteIdent},
reflect.TypeOf((*ast.Field)(nil)): {g.rewriteField},
reflect.TypeOf((*ast.FuncDecl)(nil)): {g.rewriteFuncDecl},
reflect.TypeOf((*ast.File)(nil)): {g.rewriteFile},
reflect.TypeOf((*ast.Comment)(nil)): {g.rewriteComment},
reflect.TypeOf((*ast.SelectorExpr)(nil)): {g.rewriteSelectorExpr},
reflect.TypeOf((*ast.KeyValueExpr)(nil)): {g.rewriteKeyValueExpr},
reflect.TypeOf((*ast.InterfaceType)(nil)): {g.rewriteInterfaceType},
reflect.TypeOf((*ast.ReturnStmt)(nil)): {g.rewriteReturnStmt},
reflect.TypeOf((*ast.ArrayType)(nil)): {g.rewriteArrayType},
reflect.TypeOf((*ast.ChanType)(nil)): {g.rewriteChanType},
reflect.TypeOf((*ast.MapType)(nil)): {g.rewriteMapType},
reflect.TypeOf((*ast.FuncType)(nil)): {g.rewriteFuncType},
reflect.TypeOf((*ast.StarExpr)(nil)): {g.rewriteStarExpr},
reflect.TypeOf((*ast.Ellipsis)(nil)): {g.rewriteEllipsis},
}
for k, v := range rewriters {
name, pkg, sel := parsePackageWithType(v)
if pkg != "" {
g.imports[pkg] = name
}
if sel == "" {
sel = v
}
idx := strings.Index(k, ":")
typ, kw := k[:idx], k[idx+1:]
if v == "-" {
g.CommentFilters = append(g.CommentFilters, regexpReplacer(`\b`+kw+`\b`, ""))
} else {
switch typ {
case "field":
g.rewriters["selector:."+kw] = sel
case "type":
csel := cleanUpName.ReplaceAllString(sel, "")
kw = cleanUpName.ReplaceAllString(kw, "")
if isBuiltin := csel != "interface" && builtins[csel] != ""; isBuiltin {
g.BuildTags = append(g.BuildTags, "genx_"+strings.ToLower(kw)+"_builtin")
}
g.BuildTags = append(g.BuildTags, "genx_"+strings.ToLower(kw)+"_"+csel)
g.zeroTypes[sel] = false
g.CommentFilters = append(g.CommentFilters, regexpReplacer(`\b(`+kw+`)\b`, sel))
g.CommentFilters = append(g.CommentFilters, regexpReplacer(`(`+kw+`)`, strings.Title(csel)))
}
}
g.rewriters[k] = sel
}
return g
}
// Parse parses the input file or src and returns a ParsedFile and/or an error.
// For more details about fname/src check `go/parser.ParseFile`
func (g *GenX) Parse(fname string, src interface{}) (ParsedFile, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, fname, src, parser.ParseComments)
if err != nil {
return ParsedFile{Name: fname}, err
}
return g.process(0, fset, fname, file)
}
// ParsePKG will parse the provided package, on success it will then process the files with
// x/tools/imports (goimports) then return the resulting package.
func (g *GenX) ParsePkg(path string, includeTests bool) (out ParsedPkg, err error) {
ctx := build.Default
ctx.BuildTags = append(ctx.BuildTags, g.BuildTags...)
pkg, err := ctx.ImportDir(path, build.IgnoreVendor)
if err != nil {
return nil, err
}
out = make(ParsedPkg, 0, len(pkg.GoFiles))
fset := token.NewFileSet()
files := append([]string{}, pkg.GoFiles...)
if includeTests {
files = append(files, pkg.TestGoFiles...)
}
// TODO: process multiple files in the same time.
for i, name := range files {
var file *ast.File
if file, err = parser.ParseFile(fset, filepath.Join(pkg.Dir, name), nil, parser.ParseComments); err != nil {
return
}
var pf ParsedFile
if pf, err = g.process(i, fset, name, file); err != nil {
log.Printf("%s", pf.Src)
return
}
out = append(out, pf)
}
return
}
var removePkgAndImports = regexp.MustCompile(`package .*|import ".*|(?s:import \(.*?\)\n)`)
func (g *GenX) process(idx int, fset *token.FileSet, name string, file *ast.File) (pf ParsedFile, err error) {
for imp, name := range g.imports {
if name != "" {
astutil.AddNamedImport(fset, file, name, imp)
} else {
astutil.AddImport(fset, file, imp)
}
}
if g.pkgName != "" && g.pkgName != file.Name.Name {
file.Name.Name = g.pkgName
} else {
g.pkgName = file.Name.Name
}
var buf bytes.Buffer
if err = printer.Fprint(&buf, fset, xast.Walk(file, g.rewrite)); err != nil {
return
}
if idx == 0 && len(g.zeroTypes) > 0 {
buf.WriteByte('\n')
for t, used := range g.zeroTypes {
if used {
fmt.Fprintf(&buf, "var zero_%s %s\n", cleanUpName.ReplaceAllString(t, ""), t)
}
}
}
if pf.Src, err = imports.Process(name, buf.Bytes(), &imports.Options{
AllErrors: true,
Comments: true,
TabIndent: true,
TabWidth: 4,
}); err != nil {
pf.Src = buf.Bytes()
}
pf.Name = name
return
}
func (g *GenX) rewrite(node *xast.Node) *xast.Node {
n := node.Node()
if g.visited[n] {
//dbg.DumpWithDepth(4, n)
// log.Printf("%T %#+v", n, node.Parent().Node())
return node
}
g.visited[n] = true
if fns, ok := g.rewriteFuncs[reflect.TypeOf(n)]; ok {
for _, fn := range fns {
if node = fn(node); node.Canceled() {
break
}
}
}
return node
}
func (g *GenX) shouldNukeFuncBody(bs *ast.BlockStmt) (found bool) {
if bs == nil {
return
}
ast.Inspect(bs, func(n ast.Node) bool {
if found {
return false
}
switch n := n.(type) {
// BUG: maybe? should we delete the func if we remove a field?
case *ast.KeyValueExpr:
x := getIdent(n.Key)
if x == nil {
break
}
if found = g.rewriters["field:"+x.Name] == "-"; found {
return false
}
case *ast.SelectorExpr:
x := getIdent(n.X)
if x == nil {
break
}
if x.Obj != nil && x.Obj.Type != nil {
ot := getIdent(x.Obj.Type)
if found = ot != nil && g.rewriters["type:"+ot.Name] == "-"; found {
return false
}
}
if found = g.rewriters["field:"+n.Sel.Name] == "-"; found {
return false
}
case *ast.Ident:
if found = g.rewriters["type:"+n.Name] == "-"; found {
return false
}
// TODO handle removed fields / funcs
}
return true
})
return
}
func getIdent(ex interface{}) *ast.Ident {
switch ex := ex.(type) {
case *ast.Ident:
return ex
case *ast.StarExpr:
return getIdent(ex.X)
default:
return nil
}
}
var cleanUpName = regexp.MustCompile(`[^\w\d_]+`)
func geireplacer(m map[string]string, ident bool) *strings.Replacer {
kv := make([]string, 0, len(m)*2)
for k, v := range m {
k = k[strings.Index(k, ":")+1:]
if ident {
if a := builtins[v]; a != "" {
v = a
} else {
v = cleanUpName.ReplaceAllString(strings.Title(v), "")
}
}
kv = append(kv, k, v)
}
return strings.NewReplacer(kv...)
}
var builtins = map[string]string{
"string": "String",
"byte": "Byte",
"[]byte": "Bytes",
"rune": "Rune",
"int": "Int",
"uint": "Uint",
"int8": "Int8",
"uint8": "Uint8",
"int16": "Int16",
"uint16": "Uint16",
"int32": "Int32",
"uint32": "Uint32",
"int64": "Int64",
"uint64": "Uint64",
"float32": "Float32",
"float64": "Float64",
"complex64": "Cmplx64",
"complex128": "Cmplx128",
"interface{}": "Iface",
"Interface": "Iface",
}