-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
167 lines (136 loc) · 3.68 KB
/
parse.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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
type fieldToken struct {
Name string
Type string
Tag string
}
type structToken struct {
Name string
Fields []fieldToken
}
// 表查询结构
type tableQuery struct {
Name string
Fields []fieldQuery
}
// 单个查询字段的定义
type fieldQuery struct {
structName string // 在结构体中的名字
tableName string // 在数据表中的名字
fieldType string // 字段类型,golang中的类型
}
// 解释一个文件
func parseFile(source string) ([]structToken, error) {
structToks := make([]structToken, 0, 8)
fset := token.NewFileSet()
astf, err := parser.ParseFile(fset, source, nil, 0)
if err != nil {
return nil, err
}
//ast.Print(fset, astf)
for _, decl := range astf.Decls {
genDecl, isGeneralDeclaration := decl.(*ast.GenDecl)
if !isGeneralDeclaration {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, isTypeDeclaration := spec.(*ast.TypeSpec)
if !isTypeDeclaration {
continue
}
structType, isStructTypeDeclaration := typeSpec.Type.(*ast.StructType)
if !isStructTypeDeclaration {
continue
}
// found a struct in the source code!
var structTok structToken
structTok.Name = typeSpec.Name.Name
structTok.Fields = make([]fieldToken, 0, len(structType.Fields.List))
// iterate through struct fields (1 line at a time)
for _, fieldLine := range structType.Fields.List {
fieldToks := make([]fieldToken, len(fieldLine.Names))
// get field name (or names because multiple vars can be declared in 1 line)
for i, fieldName := range fieldLine.Names {
fieldToks[i].Name = parseIdent(fieldName)
fieldToks[i].Tag = fieldLine.Tag.Value
}
var fieldType string
// get field type
switch typeToken := fieldLine.Type.(type) {
case *ast.Ident:
// simple types, e.g. bool, int
fieldType = parseIdent(typeToken)
case *ast.SelectorExpr:
// struct fields, e.g. time.Time, sql.NullString
fieldType = parseSelector(typeToken)
case *ast.ArrayType:
// arrays
fieldType = parseArray(typeToken)
case *ast.StarExpr:
// pointers
fieldType = parseStar(typeToken)
}
if fieldType == "" {
continue
}
// apply type to all variables declared in this line
for i := range fieldToks {
fieldToks[i].Type = fieldType
}
structTok.Fields = append(structTok.Fields, fieldToks...)
}
structToks = append(structToks, structTok)
}
}
return structToks, nil
}
func parseIdent(fieldType *ast.Ident) string {
// return like byte, string, int
return fieldType.Name
}
func parseSelector(fieldType *ast.SelectorExpr) string {
// return like time.Time, sql.NullString
ident, isIdent := fieldType.X.(*ast.Ident)
if !isIdent {
return ""
}
return fmt.Sprintf("%s.%s", parseIdent(ident), fieldType.Sel.Name)
}
func parseArray(fieldType *ast.ArrayType) string {
// return like []byte, []time.Time, []*byte, []*sql.NullString
var arrayType string
switch typeToken := fieldType.Elt.(type) {
case *ast.Ident:
arrayType = parseIdent(typeToken)
case *ast.SelectorExpr:
arrayType = parseSelector(typeToken)
case *ast.StarExpr:
arrayType = parseStar(typeToken)
}
if arrayType == "" {
return ""
}
return fmt.Sprintf("[]%s", arrayType)
}
func parseStar(fieldType *ast.StarExpr) string {
// return like *bool, *time.Time, *[]byte, and other array stuff
var starType string
switch typeToken := fieldType.X.(type) {
case *ast.Ident:
starType = parseIdent(typeToken)
case *ast.SelectorExpr:
starType = parseSelector(typeToken)
case *ast.ArrayType:
starType = parseArray(typeToken)
}
if starType == "" {
return ""
}
return fmt.Sprintf("*%s", starType)
}