-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
392 lines (344 loc) · 9.19 KB
/
parser.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
382
383
384
385
386
387
388
389
390
391
392
package main
import (
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"golang.org/x/exp/maps"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"golang.org/x/tools/go/ast/inspector"
)
func camelCaseToSnakeCase(camel string) (snake string) {
re := regexp.MustCompile("([a-z])([A-Z]+)")
snake = re.ReplaceAllString(camel, "${1}_${2}")
snake = strings.ToLower(snake)
return
}
func snakeCaseToCamelCase(snake string, firstLetterUpperCase bool) (camel string) {
titler := cases.Title(language.AmericanEnglish)
words := strings.Split(snake, "_")
if firstLetterUpperCase {
camel = titler.String(words[0])
} else {
camel = words[0]
}
for _, word := range words[1:] {
camel += titler.String(word)
}
return
}
func mkPlural(singular string) (plural string) {
re := regexp.MustCompile("y$")
plural = re.ReplaceAllString(singular, "ie")
plural += "s"
return
}
type tag struct {
byNumber []string
byName map[string]string
}
func newTag(text string, name string) *tag {
tags := make(map[string]tag)
const (
outer uint8 = iota
initial
tagString
optionName
optionValue
)
var (
state uint8 = outer
start int
tagName string
optName string
curTag *tag
outerDelimiter byte
)
for i := 0; i < len(text); i++ {
//fmt.Printf("i=%d c=%c state=%d\n", i, text[i], state)
switch state {
case outer:
outerDelimiter = text[i]
state = initial
start = i + 1
case initial:
switch text[i] {
case byte(':'), outerDelimiter:
tagName = text[start:i]
state = tagString
curTag = &tag{byName: make(map[string]string, 1), byNumber: make([]string, 0, 1)}
tags[tagName] = *curTag
case byte(' '):
start = i + 1
}
case tagString:
switch text[i] {
case byte('"'):
state = optionName
start = i + 1
optName = ""
case outerDelimiter:
default:
log.Fatalf("Tag delimiter %c doesn't supported", text[i])
}
case optionName:
switch text[i] {
case byte('='):
optName = text[start:i]
state = optionValue
start = i + 1
case byte(' '), byte('"'):
optName = text[start:i]
curTag.byNumber = append(tags[tagName].byNumber, optName)
curTag.byName[optName] = ""
if text[i] == byte('"') {
state = initial
}
start = i + 1
}
case optionValue:
if text[i] == byte(' ') || text[i] == byte('"') {
optValue := text[start:i]
curTag.byName[optName] = optValue
if text[i] == byte(' ') {
state = optionName
} else if text[i] == byte('"') {
state = initial
}
start = i + 1
}
}
}
tag, ok := tags[name]
if ok {
return &tag
} else {
return nil
}
}
type structInfo struct {
typeSpec *ast.TypeSpec
structType *ast.StructType
isForGentity bool
}
type structsMap map[string]structInfo
func procFieldTag(f *ast.Field, ent *entity, fld *field) {
if f.Tag != nil {
log.Println(ent.GoName, "field", fld.GoName, "tag", f.Tag.Value)
tag := newTag(f.Tag.Value, "gentity")
if tag != nil {
if index, ok := tag.byName["index"]; ok {
if _, ok := ent.NonUniqIndexes[index]; !ok {
ent.NonUniqIndexes[index] = []*field{fld}
} else {
ent.NonUniqIndexes[index] = append(ent.NonUniqIndexes[index], fld)
}
}
if unique, ok := tag.byName["unique"]; ok {
log.Println(ent.GoName, "uniq", unique, "field", fld.GoName)
if _, ok := ent.UniqIndexes[unique]; !ok {
ent.UniqIndexes[unique] = []*field{fld}
} else {
ent.UniqIndexes[unique] = append(ent.UniqIndexes[unique], fld)
}
}
if _, ok := tag.byName["autoincrement"]; ok {
ent.AutoIncrementField = fld
}
}
} else {
log.Println(ent.GoName, "field", fld.GoName, "no tag")
}
}
func (sm structsMap) procType(f *ast.Field, e ast.Expr, ent *entity) {
switch e.(type) {
case *ast.Ident:
id := e.(*ast.Ident)
log.Println(ent.GoName, "table", ent.SQLName, "fields:", f.Names, "ident.name:", id.Name)
if len(f.Names) == 0 {
if _, ok := sm[id.Name]; !ok {
log.Fatalf("Embedded structure %s wasn't found in package", id.Name)
}
subt := sm.procStruct(id.Name)
subt.Fields[0].OpeningEmbed = append(subt.Fields[0].OpeningEmbed, id.Name)
subt.Fields[len(subt.Fields)-1].ClosingEmbed = append(subt.Fields[0].ClosingEmbed, id.Name)
for i := range subt.Fields {
subt.Fields[i].EmbedLevel++
subt.Fields[i].Num = len(ent.Fields) + i
}
for name, uniq := range subt.UniqIndexes {
ent.UniqIndexes[name] = uniq
}
for name, index := range subt.NonUniqIndexes {
ent.NonUniqIndexes[name] = index
}
ent.Fields = append(ent.Fields, subt.Fields...)
} else {
fld := newField()
fld.GoName = f.Names[0].Name
fld.SQLName = camelCaseToSnakeCase(f.Names[0].Name)
fld.GoType = e.(*ast.Ident).Name
fld.Num = len(ent.Fields)
procFieldTag(f, ent, fld)
ent.Fields = append(ent.Fields, fld)
}
case *ast.SelectorExpr:
fld := newField()
fld.GoName = f.Names[0].Name
fld.SQLName = camelCaseToSnakeCase(f.Names[0].Name)
fld.GoType = e.(*ast.SelectorExpr).Sel.Name
fld.Num = len(ent.Fields)
if expX, ok := e.(*ast.SelectorExpr).X.(*ast.Ident); ok {
fld.GoType = expX.Name + "." + fld.GoType
}
procFieldTag(f, ent, fld)
ent.Fields = append(ent.Fields, fld)
case *ast.StarExpr:
se := e.(*ast.StarExpr)
sm.procType(f, se.X, ent)
ent.Fields[len(ent.Fields)-1].IsRef = true
case *ast.ArrayType:
at := e.(*ast.ArrayType)
sm.procType(f, at.Elt, ent)
ent.Fields[len(ent.Fields)-1].IsArray = true
default:
id := e.(*ast.Ident)
log.Fatalf("Unknown type of field %s.%s type: %+v", ent.GoName, id.Name, e)
}
}
func (sm structsMap) procStruct(name string) (e *entity) {
e = newEntity()
if _, ok := sm[name]; !ok {
log.Fatalf("Struct type %s not found in package", name)
}
e.GoName = sm[name].typeSpec.Name.Name
e.SQLName = e.GoName
if !*singularTablesNames {
e.SQLName = mkPlural(e.SQLName)
}
e.SQLName = camelCaseToSnakeCase(e.SQLName)
for _, f := range sm[name].structType.Fields.List {
sm.procType(f, f.Type, e)
}
for i, f := range e.Fields {
//e.Fields[i].Num = i
if f.IsArray {
e.Fields[i].GoType = "[]" + f.GoType
}
if f.IsRef {
e.Fields[i].GoType = "*" + f.GoType
}
}
for name, fields := range e.UniqIndexes {
if name == "primary" || (e.PrimaryKey != "" && len(e.UniqIndexes[e.PrimaryKey]) > len(fields)) || e.PrimaryKey == "" {
e.PrimaryKey = name
}
for _, f := range fields {
log.Println(e.GoName, name, "uniq", f.GoName)
}
}
if e.PrimaryKey != "" {
e.FieldsExcludePrimaryKey = make([]*field, 0, len(e.Fields)-len(e.UniqIndexes[e.PrimaryKey]))
for _, f := range e.UniqIndexes[e.PrimaryKey] {
e.Fields[f.Num].InPrimaryKey = true
}
for _, f := range e.Fields {
if !f.InPrimaryKey {
e.FieldsExcludePrimaryKey = append(e.FieldsExcludePrimaryKey, f)
}
}
} else {
e.FieldsExcludePrimaryKey = e.Fields
}
shortestUniqKeyLength := len(e.Fields)
shortestUniqKeyWOAutoIncrementLength := len(e.Fields)
for name, fields := range e.UniqIndexes {
var hasAutoIncrement bool
for _, f := range fields {
f.InIndexes = append(f.InIndexes, name)
if e.AutoIncrementField != nil && e.AutoIncrementField.GoName != f.GoName {
hasAutoIncrement = true
}
}
if len(fields) < shortestUniqKeyLength {
shortestUniqKeyLength = len(fields)
e.ShortestUniqKey = name
}
if !hasAutoIncrement && len(fields) < shortestUniqKeyWOAutoIncrementLength {
shortestUniqKeyWOAutoIncrementLength = len(fields)
e.ShortestUniqWOAutoIncrementKey = name
}
}
for name, fields := range e.NonUniqIndexes {
for _, f := range fields {
f.InIndexes = append(f.InIndexes, name)
}
}
if e.AutoIncrementField == nil {
e.FieldsExcludeAutoIncrement = e.Fields
} else {
e.FieldsExcludeAutoIncrement = make([]*field, 0, len(e.Fields)-1)
for _, f := range e.Fields {
if e.AutoIncrementField.GoName != f.GoName {
e.FieldsExcludeAutoIncrement = append(e.FieldsExcludeAutoIncrement, f)
}
}
}
return
}
func parse() (packageName string, entities []entity) {
path := os.Getenv("GOFILE")
if path == "" {
log.Fatal("GOFILE must be set")
}
astPkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Dir(path), nil, parser.ParseComments)
if err != nil {
log.Fatalf("parse dir: %v", err)
}
if len(astPkgs) != 1 {
log.Fatalf("Not one package found")
}
var files []*ast.File
for _, p := range astPkgs {
files = append(files, maps.Values(p.Files)...)
packageName = p.Name
}
structs := structsMap(make(map[string]structInfo))
inspector.New(files).Nodes([]ast.Node{&ast.GenDecl{}}, func(node ast.Node, push bool) (proceed bool) {
genDecl := node.(*ast.GenDecl)
si := structInfo{}
var ok bool
si.typeSpec, ok = genDecl.Specs[0].(*ast.TypeSpec)
if !ok {
return false
}
si.structType, ok = si.typeSpec.Type.(*ast.StructType)
if !ok {
return false
}
if genDecl.Doc != nil {
for _, comment := range genDecl.Doc.List {
if comment.Text == "// gentity" {
si.isForGentity = true
}
}
}
structs[si.typeSpec.Name.Name] = si
return false
})
for name, si := range structs {
if !si.isForGentity {
continue
}
//log.Println("proc struct", name)
entity := structs.procStruct(name)
//log.Printf("entity %s: %+v", name, entity)
entities = append(entities, *entity)
}
return
}