-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
375 lines (311 loc) · 11.3 KB
/
entity.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package property
import (
"encoding/json"
"time"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pkg/errors"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_group"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_type"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_unit"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_view_type"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/sysname"
"gorm.io/datatypes"
"gorm.io/gorm"
)
const (
EntityName = "property"
TableName = "property"
ParseFormateDate = "2006-01-02"
ParseFormateDateRFC = time.RFC3339
ParseFormateTime = time.RFC3339
SortOrderDefault = 9999
)
// Property ...
type Property struct {
ID uint `gorm:"type:bigserial;primaryKey" json:"id"`
Sysname string `gorm:"type:varchar(100) not null;unique;index" json:"sysname"`
NameSourceID *uint `gorm:"type:bigint null REFERENCES \"text_source\"(id);index" json:"-"`
DescriptionSourceID *uint `gorm:"type:bigint null REFERENCES \"text_source\"(id);index" json:"-"`
Name *string `gorm:"-" json:"name"`
Description *string `gorm:"-" json:"description"`
PropertyTypeID uint `gorm:"type:bigint not null REFERENCES \"property_type\"(id);index" json:"propertyTypeId"`
PropertyUnitID *uint `gorm:"type:bigint null default null REFERENCES \"property_unit\"(id);index" json:"propertyUnitId"`
PropertyViewTypeID *uint `gorm:"type:bigint null default null REFERENCES \"property_view_type\"(id);index" json:"propertyViewTypeId"`
PropertyGroupID *uint `gorm:"type:bigint null default null REFERENCES \"property_group\"(id);index" json:"propertyGroupId"`
IsSpecific bool `gorm:"type:boolean not null default false;" json:"isSpecific"`
IsRange bool `gorm:"type:boolean not null default false;" json:"isRange"`
IsMultiple bool `gorm:"type:boolean not null default false;" json:"isMultiple"`
SortOrder uint `gorm:"type:smallint;not null;default:9999" json:"sortOrder"`
OptionsB datatypes.JSON `json:"-"`
Options []map[string]interface{} `gorm:"-" json:"options"`
PropertyType *property_type.PropertyType `json:"propertyType,omitempty"`
PropertyViewType *property_view_type.PropertyViewType `json:"propertyViewType,omitempty"`
PropertyUnit *property_unit.PropertyUnit `json:"propertyUnit,omitempty"`
PropertyGroup *property_group.PropertyGroup `json:"propertyGroup,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt,omitempty"`
}
func (e *Property) TableName() string {
return TableName
}
// New func is a constructor for the Property
func New() *Property {
return &Property{}
}
func (e Property) Validate() error {
return validation.ValidateStruct(&e,
validation.Field(&e.Sysname, sysname.ValidationRules...),
validation.Field(&e.PropertyTypeID, validation.In(property_type.ValidTypesIDs...)),
validation.Field(&e.IsRange, validation.When(e.IsMultiple, validation.Empty)),
validation.Field(&e.IsMultiple, validation.When(e.IsRange, validation.Empty)),
//validation.Field(&e.PropertyUnitID, validation.When(e.PropertyTypeID != property_type.IDRelation, validation.Required)),
validation.Field(&e.Options, validation.By(e.optionsValidate)),
)
}
func (e Property) optionsValidate(value interface{}) (err error) {
v, ok := value.([]map[string]interface{})
if !ok {
return errors.New("Property.Options must be []map[string]interface{} type")
}
LOOP:
for _, item := range v {
for _, itemVal := range item {
switch e.PropertyTypeID {
case property_type.IDBoolean:
if _, err := GetValueBool(itemVal); err != nil {
err = errors.Errorf("value type must be a boolean, value: %#v", itemVal)
break LOOP
}
case property_type.IDInt:
if _, err := GetValueInt(itemVal); err != nil {
err = errors.Errorf("value type must be an int, value: %#v", itemVal)
break LOOP
}
case property_type.IDFloat:
if _, err := GetValueFloat(itemVal); err != nil {
if _, ok := itemVal.(int); !ok {
err = errors.Errorf("value type must be a float, value: %#v", itemVal)
break LOOP
}
}
case property_type.IDDate:
if _, err := GetValueDate(itemVal); err != nil {
if _, ok := itemVal.(int); !ok {
err = errors.Errorf("value type must be a date, value: %#v", itemVal)
break LOOP
}
}
case property_type.IDTime:
if _, err := GetValueTime(itemVal); err != nil {
err = errors.Errorf("value type must be a timestamp, value: %#v", itemVal)
}
case property_type.IDText:
if _, err := GetValueText(itemVal); err != nil {
err = errors.Errorf("value type must be a text, value: %#v", itemVal)
break LOOP
}
case property_type.IDUText:
if _, err := GetValueText(itemVal); err != nil {
err = errors.Errorf("value type must be a text, value: %#v", itemVal)
break LOOP
}
}
}
}
return err
}
func GetValueBool(value interface{}) (bool, error) {
res, ok := value.(bool)
if !ok {
return res, errors.Errorf("can not cast to a boolean value, value: %#v", value)
}
return res, nil
}
func GetValueInt(value interface{}) (int, error) {
var res int
valInt, okInt := value.(int)
valUint, okUint := value.(uint)
valFloat, okFloat := value.(float64) // после анмаршаллинга из JSON тип float64
if okInt {
res = valInt
} else if okUint {
if uint(int(valUint)) != valUint {
return res, errors.Errorf("Can not cast value of PropertyValue to int from uint. Value = %v.", value)
}
res = int(valUint)
} else if okFloat {
if float64(int(valFloat)) != valFloat {
return res, errors.Errorf("Can not cast value of PropertyValue to int from float64. Value = %v.", value)
}
res = int(valFloat)
} else {
return res, errors.Errorf("Can not cast value of PropertyValue to int. Value = %v.", value)
}
return res, nil
}
func GetRelationItemValue(value interface{}) (uint, error) {
var res uint
valInt, okInt := value.(int)
valUint, okUint := value.(uint)
valFloat, okFloat := value.(float64) // на всякий..
switch {
case okUint:
res = valUint
case okInt:
if int(uint(valInt)) != valInt {
return res, errors.Errorf("Can not cast value of Relation to uint from int. Value = %v.", value)
}
res = uint(valInt)
case okFloat:
if float64(uint(valFloat)) != valFloat {
return res, errors.Errorf("Can not cast value of Relation to uint from float64. Value = %v.", value)
}
res = uint(valInt)
default:
return res, errors.Errorf("Can not cast value of Relation to uint. Value = %v.", value)
}
return res, nil
}
func GetRelationValue(value interface{}) ([]uint, error) {
var res []uint
valInt, okInt := value.([]int)
valUint, okUint := value.([]uint)
valFloat, okFloat := value.([]float64) // на всякий..
valInterface, okInterface := value.([]interface{}) // после анмаршаллинга из JSON тип []interface{}
switch {
case okUint:
res = valUint
case okInt:
res = make([]uint, 0, len(valInt))
for _, i := range valInt {
if int(uint(i)) != i {
return res, errors.Errorf("Can not cast value of Relation to []uint from []int. Value = %v.", value)
}
res = append(res, uint(i))
}
case okFloat:
res = make([]uint, 0, len(valInt))
for _, i := range valFloat {
if float64(uint(i)) != i {
return res, errors.Errorf("Can not cast value of Relation to []uint from []float64. Value = %v.", value)
}
res = append(res, uint(i))
}
case okInterface:
res = make([]uint, 0, len(valInt))
for _, val := range valInterface {
valInt, err := GetValueInt(val)
if err != nil {
return nil, err
}
res = append(res, uint(valInt))
}
default:
return res, errors.Errorf("Can not cast value of Relation to []uint. Value = %v.", value)
}
return res, nil
}
func GetValueFloat(value interface{}) (float64, error) {
res, ok := value.(float64)
if !ok {
resInt, ok := value.(int)
if !ok {
return res, errors.Errorf("Can not cast value of PropertyValue to float. Value = %v.", value)
}
res = float64(resInt)
}
return res, nil
}
func GetValueDate(value interface{}) (time.Time, error) {
var res time.Time
var t time.Time
var err error
t, okTime := value.(time.Time)
vStr, okStr := value.(string)
vInt64, okInt64 := value.(int64)
vInt, okInt := value.(int)
if okTime {
} else if okStr {
if t, err = time.Parse(ParseFormateDateRFC, vStr); err != nil {
if t, err = time.Parse(ParseFormateDate, vStr); err != nil {
return res, errors.Wrapf(err, "Can not pars string value by ParseFormateDate to date. Value = %v.", value)
}
}
} else if okInt || okInt64 {
if okInt {
vInt64 = int64(vInt)
}
t = time.Unix(vInt64, 0)
} else {
return res, errors.Errorf("Can not cast value of PropertyValue to string (as a string format of a date) or int (as a timestamp). Value = %v.", value)
}
y, m, d := t.Date()
res = time.Date(y, m, d, 0, 0, 0, 0, t.Location())
return res, nil
}
func GetValueTime(value interface{}) (time.Time, error) {
var t time.Time
var err error
t, okTime := value.(time.Time)
vStr, okStr := value.(string)
vInt64, okInt64 := value.(int64)
vInt, okInt := value.(int)
if okTime {
} else if okStr {
t, err = time.Parse(ParseFormateTime, vStr)
if err != nil {
return t, errors.Wrapf(err, "Can not pars string value by ParseFormateDate to date. Value = %v.", value)
}
} else if okInt || okInt64 {
if okInt {
vInt64 = int64(vInt)
}
t = time.Unix(vInt64, 0)
} else {
return t, errors.Errorf("Can not cast value of PropertyValue to string (as a string format of a date) or int (as a timestamp). Value = %v.", value)
}
return t, nil
}
func GetValueText(value interface{}) (string, error) {
res, ok := value.(string)
if !ok {
return res, errors.Errorf("can not cast to a string value, value: %#v", value)
}
return res, nil
}
func (e *Property) ClearEmptyAssociations() {
if e.PropertyType != nil && e.PropertyType.ID == 0 {
e.PropertyType = nil
}
if e.PropertyViewType != nil && e.PropertyViewType.ID == 0 {
e.PropertyViewType = nil
}
if e.PropertyUnit != nil && e.PropertyUnit.ID == 0 {
e.PropertyUnit = nil
}
if e.PropertyGroup != nil && e.PropertyGroup.ID == 0 {
e.PropertyGroup = nil
}
}
func (e *Property) AfterFind() error {
e.ClearEmptyAssociations()
return e.optionsB2Options()
}
func (e *Property) BeforeSave() error {
return e.options2OptionsB()
}
func (e *Property) optionsB2Options() error {
jsonb, err := e.OptionsB.MarshalJSON()
if err != nil {
return err
}
return json.Unmarshal(jsonb, &e.Options)
}
func (e *Property) options2OptionsB() error {
jsonb, err := json.Marshal(&e.Options)
if err != nil {
return err
}
return e.OptionsB.UnmarshalJSON(jsonb)
}