forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_options.go
196 lines (188 loc) · 5.44 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
package gen
import (
"reflect"
"regexp"
"strings"
"gorm.io/gen/field"
"gorm.io/gen/internal/check"
"gorm.io/gen/internal/model"
"gorm.io/gorm/schema"
)
var ns = schema.NamingStrategy{}
var (
// FieldNew add new field (any type your want)
FieldNew = func(fieldName, fieldType, fieldTag string) model.CreateMemberOpt {
return func(*model.Member) *model.Member {
return &model.Member{
Name: fieldName,
Type: fieldType,
OverwriteTag: fieldTag,
}
}
}
// FieldIgnore ignore some columns by name
FieldIgnore = func(columnNames ...string) model.FilterMemberOpt {
return func(m *model.Member) *model.Member {
for _, name := range columnNames {
if m.ColumnName == name {
return nil
}
}
return m
}
}
// FieldIgnoreReg ignore some columns by RegExp
FieldIgnoreReg = func(columnNameRegs ...string) model.FilterMemberOpt {
regs := make([]regexp.Regexp, len(columnNameRegs))
for i, reg := range columnNameRegs {
regs[i] = *regexp.MustCompile(reg)
}
return func(m *model.Member) *model.Member {
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.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
if m.ColumnName == columnName {
m.Name = newName
}
return m
}
}
// FieldType specify field type in generated struct
FieldType = func(columnName string, newType string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
if m.ColumnName == columnName {
m.Type = newType
}
return m
}
}
// FieldIgnoreType ignore some columns by RegExp
FieldTypeReg = func(columnNameReg string, newType string) model.ModifyMemberOpt {
reg := regexp.MustCompile(columnNameReg)
return func(m *model.Member) *model.Member {
if reg.MatchString(m.ColumnName) {
m.Type = newType
}
return m
}
}
// FieldTag specify GORM tag and JSON tag
FieldTag = func(columnName string, gormTag, jsonTag string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
if m.ColumnName == columnName {
m.GORMTag, m.JSONTag = gormTag, jsonTag
}
return m
}
}
// FieldJSONTag specify JSON tag
FieldJSONTag = func(columnName string, jsonTag string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
if m.ColumnName == columnName {
m.JSONTag = jsonTag
}
return m
}
}
// FieldGORMTag specify GORM tag
FieldGORMTag = func(columnName string, gormTag string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
if m.ColumnName == columnName {
m.GORMTag = gormTag
}
return m
}
}
// FieldNewTag add new tag
FieldNewTag = func(columnName string, newTag string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
if m.ColumnName == columnName {
m.NewTag += " " + newTag
}
return m
}
}
// FieldTrimPrefix trim column name's prefix
FieldTrimPrefix = func(prefix string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
m.Name = strings.TrimPrefix(m.Name, prefix)
return m
}
}
// FieldTrimSuffix trim column name's suffix
FieldTrimSuffix = func(suffix string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
m.Name = strings.TrimSuffix(m.Name, suffix)
return m
}
}
// FieldAddPrefix add prefix to struct's memeber name
FieldAddPrefix = func(prefix string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
m.Name = prefix + m.Name
return m
}
}
// FieldAddSuffix add suffix to struct's memeber name
FieldAddSuffix = func(suffix string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
m.Name += suffix
return m
}
}
// FieldRelate relate to table in database
FieldRelate = func(relationship field.RelationshipType, fieldName string, table *check.BaseStruct, config *field.RelateConfig) model.CreateMemberOpt {
if config == nil {
config = &field.RelateConfig{}
}
if config.JSONTag == "" {
config.JSONTag = ns.ColumnName("", fieldName)
}
return func(*model.Member) *model.Member {
return &model.Member{
Name: fieldName,
Type: config.RelateFieldPrefix(relationship) + table.StructInfo.Type,
JSONTag: config.JSONTag,
GORMTag: config.GORMTag,
NewTag: config.NewTag,
OverwriteTag: config.OverwriteTag,
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.CreateMemberOpt {
st := reflect.TypeOf(relModel)
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
fieldType := st.String()
if config == nil {
config = &field.RelateConfig{}
}
if config.JSONTag == "" {
config.JSONTag = ns.ColumnName("", fieldName)
}
return func(*model.Member) *model.Member {
return &model.Member{
Name: fieldName,
Type: config.RelateFieldPrefix(relationship) + fieldType,
JSONTag: config.JSONTag,
GORMTag: config.GORMTag,
NewTag: config.NewTag,
OverwriteTag: config.OverwriteTag,
Relation: field.NewRelationWithModel(relationship, fieldName, fieldType, relModel),
}
}
}
)