This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
284 lines (236 loc) · 6.98 KB
/
field.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
package heidou
import (
"html/template"
"strconv"
"strings"
"github.com/jinzhu/inflection"
)
const (
OpEq = "Eq"
OpNeq = "Neq"
OpIn = "In"
OpNotIn = "NotIn"
OpGt = "Gt"
OpGte = "Gte"
OpLt = "Lt"
OpLte = "Lte"
OpIsNil = "IsNil"
OpNotNil = "NotNil"
OpContains = "Contains"
OpStartsWith = "StartsWith"
OpEndsWith = "EndsWith"
OpAnd = "AND"
OpOr = "OR"
OpNot = "NOT"
)
var (
// operations collection.
boolOps = []string{OpEq, OpNeq}
enumOps = append(boolOps, OpIn, OpNotIn)
numericOps = append(enumOps, OpGt, OpGte, OpLt, OpLte)
stringOps = append(numericOps, OpContains, OpStartsWith, OpEndsWith)
nillableOps = []string{OpIsNil, OpNotNil}
)
type JoinType string
const (
JoinTypeNone JoinType = "None"
JoinTypeBelongTo JoinType = "BelongTo"
JoinTypeHasOne JoinType = "HasOne"
JoinTypeHasMany JoinType = "HasMany"
JoinTypeManyToMany JoinType = "ManyToMany"
)
type Field struct {
//从配置文件读取的数据
Name string `mapstructure:"name" yaml:"name"`
Alias string `mapstructure:"alias" yaml:"alias"`
Description string `mapstructure:"description" yaml:"description"`
Tags string `mapstructure:"tags" yaml:"tags"`
//用于扩展字段定义
Annotations interface{} `mapstructure:"annotations" yaml:"annotations"`
IsSkip bool `mapstructure:"isSkip" yaml:"isSkip"`
IsRequired bool `mapstructure:"isRequired" yaml:"isRequired"`
IsSortable bool `mapstructure:"isSortable" yaml:"isSortable"`
IsFilterable bool `mapstructure:"isFilterable" yaml:"isFilterable"`
IsForeignKey bool `mapstructure:"isForeignKey" yaml:"isForeignKey"`
//非本服务定义的字段
IsRemote bool `mapstructure:"isRemote" yaml:"isRemote"`
JoinType JoinType `mapstructure:"joinType" yaml:"joinType"`
//Default: {field_name}
RefTableName string `mapstructure:"refTableName" yaml:"refTableName"`
//Default: {table}_{ref_table}
JoinTableName string `mapstructure:"joinTableName" yaml:"joinTableName"`
//Default: {ref_table}_id
ForeignKey string `mapstructure:"foreignKey" yaml:"foreignKey"`
//Default: id
References string `mapstructure:"references" yaml:"references"`
//Default: {table}_id
JoinForeignKey string `mapstructure:"joinForeignKey" yaml:"joinForeignKey"`
//Default: {ref_table}_id
JoinReferences string `mapstructure:"joinReferences" yaml:"joinReferences"`
Operations []string `mapstructure:"operations" yaml:"operations"`
//生成的数据
IsPrimaryKey bool
IsAutoIncrement bool
IsUnique bool
IsIndex bool
MetaType MetaType
TagsHTML template.HTML
//字段所在表
Table *Table
//引用表
RefTable *Table
//如果是M2M关联字段,字段对应的联接表
JoinTable *JoinTable
MaxLength int
NameSnake string
NameSnakePlural string
NameKebab string
NameKebabPlural string
NameCamel string
NameCamelPlural string
NameLowerCamel string
NameLowerCamelPlural string
}
func (f *Field) genName() {
name := f.Name
f.NameSnake = snake(name)
f.NameSnakePlural = inflection.Plural(f.NameSnake)
f.NameKebab = snake(name)
f.NameKebabPlural = inflection.Plural(f.NameKebab)
f.NameCamel = pascal(name)
f.NameCamelPlural = inflection.Plural(f.NameCamel)
f.NameLowerCamel = camel(name)
f.NameLowerCamelPlural = inflection.Plural(f.NameLowerCamel)
}
func findField(fields []*Field, name string) *Field {
for _, v := range fields {
if v.Name == name {
return v
}
}
return nil
}
// parseColumnType parse sql type and return raw type and length
func parseColumnLength(columnType string) (maxLength int) {
columnType = strings.ToLower(columnType)
maxLength = -1
idx1 := strings.Index(columnType, "(")
idx2 := strings.Index(columnType, ")")
if idx1 > -1 && idx2 > -1 {
sizeStr := columnType[idx1+1 : idx2]
i, err := strconv.Atoi(sizeStr)
if err == nil {
maxLength = i
}
}
return maxLength
}
func shiftMetaField(column *Column, metaTypes map[string]MetaType) *Field {
// isUnsigned := strings.Contains(column.Type, " unsigned") || strings.Contains(column.Type, " UNSIGNED")
columnType := strings.ToLower(column.Type)
// if isUnsigned {
// columnType = "u" + columnType
// }
// maxLength := parseColumnLength(column.Type)
//TODO
maxLength := 0
field := &Field{
Name: column.Name,
Description: column.Comment,
MetaType: metaTypes[columnType], //meta
MaxLength: maxLength,
JoinType: JoinTypeNone,
IsPrimaryKey: column.IsPrimaryKey,
IsAutoIncrement: column.IsAutoIncrement,
IsUnique: column.IsUnique,
IsIndex: column.IsIndex,
}
field.genName()
return field
}
func mergeField(field *Field, fieldInCfg *Field) *Field {
if fieldInCfg == nil {
return field
}
field.Alias = fieldInCfg.Alias
field.Description = fieldInCfg.Description
field.Tags = fieldInCfg.Tags
field.Annotations = fieldInCfg.Annotations
if fieldInCfg.JoinType == "" {
fieldInCfg.JoinType = JoinTypeNone
} else {
field.JoinType = fieldInCfg.JoinType
}
if fieldInCfg.IsSkip {
field.IsSkip = fieldInCfg.IsSkip
}
if fieldInCfg.IsPrimaryKey {
field.IsPrimaryKey = fieldInCfg.IsPrimaryKey
}
if fieldInCfg.IsRequired {
field.IsRequired = fieldInCfg.IsRequired
}
if fieldInCfg.IsSortable {
field.IsSortable = fieldInCfg.IsSortable
}
if fieldInCfg.IsFilterable {
field.IsFilterable = fieldInCfg.IsFilterable
}
field.IsForeignKey = fieldInCfg.IsForeignKey
field.IsRemote = fieldInCfg.IsRemote
field.Operations = fieldInCfg.Operations
field.ForeignKey = fieldInCfg.ForeignKey
field.References = fieldInCfg.References
field.JoinForeignKey = fieldInCfg.JoinForeignKey
field.JoinReferences = fieldInCfg.JoinReferences
return field
}
func (f *Field) HandleAssociation() {
if f.JoinType == JoinTypeNone {
return
}
if f.RefTableName == "" {
f.RefTableName = f.Name
}
var defaultForeignKey, defaultReferences, defaultJoinForeignKey, defaultJoinReferences string
switch f.JoinType {
case JoinTypeBelongTo:
defaultForeignKey = f.RefTableName + "_id"
defaultReferences = "id"
case JoinTypeHasOne:
defaultForeignKey = f.Table.Name + "_id"
defaultReferences = "id"
case JoinTypeHasMany:
defaultForeignKey = f.Table.Name + "_id"
defaultReferences = "id"
case JoinTypeManyToMany:
defaultForeignKey = "id"
defaultReferences = "id"
defaultJoinForeignKey = f.Table.Name + "_id"
defaultJoinReferences = f.RefTableName + "_id"
}
if f.ForeignKey == "" {
f.ForeignKey = defaultForeignKey
}
if f.References == "" {
f.References = defaultReferences
}
if f.JoinType == JoinTypeManyToMany {
if f.JoinTableName == "" {
f.JoinTableName = f.Table.Name + "_" + f.Name
}
if f.JoinForeignKey == "" {
f.JoinForeignKey = defaultJoinForeignKey
}
if f.JoinReferences == "" {
f.JoinReferences = defaultJoinReferences
}
}
}
func (f *Field) handleOperations() {
if f.IsIndex || f.IsFilterable {
if len(f.Operations) == 0 {
f.Operations = []string{OpEq}
}
}
}