forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
150 lines (121 loc) · 4.55 KB
/
config.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
package gen
import (
"fmt"
"path/filepath"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/utils/tests"
"gorm.io/gen/internal/model"
)
// GenerateMode generate mode
type GenerateMode uint
const (
// WithDefaultQuery create default query in generated code
WithDefaultQuery GenerateMode = 1 << iota
// WithoutContext generate code without context constrain
WithoutContext
// WithQueryInterface generate code with exported interface object
WithQueryInterface
)
// Config generator's basic configuration
type Config struct {
db *gorm.DB // db connection
OutPath string // query code path
OutFile string // query code file name, default: gen.go
ModelPkgPath string // generated model code's package name
WithUnitTest bool // generate unit test for query code
// generate model global configuration
FieldNullable bool // generate pointer when field is nullable
FieldCoverable bool // generate pointer when field has default value, to fix problem zero value cannot be assign: https://gorm.io/docs/create.html#Default-Values
FieldSignable bool // detect integer field's unsigned type, adjust generated data type
FieldWithIndexTag bool // generate with gorm index tag
FieldWithTypeTag bool // generate with gorm column type tag
Mode GenerateMode // generate mode
queryPkgName string // generated query code's package name
modelPkgPath string // model pkg path in target project
dbNameOpts []model.SchemaNameOpt
importPkgPaths []string
// name strategy for syncing table from db
tableNameNS func(tableName string) (targetTableName string)
modelNameNS func(tableName string) (modelName string)
fileNameNS func(tableName string) (fileName string)
dataTypeMap map[string]func(detailType string) (dataType string)
fieldJSONTagNS func(columnName string) (tagContent string)
fieldNewTagNS func(columnName string) (tagContent string)
modelOpts []ModelOpt
}
// WithOpts set global model options
func (cfg *Config) WithOpts(opts ...ModelOpt) {
if cfg.modelOpts == nil {
cfg.modelOpts = opts
} else {
cfg.modelOpts = append(cfg.modelOpts, opts...)
}
}
// WithDbNameOpts set get database name function
func (cfg *Config) WithDbNameOpts(opts ...model.SchemaNameOpt) {
if cfg.dbNameOpts == nil {
cfg.dbNameOpts = opts
} else {
cfg.dbNameOpts = append(cfg.dbNameOpts, opts...)
}
}
// WithTableNameStrategy specify table name naming strategy, only work when syncing table from db
func (cfg *Config) WithTableNameStrategy(ns func(tableName string) (targetTableName string)) {
cfg.tableNameNS = ns
}
// WithModelNameStrategy specify model struct name naming strategy, only work when syncing table from db
func (cfg *Config) WithModelNameStrategy(ns func(tableName string) (modelName string)) {
cfg.modelNameNS = ns
}
// WithFileNameStrategy specify file name naming strategy, only work when syncing table from db
func (cfg *Config) WithFileNameStrategy(ns func(tableName string) (fileName string)) {
cfg.fileNameNS = ns
}
// WithDataTypeMap specify data type mapping relationship, only work when syncing table from db
func (cfg *Config) WithDataTypeMap(newMap map[string]func(detailType string) (dataType string)) {
cfg.dataTypeMap = newMap
}
// WithJSONTagNameStrategy specify json tag naming strategy
func (cfg *Config) WithJSONTagNameStrategy(ns func(columnName string) (tagContent string)) {
cfg.fieldJSONTagNS = ns
}
// WithNewTagNameStrategy specify new tag naming strategy
func (cfg *Config) WithNewTagNameStrategy(ns func(columnName string) (tagContent string)) {
cfg.fieldNewTagNS = ns
}
// WithImportPkgPath specify import package path
func (cfg *Config) WithImportPkgPath(paths ...string) {
for i, path := range paths {
path = strings.TrimSpace(path)
if len(path) > 0 && path[0] != '"' && path[len(path)-1] != '"' { // without quote
path = `"` + path + `"`
}
paths[i] = path
}
cfg.importPkgPaths = append(cfg.importPkgPaths, paths...)
}
// Revise format path and db
func (cfg *Config) Revise() (err error) {
if strings.TrimSpace(cfg.ModelPkgPath) == "" {
cfg.ModelPkgPath = model.DefaultModelPkg
}
cfg.OutPath, err = filepath.Abs(cfg.OutPath)
if err != nil {
return fmt.Errorf("outpath is invalid: %w", err)
}
if cfg.OutPath == "" {
cfg.OutPath = "./query/"
}
if cfg.OutFile == "" {
cfg.OutFile = cfg.OutPath + "/gen.go"
} else if !strings.Contains(cfg.OutFile, "/") {
cfg.OutFile = cfg.OutPath + "/" + cfg.OutFile
}
cfg.queryPkgName = filepath.Base(cfg.OutPath)
if cfg.db == nil {
cfg.db, _ = gorm.Open(tests.DummyDialector{})
}
return nil
}
func (cfg *Config) judgeMode(mode GenerateMode) bool { return cfg.Mode&mode != 0 }