forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
533 lines (460 loc) · 13.7 KB
/
generator.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package gen
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"text/template"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/gen/internal/check"
"gorm.io/gen/internal/model"
"gorm.io/gen/internal/parser"
tmpl "gorm.io/gen/internal/template"
"gorm.io/gen/internal/utils/pools"
)
// T generic type
type T interface{}
// M map[string]interface{}
type M map[string]interface{}
// RowsAffected execute affected raws
type RowsAffected int64
var concurrent = runtime.NumCPU()
func init() { runtime.GOMAXPROCS(runtime.NumCPU()) }
// NewGenerator create a new generator
func NewGenerator(cfg Config) *Generator {
err := cfg.Revise()
if err != nil {
panic(fmt.Errorf("create generator fail: %w", err))
}
return &Generator{
Config: cfg,
Data: make(map[string]*genInfo),
modelData: make(map[string]*check.BaseStruct),
}
}
// genInfo info about generated code
type genInfo struct {
*check.BaseStruct
Interfaces []*check.InterfaceMethod
}
func (i *genInfo) appendMethods(methods []*check.InterfaceMethod) error {
for _, newMethod := range methods {
if i.methodInGenInfo(newMethod) {
continue
}
i.Interfaces = append(i.Interfaces, newMethod)
}
return nil
}
func (i *genInfo) methodInGenInfo(m *check.InterfaceMethod) bool {
for _, method := range i.Interfaces {
if method.IsRepeatFromSameInterface(m) {
return true
}
}
return false
}
// Generator code generator
type Generator struct {
Config
Data map[string]*genInfo //gen query data
modelData map[string]*check.BaseStruct //gen model data
}
// UseDB set db connection
func (g *Generator) UseDB(db *gorm.DB) {
if db != nil {
g.db = db
}
}
/*
** The feature of mapping table from database server to Golang struct
** Provided by @qqxhb
*/
// GenerateModel catch table info from db, return a BaseStruct
func (g *Generator) GenerateModel(tableName string, opts ...FieldOpt) *check.BaseStruct {
return g.GenerateModelAs(tableName, g.db.Config.NamingStrategy.SchemaName(tableName), opts...)
}
// GenerateModel catch table info from db, return a BaseStruct
func (g *Generator) GenerateModelAs(tableName string, modelName string, fieldOpts ...FieldOpt) *check.BaseStruct {
modelFieldOpts := make([]model.FieldOpt, len(fieldOpts))
for i, opt := range fieldOpts {
modelFieldOpts[i] = opt
}
s, err := check.GenBaseStructs(g.db, model.Conf{
ModelPkg: g.Config.ModelPkgPath,
TablePrefix: g.getTablePrefix(),
TableName: tableName,
ModelName: modelName,
ImportPkgPaths: g.importPkgPaths,
SchemaNameOpts: g.dbNameOpts,
TableNameNS: g.tableNameNS,
ModelNameNS: g.modelNameNS,
FileNameNS: g.fileNameNS,
FieldConf: model.FieldConf{
DataTypeMap: g.dataTypeMap,
FieldNullable: g.FieldNullable,
FieldCoverable: g.FieldCoverable,
FieldWithIndexTag: g.FieldWithIndexTag,
FieldWithTypeTag: g.FieldWithTypeTag,
FieldJSONTagNS: g.fieldJSONTagNS,
FieldNewTagNS: g.fieldNewTagNS,
FieldOpts: modelFieldOpts,
},
})
if err != nil {
g.db.Logger.Error(context.Background(), "generate struct from table fail: %s", err)
panic("generate struct fail")
}
g.modelData[s.StructName] = s
g.successInfo(fmt.Sprintf("got %d columns from table <%s>", len(s.Fields), s.TableName))
return s
}
func (g *Generator) getTablePrefix() string {
if ns, ok := g.db.NamingStrategy.(schema.NamingStrategy); ok {
return ns.TablePrefix
}
return ""
}
// GenerateAllTable generate all tables in db
func (g *Generator) GenerateAllTable(opts ...FieldOpt) (tableModels []interface{}) {
tableList, err := g.db.Migrator().GetTables()
if err != nil {
panic(fmt.Sprintf("get all tables fail: %s", err))
}
g.successInfo(fmt.Sprintf("find %d table from db: %s", len(tableList), tableList))
tableModels = make([]interface{}, len(tableList))
for i, tableName := range tableList {
tableModels[i] = g.GenerateModel(tableName, opts...)
}
return tableModels
}
// ApplyBasic specify models which will implement basic method
func (g *Generator) ApplyBasic(models ...interface{}) {
g.ApplyInterface(func() {}, models...)
}
// ApplyInterface specifies method interfaces on structures, implment codes will be generated after calling g.Execute()
// eg: g.ApplyInterface(func(model.Method){}, model.User{}, model.Company{})
func (g *Generator) ApplyInterface(fc interface{}, models ...interface{}) {
structs, err := check.CheckStructs(g.db, models...)
if err != nil {
g.db.Logger.Error(context.Background(), "check struct fail: %v", err)
panic("check struct fail")
}
g.apply(fc, structs)
}
func (g *Generator) apply(fc interface{}, structs []*check.BaseStruct) {
readInterface := new(parser.InterfaceSet)
interfacePaths, err := parser.GetInterfacePath(fc)
if err != nil {
g.db.Logger.Error(context.Background(), "get interface name or file fail: %s", err)
panic("check interface fail")
}
err = readInterface.ParseFile(interfacePaths, check.GetStructNames(structs))
if err != nil {
g.db.Logger.Error(context.Background(), "parser interface file fail: %s", err)
panic("parser interface file fail")
}
for _, interfaceStruct := range structs {
if g.judgeMode(WithoutContext) {
interfaceStruct.ReviseFieldName()
}
data, err := g.pushBaseStruct(interfaceStruct)
if err != nil {
g.db.Logger.Error(context.Background(), "gen struct fail: %v", err)
panic("gen struct fail")
}
functions, err := check.BuildDiyMethod(readInterface, interfaceStruct, data.Interfaces)
if err != nil {
g.db.Logger.Error(context.Background(), "check interface fail: %v", err)
panic("check interface fail")
}
err = data.appendMethods(functions)
if err != nil {
g.db.Logger.Error(context.Background(), "check interface fail: %v", err)
panic("check interface fail")
}
}
}
// Execute generate code to output path
func (g *Generator) Execute() {
g.successInfo("Start generating code.")
if err := g.generateModelFile(); err != nil {
g.db.Logger.Error(context.Background(), "generate model struct fail: %s", err)
panic("generate model struct fail")
}
if err := g.generateQueryFile(); err != nil {
g.db.Logger.Error(context.Background(), "generate query code fail: %s", err)
panic("generate query code fail")
}
g.successInfo("Generate code done.")
}
// successInfo logger
func (g *Generator) successInfo(logInfos ...string) {
for _, l := range logInfos {
g.db.Logger.Info(context.Background(), l)
log.Println(l)
}
}
// generateQueryFile generate query code and save to file
func (g *Generator) generateQueryFile() (err error) {
if len(g.Data) == 0 {
return nil
}
if err := os.MkdirAll(g.OutPath, os.ModePerm); err != nil {
return fmt.Errorf("make dir outpath(%s) fail: %s", g.OutPath, err)
}
errChan := make(chan error)
pool := pools.NewPool(concurrent)
// generate query code for all struct
for _, info := range g.Data {
pool.Wait()
go func(info *genInfo) {
defer pool.Done()
err = g.generateSingleQueryFile(info)
if err != nil {
errChan <- err
}
if g.WithUnitTest {
err = g.generateQueryUnitTestFile(info)
if err != nil { // do not panic
g.db.Logger.Error(context.Background(), "generate unit test fail: %s", err)
}
}
}(info)
}
select {
case err = <-errChan:
return err
case <-pool.AsyncWaitAll():
}
// generate query file
var buf bytes.Buffer
err = render(tmpl.Header, &buf, map[string]interface{}{
"Package": g.queryPkgName,
"StructPkgPath": "",
"ImportPkgPaths": g.importPkgPaths,
})
if err != nil {
return err
}
if g.judgeMode(WithDefaultQuery) {
err = render(tmpl.DefaultQuery, &buf, g)
if err != nil {
return err
}
}
err = render(tmpl.QueryMethod, &buf, g)
if err != nil {
return err
}
err = g.output(g.OutFile, buf.Bytes())
if err != nil {
return err
}
g.successInfo("generate query file: " + g.OutFile)
// generate query unit test file
if g.WithUnitTest {
buf.Reset()
err = render(tmpl.UnitTestHeader, &buf, map[string]interface{}{
"Package": g.queryPkgName,
"StructPkgPath": "",
"ImportPkgPaths": g.importPkgPaths,
})
if err != nil {
g.db.Logger.Error(context.Background(), "generate query unit test fail: %s", err)
return nil
}
err = render(tmpl.DIYMethod_TEST_Basic, &buf, nil)
if err != nil {
return err
}
err = render(tmpl.QueryMethod_TEST, &buf, g)
if err != nil {
g.db.Logger.Error(context.Background(), "generate query unit test fail: %s", err)
return nil
}
fileName := strings.TrimSuffix(g.OutFile, ".go") + "_test.go"
err = g.output(fileName, buf.Bytes())
if err != nil {
g.db.Logger.Error(context.Background(), "generate query unit test fail: %s", err)
return nil
}
g.successInfo("generate unit test file: " + fileName)
}
return nil
}
// generateSingleQueryFile generate query code and save to file
func (g *Generator) generateSingleQueryFile(data *genInfo) (err error) {
var buf bytes.Buffer
structPkgPath := data.StructInfo.PkgPath
if structPkgPath == "" {
structPkgPath = g.modelPkgPath
}
err = render(tmpl.Header, &buf, map[string]interface{}{
"Package": g.queryPkgName,
"StructPkgPath": structPkgPath,
"ImportPkgPaths": data.ImportPkgPaths,
})
if err != nil {
return err
}
structTmpl := tmpl.BaseStructWithContext
if g.judgeMode(WithoutContext) {
structTmpl = tmpl.BaseStruct
}
err = render(structTmpl, &buf, data.BaseStruct)
if err != nil {
return err
}
for _, method := range data.Interfaces {
err = render(tmpl.DIYMethod, &buf, method)
if err != nil {
return err
}
}
err = render(tmpl.CRUDMethod, &buf, data.BaseStruct)
if err != nil {
return err
}
defer g.successInfo(fmt.Sprintf("generate query file: %s/%s.gen.go", g.OutPath, data.FileName))
return g.output(fmt.Sprintf("%s/%s.gen.go", g.OutPath, data.FileName), buf.Bytes())
}
// generateQueryUnitTestFile generate unit test file for query
func (g *Generator) generateQueryUnitTestFile(data *genInfo) (err error) {
var buf bytes.Buffer
structPkgPath := data.StructInfo.PkgPath
if structPkgPath == "" {
structPkgPath = g.modelPkgPath
}
err = render(tmpl.UnitTestHeader, &buf, map[string]interface{}{
"Package": g.queryPkgName,
"StructPkgPath": structPkgPath,
"ImportPkgPaths": data.ImportPkgPaths,
})
if err != nil {
return err
}
err = render(tmpl.CRUDMethod_TEST, &buf, data.BaseStruct)
if err != nil {
return err
}
for _, method := range data.Interfaces {
err = render(tmpl.DIYMethod_TEST, &buf, method)
if err != nil {
return err
}
}
defer g.successInfo(fmt.Sprintf("generate unit test file: %s/%s.gen_test.go", g.OutPath, data.FileName))
return g.output(fmt.Sprintf("%s/%s.gen_test.go", g.OutPath, data.FileName), buf.Bytes())
}
// generateModelFile generate model structures and save to file
func (g *Generator) generateModelFile() error {
if len(g.modelData) == 0 {
return nil
}
modelOutPath, err := g.getModelOutputPath()
if err != nil {
return err
}
if err := os.MkdirAll(modelOutPath, os.ModePerm); err != nil {
return fmt.Errorf("create model pkg path(%s) fail: %s", modelOutPath, err)
}
errChan := make(chan error)
pool := pools.NewPool(concurrent)
for _, data := range g.modelData {
if data == nil || !data.GenBaseStruct {
continue
}
pool.Wait()
go func(data *check.BaseStruct) {
defer pool.Done()
var buf bytes.Buffer
err = render(tmpl.Model, &buf, data)
if err != nil {
errChan <- err
}
modelFile := modelOutPath + data.FileName + ".gen.go"
err = g.output(modelFile, buf.Bytes())
if err != nil {
errChan <- err
}
g.successInfo(fmt.Sprintf("generate model file(table <%s> -> {%s.%s}): %s", data.TableName, data.StructInfo.Package, data.StructInfo.Type, modelFile))
}(data)
}
select {
case err = <-errChan:
return err
case <-pool.AsyncWaitAll():
g.fillModelPkgPath(modelOutPath)
}
return nil
}
func (g *Generator) getModelOutputPath() (outPath string, err error) {
if strings.Contains(g.ModelPkgPath, "/") {
outPath, err = filepath.Abs(g.ModelPkgPath)
if err != nil {
return "", fmt.Errorf("cannot parse model pkg path: %w", err)
}
} else {
outPath = filepath.Dir(g.OutPath) + "/" + g.ModelPkgPath
}
return outPath + "/", nil
}
func (g *Generator) fillModelPkgPath(filePath string) {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName,
Dir: filePath,
})
if err != nil {
g.db.Logger.Warn(context.Background(), "parse model pkg path fail: %s", err)
return
}
if len(pkgs) == 0 {
g.db.Logger.Warn(context.Background(), "parse model pkg path fail: got 0 packages")
return
}
g.Config.modelPkgPath = pkgs[0].PkgPath
}
// output format and output
func (g *Generator) output(fileName string, content []byte) error {
result, err := imports.Process(fileName, content, nil)
if err != nil {
line := strings.Split(string(content), "\n")
errLine, _ := strconv.Atoi(strings.Split(err.Error(), ":")[1])
startLine, endLine := errLine-5, errLine+5
fmt.Println("Format fail:", errLine, err)
for i := startLine; i <= endLine; i++ {
fmt.Println(i, line[i])
}
return fmt.Errorf("cannot format file: %w", err)
}
return ioutil.WriteFile(fileName, result, 0640)
}
func (g *Generator) pushBaseStruct(base *check.BaseStruct) (*genInfo, error) {
structName := base.StructName
if g.Data[structName] == nil {
g.Data[structName] = &genInfo{BaseStruct: base}
}
if g.Data[structName].Source != base.Source {
return nil, fmt.Errorf("cannot generate struct with the same name from different source:%s.%s and %s.%s",
base.StructInfo.Package, base.StructName, g.Data[structName].StructInfo.Package, g.Data[structName].StructName)
}
return g.Data[structName], nil
}
func render(tmpl string, wr io.Writer, data interface{}) error {
t, err := template.New(tmpl).Parse(tmpl)
if err != nil {
return err
}
return t.Execute(wr, data)
}