/
scanner.go
56 lines (47 loc) · 1.18 KB
/
scanner.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
package gen_method
import (
"go/parser"
"go/types"
"golang.org/x/tools/go/loader"
)
func NewScanner(packagePath string) *Scanner {
ldr := loader.Config{}
ldr.AllowErrors = true
ldr.ParserMode = parser.ParseComments
ldr.Import(packagePath)
prog, err := ldr.Load()
if err != nil {
panic(err)
}
return &Scanner{
packagePath: packagePath,
prog: prog,
}
}
type Scanner struct {
packagePath string
prog *loader.Program
}
func (s *Scanner) Output(modelName string, ignoreTable bool) {
for pkg, packageInfo := range s.prog.AllPackages {
if packageInfo.Pkg.Path() == s.packagePath {
for ident, def := range packageInfo.Defs {
if ident.Name == modelName {
if typeName, ok := def.(*types.TypeName); ok {
if typeStruct, ok := typeName.Type().Underlying().(*types.Struct); ok {
m := Model{
Pkg: pkg,
Name: modelName,
UniqueIndex: make(map[string][]Field),
NormalIndex: make(map[string][]Field),
FuncMapContent: make(map[string]string),
}
m.collectInfoFromStructType(typeStruct)
m.Output(packageInfo.Pkg.Name(), ignoreTable)
}
}
}
}
}
}
}