/
field.go
319 lines (262 loc) · 5.95 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
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
package remote
import (
"fmt"
"math"
"reflect"
cd "github.com/muidea/magicCommon/def"
"github.com/muidea/magicOrm/model"
)
type Field struct {
Name string `json:"name"`
Description string `json:"description"`
Type *TypeImpl `json:"type"`
Spec *SpecImpl `json:"spec"`
value *ValueImpl
}
type FieldValue struct {
Name string `json:"name"`
Value any `json:"value"`
}
func (s *Field) GetName() string {
return s.Name
}
func (s *Field) GetDescription() string {
return s.Description
}
func (s *Field) GetType() (ret model.Type) {
ret = s.Type
return
}
func (s *Field) GetSpec() (ret model.Spec) {
if s.Spec != nil {
ret = s.Spec
return
}
ret = &emptySpec
return
}
func (s *Field) GetValue() (ret model.Value) {
if s.value != nil {
ret = s.value
return
}
if s.Spec != nil && s.Spec.DefaultValue != nil {
return &ValueImpl{value: s.Spec.ValueDeclare}
}
ret = &NilValue
return
}
func (s *Field) SetValue(val model.Value) {
s.value = val.(*ValueImpl)
return
}
func (s *Field) IsPrimaryKey() bool {
if s.Spec == nil {
return false
}
return s.Spec.IsPrimaryKey()
}
func (s *Field) IsBasic() bool {
return s.Type.IsBasic()
}
func (s *Field) IsStruct() bool {
return s.Type.IsStruct()
}
func (s *Field) IsSlice() bool {
return s.Type.IsSlice()
}
func (s *Field) IsPtrType() bool {
return s.Type.IsPtrType()
}
func (s *Field) copy(reset bool) (ret *Field) {
val := &Field{
Name: s.Name,
Description: s.Description,
}
if s.Spec != nil {
val.Spec = s.Spec.copy()
}
if s.Type != nil {
val.Type = s.Type.copy()
}
if !reset && s.value != nil {
val.value = s.value.Copy()
}
if val.value == nil && val.Spec != nil && val.Spec.DefaultValue != nil {
val.value = &ValueImpl{value: val.Spec.DefaultValue}
}
ret = val
return
}
func (s *Field) verifyAutoIncrement(typeVal model.TypeDeclare) *cd.Result {
switch typeVal {
case model.TypeBooleanValue,
model.TypeStringValue,
model.TypeDateTimeValue,
model.TypeFloatValue,
model.TypeDoubleValue,
model.TypeStructValue,
model.TypeSliceValue:
return cd.NewError(cd.UnExpected, fmt.Sprintf("illegal auto_increment field type, type:%v", typeVal))
default:
}
return nil
}
func (s *Field) verifyUUID(typeVal model.TypeDeclare) *cd.Result {
if typeVal != model.TypeStringValue {
return cd.NewError(cd.UnExpected, fmt.Sprintf("illegal uuid field type, type:%v", typeVal))
}
return nil
}
func (s *Field) verifySnowFlake(typeVal model.TypeDeclare) *cd.Result {
if typeVal != model.TypeBigIntegerValue {
return cd.NewError(cd.UnExpected, fmt.Sprintf("illegal snowflake field type, type:%v", typeVal))
}
return nil
}
func (s *Field) verifyDateTime(typeVal model.TypeDeclare) *cd.Result {
if typeVal != model.TypeDateTimeValue {
return cd.NewError(cd.UnExpected, fmt.Sprintf("illegal dateTime field type, type:%v", typeVal))
}
return nil
}
func (s *Field) verifyPK(typeVal model.TypeDeclare) *cd.Result {
switch typeVal {
case model.TypeStructValue, model.TypeSliceValue:
return cd.NewError(cd.UnExpected, fmt.Sprintf("illegal primary key field type, type:%v", typeVal))
default:
}
return nil
}
func (s *Field) verify() (err *cd.Result) {
if s.Type == nil {
err = cd.NewError(cd.UnExpected, fmt.Sprintf("illegal filed, field type is null, name:%v", s.Name))
return
}
if s.Spec == nil {
return
}
val := s.Type.GetValue()
if s.Spec.GetValueDeclare() == model.AutoIncrement {
err = s.verifyAutoIncrement(val)
if err != nil {
return
}
}
if s.Spec.GetValueDeclare() == model.UUID {
err = s.verifyUUID(val)
if err != nil {
return
}
}
if s.Spec.GetValueDeclare() == model.SnowFlake {
err = s.verifySnowFlake(val)
if err != nil {
return
}
}
if s.Spec.GetValueDeclare() == model.DateTime {
err = s.verifyDateTime(val)
if err != nil {
return
}
}
if s.Spec.IsPrimaryKey() {
err = s.verifyPK(val)
}
return
}
func (s *Field) dump() string {
str := fmt.Sprintf("name:%s,type:[%s]", s.Name, s.Type.dump())
if s.Spec != nil {
str = fmt.Sprintf("%s,spec:[%s]", str, s.Spec.dump())
}
if s.value != nil {
str = fmt.Sprintf("%s,value:%v", str, s.value.Interface())
}
return str
}
func compareItem(l, r *Field) bool {
if l.Name != r.Name {
return false
}
if !compareType(l.Type, r.Type) {
return false
}
if !compareSpec(l.Spec, r.Spec) {
return false
}
return true
}
func (s *FieldValue) IsNil() bool {
return s.Value == nil
}
func (s *FieldValue) IsZero() bool {
if s.Value == nil {
return true
}
rVal := reflect.ValueOf(s.Value)
switch s.Value.(type) {
case bool:
return rVal.Bool() == false
case int8, int16, int32, int, int64:
return rVal.Int() == 0
case uint8, uint16, uint32, uint, uint64:
return rVal.Uint() == 0
case float32, float64:
return math.Float64bits(rVal.Float()) == 0
case string:
return rVal.String() == ""
case []bool,
[]int8, []int16, []int32, []int, []int64,
[]uint8, []uint16, []uint32, []uint, []uint64,
[]float32, []float64,
[]string,
[]any:
return rVal.Len() == 0
case *ObjectValue:
valuePtr, valueOK := s.Value.(*ObjectValue)
if valueOK {
if valuePtr != nil {
return !valuePtr.IsAssigned()
}
return true
}
err := fmt.Errorf("illegal value, val:%v", s.Value)
panic(err.Error())
case *SliceObjectValue:
valuePtr, valueOK := s.Value.(*SliceObjectValue)
if valueOK {
if valuePtr != nil {
return !valuePtr.IsAssigned()
}
return true
}
err := fmt.Errorf("illegal value, val:%v", s.Value)
panic(err.Error())
default:
err := fmt.Errorf("illegal value, val:%v", s.Value)
panic(err.Error())
}
return true
}
func (s *FieldValue) Set(val any) {
s.Value = val
}
func (s *FieldValue) Get() any {
return s.Value
}
func (s *FieldValue) GetName() string {
return s.Name
}
func (s *FieldValue) GetValue() model.Value {
return &ValueImpl{value: s.Value}
}
func (s *FieldValue) copy() (ret *FieldValue) {
if s.Value == nil {
ret = &FieldValue{}
return
}
ret = &FieldValue{Value: s.Value}
return
}