-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
66 lines (55 loc) · 1.19 KB
/
generate.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
package go_generate
import (
"github.com/sicuni/go-generate/config"
"github.com/sicuni/go-generate/database"
"github.com/sicuni/go-generate/internal/struct"
)
type Generator struct {
db database.Database
executor *Executor
Err error
}
func New(config *config.Config) *Generator {
return &Generator{
executor: &Executor{
Data: make(map[string]*_struct.BaseStruct),
Config: config,
},
}
}
func (gen *Generator) UseDB(db database.Database, err error) {
if err != nil {
gen.Err = err
return
}
gen.db = db
}
func (gen *Generator) BindModel(base *_struct.BaseStruct) {
if gen.Err != nil {
return
}
gen.executor.Data[base.StructName] = base
}
func (gen *Generator) GenModel(tableName string) *_struct.BaseStruct {
if gen.Err != nil {
return nil
}
return gen.GenModelAs(tableName, "")
}
func (gen *Generator) GenModelAs(tableName, modelName string) *_struct.BaseStruct {
if gen.Err != nil {
return nil
}
baseStruct, err := _struct.GenBaseStructs(gen.db, tableName, modelName)
if err != nil {
gen.Err = err
return nil
}
return baseStruct
}
func (gen *Generator) Execute() error {
if gen.Err != nil {
return gen.Err
}
return gen.executor.Execute()
}