-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
field_options.go
322 lines (306 loc) · 9.13 KB
/
field_options.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
package gen
import (
"fmt"
"reflect"
"regexp"
"strings"
"gorm.io/gen/field"
"gorm.io/gen/internal/generate"
"gorm.io/gen/internal/model"
"gorm.io/gorm/schema"
)
// ModelOpt field option
type ModelOpt = model.Option
// Field exported model.Field
type Field = *model.Field
var ns = schema.NamingStrategy{}
var (
FieldModify = func(opt func(Field) Field) model.ModifyFieldOpt {
return func(f *model.Field) *model.Field {
return opt(f)
}
}
// WithDataTypesNullType configures the types of fields to use their datatypes nullable counterparts.
/**
*
* @param {boolean} all - If true, all basic types of fields will be replaced with their `datatypes.Null[T]` types.
* If false, only fields that are allowed to be null will be replaced with `datatypes.Null[T]` types.
*
* Examples:
*
* When `all` is true:
* - `int64` will be replaced with `datatypes.NullInt64`
* - `string` will be replaced with `datatypes.NullString`
*
* When `all` is false:
* - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `datatypes.Null[T]` types.
*
* Note:
* Ensure that proper error handling is implemented when converting
* fields to their `datatypes.Null[T]` types to avoid runtime issues.
*/
WithDataTypesNullType = func(all bool) model.ModifyFieldOpt {
return func(f *model.Field) *model.Field {
ft := f.Type
nullable := false
if strings.HasPrefix(ft, "*") {
nullable = true
ft = strings.TrimLeft(ft, "*")
}
if !all && !nullable {
return f
}
switch ft {
case "time.Time", "string", "int", "int8", "int16",
"int32", "int64", "uint", "uint8", "uint16", "uint32",
"uint64", "float64", "float32", "byte", "bool":
ft = fmt.Sprintf("datatypes.Null[%s]", ft)
default:
return f
}
f.CustomGenType = f.GenType()
f.Type = ft
return f
}
}
// FieldNew add new field (any type your want)
FieldNew = func(fieldName, fieldType string, fieldTag field.Tag) model.CreateFieldOpt {
return func(*model.Field) *model.Field {
return &model.Field{
Name: fieldName,
Type: fieldType,
Tag: fieldTag,
}
}
}
// FieldIgnore ignore some columns by name
FieldIgnore = func(columnNames ...string) model.FilterFieldOpt {
return func(m *model.Field) *model.Field {
for _, name := range columnNames {
if m.ColumnName == name {
return nil
}
}
return m
}
}
// FieldIgnoreReg ignore some columns by RegExp
FieldIgnoreReg = func(columnNameRegs ...string) model.FilterFieldOpt {
regs := make([]regexp.Regexp, len(columnNameRegs))
for i, reg := range columnNameRegs {
regs[i] = *regexp.MustCompile(reg)
}
return func(m *model.Field) *model.Field {
for _, reg := range regs {
if reg.MatchString(m.ColumnName) {
return nil
}
}
return m
}
}
// FieldRename specify field name in generated struct
FieldRename = func(columnName string, newName string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
m.Name = newName
}
return m
}
}
// FieldComment specify field comment in generated struct
FieldComment = func(columnName string, comment string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
m.ColumnComment = comment
m.MultilineComment = strings.Contains(comment, "\n")
}
return m
}
}
// FieldType specify field type in generated struct
FieldType = func(columnName string, newType string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
m.Type = newType
}
return m
}
}
// FieldTypeReg specify field type in generated struct by RegExp
FieldTypeReg = func(columnNameReg string, newType string) model.ModifyFieldOpt {
reg := regexp.MustCompile(columnNameReg)
return func(m *model.Field) *model.Field {
if reg.MatchString(m.ColumnName) {
m.Type = newType
}
return m
}
}
// FieldGenType specify field gen type in generated dao
FieldGenType = func(columnName string, newType string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
m.CustomGenType = newType
}
return m
}
}
// FieldGenTypeReg specify field gen type in generated dao by RegExp
FieldGenTypeReg = func(columnNameReg string, newType string) model.ModifyFieldOpt {
reg := regexp.MustCompile(columnNameReg)
return func(m *model.Field) *model.Field {
if reg.MatchString(m.ColumnName) {
m.CustomGenType = newType
}
return m
}
}
// FieldTag specify GORM tag and JSON tag
FieldTag = func(columnName string, tagFunc func(tag field.Tag) field.Tag) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
m.Tag = tagFunc(m.Tag)
}
return m
}
}
// FieldJSONTag specify JSON tag
FieldJSONTag = func(columnName string, jsonTag string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
m.Tag.Set(field.TagKeyJson, jsonTag)
}
return m
}
}
// FieldJSONTagWithNS specify JSON tag with name strategy
FieldJSONTagWithNS = func(schemaName func(columnName string) (tagContent string)) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if schemaName != nil {
m.Tag.Set(field.TagKeyJson, schemaName(m.ColumnName))
}
return m
}
}
// FieldGORMTag specify GORM tag
FieldGORMTag = func(columnName string, gormTag func(tag field.GormTag) field.GormTag) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
m.GORMTag = gormTag(m.GORMTag)
}
return m
}
}
// FieldGORMTagReg specify GORM tag by RegExp
FieldGORMTagReg = func(columnNameReg string, gormTag func(tag field.GormTag) field.GormTag) model.ModifyFieldOpt {
reg := regexp.MustCompile(columnNameReg)
return func(m *model.Field) *model.Field {
if reg.MatchString(m.ColumnName) {
m.GORMTag = gormTag(m.GORMTag)
}
return m
}
}
// FieldNewTag add new tag
FieldNewTag = func(columnName string, newTag field.Tag) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if m.ColumnName == columnName {
for k, v := range newTag {
m.Tag.Set(k, v)
}
}
return m
}
}
// FieldNewTagWithNS add new tag with name strategy
FieldNewTagWithNS = func(tagName string, schemaName func(columnName string) string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
if schemaName == nil {
schemaName = func(name string) string { return name }
}
m.Tag.Set(tagName, schemaName(m.ColumnName))
return m
}
}
// FieldTrimPrefix trim column name's prefix
FieldTrimPrefix = func(prefix string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
m.Name = strings.TrimPrefix(m.Name, prefix)
return m
}
}
// FieldTrimSuffix trim column name's suffix
FieldTrimSuffix = func(suffix string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
m.Name = strings.TrimSuffix(m.Name, suffix)
return m
}
}
// FieldAddPrefix add prefix to struct's memeber name
FieldAddPrefix = func(prefix string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
m.Name = prefix + m.Name
return m
}
}
// FieldAddSuffix add suffix to struct's memeber name
FieldAddSuffix = func(suffix string) model.ModifyFieldOpt {
return func(m *model.Field) *model.Field {
m.Name += suffix
return m
}
}
// FieldRelate relate to table in database
FieldRelate = func(relationship field.RelationshipType, fieldName string, table *generate.QueryStructMeta, config *field.RelateConfig) model.CreateFieldOpt {
if config == nil {
config = &field.RelateConfig{}
}
return func(*model.Field) *model.Field {
return &model.Field{
Name: fieldName,
Type: config.RelateFieldPrefix(relationship) + table.StructInfo.Type,
Tag: config.GetTag(fieldName),
GORMTag: config.GORMTag,
Relation: field.NewRelationWithType(
relationship, fieldName, table.StructInfo.Package+"."+table.StructInfo.Type,
table.Relations()...),
}
}
}
// FieldRelateModel relate to exist table model
FieldRelateModel = func(relationship field.RelationshipType, fieldName string, relModel interface{}, config *field.RelateConfig) model.CreateFieldOpt {
st := reflect.TypeOf(relModel)
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
fieldType := st.String()
if config == nil {
config = &field.RelateConfig{}
}
return func(*model.Field) *model.Field {
return &model.Field{
Name: fieldName,
Type: config.RelateFieldPrefix(relationship) + fieldType,
GORMTag: config.GORMTag,
Tag: config.GetTag(fieldName),
Relation: field.NewRelationWithModel(relationship, fieldName, fieldType, relModel),
}
}
}
// WithMethod add custom method for table model
WithMethod = func(methods ...interface{}) model.AddMethodOpt {
return func() []interface{} { return methods }
}
)
var (
DefaultMethodTableWithNamer = (&defaultModel{}).TableName
)
type defaultModel struct {
}
func (*defaultModel) TableName(namer schema.Namer) string {
if namer == nil {
return "@@table"
}
return namer.TableName("@@table")
}