/
main.go
381 lines (340 loc) · 9.67 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
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
package main
import (
"bytes"
_ "embed"
"flag"
"fmt"
"github.com/fatih/structtag"
"go/ast"
"go/printer"
"go/token"
"go/types"
"golang.org/x/tools/go/packages"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"text/template"
)
const funGetter = "get"
const funSetter = "set"
const tagName = "gorm"
var (
outStruct = flag.String("out", "", "需要输出的结构体 ; must be set")
outFun = flag.String("fun", "", "需要生成的方法(Set|Get); must be set")
mod = flag.String("mod", "", "自定义模板(绝对路径); The file name must be sloth_getter.tmp|sloth_setter.tmp; Not required")
output = flag.String("output", "", "output file name; default srcdir/<type>_sloth.go")
columnCompile = regexp.MustCompile("column:([\\w]+);?")
autoFunc []string
customSetter, customGetter string
)
// Usage is a replacement usage function for the flags package.
func Usage() {
fmt.Fprintf(os.Stderr, "Usage of sloth:\n")
fmt.Fprintf(os.Stderr, "\tsloth [flags] -out T [structs]\n")
fmt.Fprintf(os.Stderr, "\tsloth [flags] -fun T [functions] # Must be a single package\n")
fmt.Fprintf(os.Stderr, "\tsloth [flags] -mod T [dir] # Must be a single package\n")
fmt.Fprintf(os.Stderr, "For more information, see:\n")
fmt.Fprintf(os.Stderr, "\thttps://gitee.com/dwdcth/sloth.git\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
log.SetFlags(0)
log.SetPrefix("sloth: ")
flag.Usage = Usage
flag.Parse()
if len(*outStruct) == 0 {
flag.Usage()
os.Exit(2)
}
types := strings.Split(*outStruct, ",")
autoFunc = strings.Split(*outFun, ",")
// We accept either one directory 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 dir string
g := Generator{
buf: make(map[string]*bytes.Buffer),
//structInfo: make(map[string]StructFieldInfoArr), //一定不能初始化
walkMark: make(map[string]bool),
}
if len(args) == 1 && isDirectory(args[0]) {
dir = args[0]
} else {
dir = filepath.Dir(args[0])
}
isCustomTemplate(*mod)
//ParseStruct(dir, nil, "access")
g.parsePackage(args)
// Print the header and package clause.
// Run generate for each type.
for i, typeName := range types {
g.generate(typeName)
// funSetter to file.
outputName := *output
if outputName == "" {
baseName := fmt.Sprintf("%s_sloth.go", types[i])
outputName = filepath.Join(dir, strings.ToLower(baseName))
}
buf, ok := g.buf[typeName]
if !ok {
panic(fmt.Sprintf("generate struct %s failed.", typeName))
}
var src = (buf).Bytes()
err := ioutil.WriteFile(outputName, src, 0644)
if err != nil {
log.Fatalf("writing output: %s", err)
}
}
}
//use custom template
func isCustomTemplate(mod string) {
if mod == "" {
return
}
modDir, err := filepath.Abs(mod)
if err != nil {
panic(fmt.Sprintf("Abs returns an absolute representation of %s, err : %+v", mod, err))
return
}
modDir = strings.Replace(modDir, "\\", "/", -1)
tmps := []string{"sloth_getter.tmp", "sloth_setter.tmp"}
for _, tmp := range tmps {
tmpAbsAddr := strings.Join([]string{modDir, tmp}, "/")
redr, err := ioutil.ReadFile(tmpAbsAddr)
if err != nil {
panic(fmt.Sprintf("Abs %s read failed, err =%+v.", tmpAbsAddr, err))
return
}
tp := string(redr)
//log.Printf("[isCustomTemplate] the mod =%s.", tp)
if strings.Contains(tmp, funGetter) {
customGetter = tp
} else {
customSetter = tp
}
}
}
// 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()
}
// Generator holds the state of the analysis. Primarily used to buffer
// the output for format.Source.
type Generator struct {
buf map[string]*bytes.Buffer // Accumulated output.
pkg *Package // Package we are scanning.
structInfo map[string]StructFieldInfoArr
walkMark map[string]bool
}
func (g *Generator) Printf(structName, format string, args ...interface{}) {
buf, ok := g.buf[structName]
if !ok {
buf = bytes.NewBufferString("")
g.buf[structName] = buf
}
fmt.Fprintf(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.
fileSet *token.FileSet
// These fields are reset for each type being generated.
typeName string // Name of the constant type.
}
type Package struct {
name string
defs map[*ast.Ident]types.Object
files []*File
}
// parsePackage analyzes the single package constructed from the patterns and tags.
// parsePackage exits if there is an error.
func (g *Generator) parsePackage(patterns []string) {
/* cfg := &packages.Config{
Mode: packages.LoadSyntax,
Tests: false,
}*/
cfg := &packages.Config{
Mode: packages.LoadAllSyntax,
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,
defs: pkg.TypesInfo.Defs,
files: make([]*File, len(pkg.Syntax)),
}
for i, file := range pkg.Syntax {
g.pkg.files[i] = &File{
file: file,
pkg: g.pkg,
fileSet: pkg.Fset,
}
}
}
// generate produces the String method for the named type.
func (g *Generator) generate(typeName string) {
tpts := getTemplate()
for _, file := range g.pkg.files { //按包来的,读取包下的所有文件
// Set the state for this run of the walker.
file.typeName = typeName
//ast.Print(file.fileSet, file.file)
if file.file != nil {
//自定义模板检查
/* if *mdo != "" {
}*/
structInfo, err := ParseStruct(file.file, file.fileSet, tagName)
if err != nil {
fmt.Println("失败:" + err.Error())
return
}
for stName, info := range structInfo {
if stName != typeName {
continue
}
g.Printf(stName, "// Code generated by \"sloth %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " "))
g.Printf(stName, "\n")
g.Printf(stName, "package %s\n", g.pkg.name)
g.Printf(stName, "\n")
for _, field := range info {
for _, access := range autoFunc {
switch access {
case funSetter:
g.Printf(stName, "%s\n", genSetter(tpts[1], stName, field.Name, field.column, field.Type))
case funGetter:
g.Printf(stName, "%s\n", genGetter(tpts[0], stName, field.Name, field.Type))
}
}
}
}
}
}
}
var (
//go:embed model/sloth_getter.tmp
getterTemplate string
//go:embed model/sloth_setter.tmp
setterTemplate string
)
func getTemplate() []*template.Template {
tptGetter := template.New("getter")
if customSetter != "" {
tptGetter = template.Must(tptGetter.Parse(customGetter))
} else {
tptGetter = template.Must(tptGetter.Parse(getterTemplate))
}
tptSetter := template.New("setter")
if customSetter != "" {
tptSetter = template.Must(tptSetter.Parse(customSetter))
} else {
tptSetter = template.Must(tptSetter.Parse(setterTemplate))
}
return []*template.Template{tptGetter, tptSetter}
}
type StructFieldInfo struct {
Name string
Type string
Access []string
column string
}
type StructFieldInfoArr = []StructFieldInfo
func ParseStruct(file *ast.File, fileSet *token.FileSet, tagName string) (structMap map[string]StructFieldInfoArr, err error) {
structMap = make(map[string]StructFieldInfoArr)
collectStructs := func(x ast.Node) bool {
ts, ok := x.(*ast.TypeSpec)
if !ok || ts.Type == nil {
return true
}
// 获取结构体名称
structName := ts.Name.Name
s, ok := ts.Type.(*ast.StructType)
if !ok {
return true
}
fileInfos := make([]StructFieldInfo, 0)
for _, field := range s.Fields.List {
kind := reflect.TypeOf(file).Kind()
if kind == reflect.Struct || kind == reflect.Array || kind == reflect.UnsafePointer || len(field.Names) == 0 {
continue
}
name := field.Names[0].Name
info := StructFieldInfo{Name: name}
var typeNameBuf bytes.Buffer
err := printer.Fprint(&typeNameBuf, fileSet, field.Type)
if err != nil {
fmt.Println("获取类型失败:", err)
return true
}
info.Type = typeNameBuf.String()
if field.Tag != nil { // 有tag
tag := field.Tag.Value
tag = strings.Trim(tag, "`")
tags, err := structtag.Parse(tag)
if err != nil {
return true
}
filedTag, err := tags.Get(tagName)
submatch := columnCompile.FindStringSubmatch(filedTag.Value())
if len(submatch) > 0 {
info.column = submatch[1]
}
} else {
firstChar := name[0:1]
if strings.ToUpper(firstChar) == firstChar { //大写
info.Access = []string{funGetter, funSetter}
} else { // 小写
info.Access = []string{funGetter}
}
}
fileInfos = append(fileInfos, info)
}
structMap[structName] = fileInfos
return false
}
ast.Inspect(file, collectStructs)
return structMap, nil
}
func genSetter(tpt *template.Template, structName, fieldName, column, typeName string) string {
res := bytes.NewBufferString("\t")
tpt.Execute(res, map[string]string{
"Receiver": strings.ToLower(structName[0:1]),
"Struct": structName,
"Field": fieldName,
"Type": typeName,
"Column": column,
})
return res.String()
}
func genGetter(t *template.Template, structName, fieldName, typeName string) string {
res := bytes.NewBufferString("\t")
t.Execute(res, map[string]string{
"Receiver": strings.ToLower(structName[0:1]),
"Struct": structName,
"Field": fieldName,
"Type": typeName,
})
return res.String()
}