-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparser.go
More file actions
155 lines (140 loc) · 4.09 KB
/
parser.go
File metadata and controls
155 lines (140 loc) · 4.09 KB
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
package gormgen
import (
"github.com/jinzhu/gorm"
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"strings"
)
// The Parser is used to parse a directory and expose information about the structs defined in the files of this directory.
type Parser struct {
dir string
pkg *build.Package
parsedFiles []*ast.File
}
// NewParser create a new parser instance.
func NewParser(dir string) *Parser {
return &Parser{
dir: dir,
}
}
// getPackage parse dir get go file and package
func (p *Parser) getPackage() {
pkg, err := build.Default.ImportDir(p.dir, build.ImportComment)
if err != nil {
log.Fatalf("cannot process directory %s: %s", p.dir, err)
}
p.pkg = pkg
}
// parseGoFiles parse go file
func (p *Parser) parseGoFiles() {
var parsedFiles []*ast.File
fs := token.NewFileSet()
for _, file := range p.pkg.GoFiles {
file = p.dir + "/" + file
parsedFile, err := parser.ParseFile(fs, file, nil, 0)
if err != nil {
log.Fatalf("parsing package: %s: %s\n", file, err)
}
parsedFiles = append(parsedFiles, parsedFile)
}
p.parsedFiles = parsedFiles
}
// parseTypes parse type of struct
func (p *Parser) parseTypes(file *ast.File) (ret []structConfig) {
ast.Inspect(file, func(n ast.Node) bool {
decl, ok := n.(*ast.GenDecl)
if !ok || decl.Tok != token.TYPE {
return true
}
for _, spec := range decl.Specs {
var (
data structConfig
)
typeSpec, _ok := spec.(*ast.TypeSpec)
if !_ok {
continue
}
// We only care about struct declaration (for now)
var structType *ast.StructType
if structType, ok = typeSpec.Type.(*ast.StructType); !ok {
continue
}
data.StructName = typeSpec.Name.Name
for _, v := range structType.Fields.List {
var (
onlyField fieldConfig
optionField fieldConfig
)
// type is select expr, and sel is Model , else continue
if _v, _ok := v.Type.(*ast.SelectorExpr); _ok {
if _v.Sel.Name == "Model" {
onlyField.FieldName = "ID"
onlyField.FieldType = "uint"
onlyField.ColumnName = gorm.ToDBName("ID")
onlyField.HumpName = SQLColumnToHumpStyle(onlyField.ColumnName)
data.OnlyFields = append(data.OnlyFields, onlyField)
f1 := fieldConfig{}
f1.FieldName = "CreatedAt"
f1.FieldType = "time.Time"
f1.ColumnName = gorm.ToDBName("CreatedAt")
f1.HumpName = SQLColumnToHumpStyle(f1.ColumnName)
data.OptionFields = append(data.OptionFields, f1)
f2 := fieldConfig{}
f2.FieldName = "UpdatedAt"
f2.FieldType = "time.Time"
f2.ColumnName = gorm.ToDBName("UpdatedAt")
f2.HumpName = SQLColumnToHumpStyle(f2.ColumnName)
data.OptionFields = append(data.OptionFields, f2)
}
continue
}
// get onlyField tag
if v.Tag != nil {
if strings.Contains(v.Tag.Value, "gorm") && strings.Contains(v.Tag.Value, "unique") ||
strings.Contains(v.Tag.Value, "primary") {
// type is ident, get onlyField type
if t, _ok := v.Type.(*ast.Ident); _ok {
onlyField.FieldType = t.String()
}
// get file name
if len(v.Names) > 0 {
onlyField.FieldName = v.Names[0].String()
onlyField.ColumnName = gorm.ToDBName(onlyField.FieldName)
}
data.OnlyFields = append(data.OnlyFields, onlyField)
continue
}
}
// type is ident, get onlyField type
if t, _ok := v.Type.(*ast.Ident); _ok {
optionField.FieldType = t.String()
}
// get file name
if len(v.Names) > 0 {
optionField.FieldName = v.Names[0].String()
optionField.ColumnName = gorm.ToDBName(optionField.FieldName)
optionField.HumpName = SQLColumnToHumpStyle(optionField.ColumnName)
}
data.OptionFields = append(data.OptionFields, optionField)
}
ret = append(ret, data)
}
return true
})
return
}
// Parse should be called before any type querying for the parser. It takes the directory to be parsed and extracts all the structs defined in this directory.
func (p *Parser) Parse() (ret []structConfig) {
var (
data []structConfig
)
p.getPackage()
p.parseGoFiles()
for _, f := range p.parsedFiles {
data = append(data, p.parseTypes(f)...)
}
return data
}