/
meta.go
430 lines (372 loc) · 13.7 KB
/
meta.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package resource
import (
"database/sql"
"fmt"
"reflect"
"runtime/debug"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
"github.com/qor/qor/utils"
"github.com/qor/roles"
"github.com/qor/validations"
)
// Metaor interface
type Metaor interface {
GetName() string
GetFieldName() string
GetSetter() func(resource interface{}, metaValue *MetaValue, context *qor.Context)
GetFormattedValuer() func(interface{}, *qor.Context) interface{}
GetValuer() func(interface{}, *qor.Context) interface{}
GetResource() Resourcer
GetMetas() []Metaor
SetPermission(*roles.Permission)
HasPermission(roles.PermissionMode, *qor.Context) bool
}
// ConfigureMetaBeforeInitializeInterface if a struct's field's type implemented this interface, it will be called when initializing a meta
type ConfigureMetaBeforeInitializeInterface interface {
ConfigureQorMetaBeforeInitialize(Metaor)
}
// ConfigureMetaInterface if a struct's field's type implemented this interface, it will be called after configed
type ConfigureMetaInterface interface {
ConfigureQorMeta(Metaor)
}
// MetaConfigInterface meta configuration interface
type MetaConfigInterface interface {
ConfigureMetaInterface
}
// MetaConfig base meta config struct
type MetaConfig struct {
}
// ConfigureQorMeta implement the MetaConfigInterface
func (MetaConfig) ConfigureQorMeta(Metaor) {
}
// Meta meta struct definition
type Meta struct {
Name string
FieldName string
FieldStruct *gorm.StructField
Setter func(resource interface{}, metaValue *MetaValue, context *qor.Context)
Valuer func(interface{}, *qor.Context) interface{}
FormattedValuer func(interface{}, *qor.Context) interface{}
Config MetaConfigInterface
BaseResource Resourcer
Resource Resourcer
Permission *roles.Permission
}
// GetBaseResource get base resource from meta
func (meta Meta) GetBaseResource() Resourcer {
return meta.BaseResource
}
// GetName get meta's name
func (meta Meta) GetName() string {
return meta.Name
}
// GetFieldName get meta's field name
func (meta Meta) GetFieldName() string {
return meta.FieldName
}
// SetFieldName set meta's field name
func (meta *Meta) SetFieldName(name string) {
meta.FieldName = name
}
// GetSetter get setter from meta
func (meta Meta) GetSetter() func(resource interface{}, metaValue *MetaValue, context *qor.Context) {
return meta.Setter
}
// SetSetter set setter to meta
func (meta *Meta) SetSetter(fc func(resource interface{}, metaValue *MetaValue, context *qor.Context)) {
meta.Setter = fc
}
// GetValuer get valuer from meta
func (meta Meta) GetValuer() func(interface{}, *qor.Context) interface{} {
return meta.Valuer
}
// SetValuer set valuer for meta
func (meta *Meta) SetValuer(fc func(interface{}, *qor.Context) interface{}) {
meta.Valuer = fc
}
// GetFormattedValuer get formatted valuer from meta
func (meta *Meta) GetFormattedValuer() func(interface{}, *qor.Context) interface{} {
if meta.FormattedValuer != nil {
return meta.FormattedValuer
}
return meta.Valuer
}
// SetFormattedValuer set formatted valuer for meta
func (meta *Meta) SetFormattedValuer(fc func(interface{}, *qor.Context) interface{}) {
meta.FormattedValuer = fc
}
// HasPermission check has permission or not
func (meta Meta) HasPermission(mode roles.PermissionMode, context *qor.Context) bool {
if meta.Permission == nil {
return true
}
var roles = []interface{}{}
for _, role := range context.Roles {
roles = append(roles, role)
}
return meta.Permission.HasPermission(mode, roles...)
}
// SetPermission set permission for meta
func (meta *Meta) SetPermission(permission *roles.Permission) {
meta.Permission = permission
}
// PreInitialize when will be run before initialize, used to fill some basic necessary information
func (meta *Meta) PreInitialize() error {
if meta.Name == "" {
utils.ExitWithMsg("Meta should have name: %v", reflect.TypeOf(meta))
} else if meta.FieldName == "" {
meta.FieldName = meta.Name
}
// parseNestedField used to handle case like Profile.Name
var parseNestedField = func(value reflect.Value, name string) (reflect.Value, string) {
fields := strings.Split(name, ".")
value = reflect.Indirect(value)
for _, field := range fields[:len(fields)-1] {
value = value.FieldByName(field)
}
return value, fields[len(fields)-1]
}
var getField = func(fields []*gorm.StructField, name string) *gorm.StructField {
for _, field := range fields {
if field.Name == name || field.DBName == name {
return field
}
}
return nil
}
var nestedField = strings.Contains(meta.FieldName, ".")
var scope = &gorm.Scope{Value: meta.BaseResource.GetResource().Value}
if nestedField {
subModel, name := parseNestedField(reflect.ValueOf(meta.BaseResource.GetResource().Value), meta.FieldName)
meta.FieldStruct = getField(scope.New(subModel.Interface()).GetStructFields(), name)
} else {
meta.FieldStruct = getField(scope.GetStructFields(), meta.FieldName)
}
return nil
}
// Initialize initialize meta, will set valuer, setter if haven't configure it
func (meta *Meta) Initialize() error {
// Set Valuer for Meta
if meta.Valuer == nil {
setupValuer(meta, meta.FieldName, meta.GetBaseResource().NewStruct())
}
if meta.Valuer == nil {
utils.ExitWithMsg("Meta %v is not supported for resource %v, no `Valuer` configured for it", meta.FieldName, reflect.TypeOf(meta.BaseResource.GetResource().Value))
}
// Set Setter for Meta
if meta.Setter == nil {
setupSetter(meta, meta.FieldName, meta.GetBaseResource().NewStruct())
}
return nil
}
func setupValuer(meta *Meta, fieldName string, record interface{}) {
nestedField := strings.Contains(fieldName, ".")
// Setup nested fields
if nestedField {
fieldNames := strings.Split(fieldName, ".")
setupValuer(meta, strings.Join(fieldNames[1:], "."), getNestedModel(record, strings.Join(fieldNames[0:2], "."), nil))
oldValuer := meta.Valuer
meta.Valuer = func(record interface{}, context *qor.Context) interface{} {
return oldValuer(getNestedModel(record, strings.Join(fieldNames[0:2], "."), context), context)
}
return
}
if meta.FieldStruct != nil {
meta.Valuer = func(value interface{}, context *qor.Context) interface{} {
scope := context.GetDB().NewScope(value)
if f, ok := scope.FieldByName(fieldName); ok {
if relationship := f.Relationship; relationship != nil && f.Field.CanAddr() && !scope.PrimaryKeyZero() {
if (relationship.Kind == "has_many" || relationship.Kind == "many_to_many") && f.Field.Len() == 0 {
context.GetDB().Model(value).Related(f.Field.Addr().Interface(), fieldName)
} else if (relationship.Kind == "has_one" || relationship.Kind == "belongs_to") && context.GetDB().NewScope(f.Field.Interface()).PrimaryKeyZero() {
if f.Field.Kind() == reflect.Ptr && f.Field.IsNil() {
f.Field.Set(reflect.New(f.Field.Type().Elem()))
}
context.GetDB().Model(value).Related(f.Field.Addr().Interface(), fieldName)
}
}
return f.Field.Interface()
}
return ""
}
}
}
func setupSetter(meta *Meta, fieldName string, record interface{}) {
nestedField := strings.Contains(fieldName, ".")
// Setup nested fields
if nestedField {
fieldNames := strings.Split(fieldName, ".")
setupSetter(meta, strings.Join(fieldNames[1:], "."), getNestedModel(record, strings.Join(fieldNames[0:2], "."), nil))
oldSetter := meta.Setter
meta.Setter = func(record interface{}, metaValue *MetaValue, context *qor.Context) {
oldSetter(getNestedModel(record, strings.Join(fieldNames[0:2], "."), context), metaValue, context)
}
return
}
commonSetter := func(setter func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{})) func(record interface{}, metaValue *MetaValue, context *qor.Context) {
return func(record interface{}, metaValue *MetaValue, context *qor.Context) {
if metaValue == nil {
return
}
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
context.AddError(validations.NewError(record, meta.Name, fmt.Sprintf("Failed to set Meta %v's value with %v, got %v", meta.Name, metaValue.Value, r)))
}
}()
field := utils.Indirect(reflect.ValueOf(record)).FieldByName(fieldName)
if field.Kind() == reflect.Ptr {
if field.IsNil() && utils.ToString(metaValue.Value) != "" {
field.Set(utils.NewValue(field.Type()).Elem())
}
if utils.ToString(metaValue.Value) == "" {
field.Set(reflect.Zero(field.Type()))
return
}
for field.Kind() == reflect.Ptr {
field = field.Elem()
}
}
if field.IsValid() && field.CanAddr() {
setter(field, metaValue, context, record)
}
}
}
// Setup belongs_to / many_to_many Setter
if meta.FieldStruct != nil {
if relationship := meta.FieldStruct.Relationship; relationship != nil {
if relationship.Kind == "belongs_to" || relationship.Kind == "many_to_many" {
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
var (
scope = context.GetDB().NewScope(record)
indirectValue = reflect.Indirect(reflect.ValueOf(record))
)
primaryKeys := utils.ToArray(metaValue.Value)
if metaValue.Value == nil {
primaryKeys = []string{}
}
// associations not changed for belongs to
if relationship.Kind == "belongs_to" && len(relationship.ForeignFieldNames) == 1 {
oldPrimaryKeys := utils.ToArray(indirectValue.FieldByName(relationship.ForeignFieldNames[0]).Interface())
// if not changed
if fmt.Sprint(primaryKeys) == fmt.Sprint(oldPrimaryKeys) {
return
}
// if removed
if len(primaryKeys) == 0 {
field := indirectValue.FieldByName(relationship.ForeignFieldNames[0])
field.Set(reflect.Zero(field.Type()))
}
}
// set current field value to blank
field.Set(reflect.Zero(field.Type()))
if len(primaryKeys) > 0 {
// replace it with new value
context.GetDB().Where(primaryKeys).Find(field.Addr().Interface())
}
// Replace many 2 many relations
if relationship.Kind == "many_to_many" {
if !scope.PrimaryKeyZero() {
context.GetDB().Model(record).Association(meta.FieldName).Replace(field.Interface())
field.Set(reflect.Zero(field.Type()))
}
}
})
return
}
}
}
field := reflect.Indirect(reflect.ValueOf(record)).FieldByName(fieldName)
for field.Kind() == reflect.Ptr {
if field.IsNil() {
field.Set(utils.NewValue(field.Type().Elem()))
}
field = field.Elem()
}
if !field.IsValid() {
return
}
switch field.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
field.SetInt(utils.ToInt(metaValue.Value))
})
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
field.SetUint(utils.ToUint(metaValue.Value))
})
case reflect.Float32, reflect.Float64:
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
field.SetFloat(utils.ToFloat(metaValue.Value))
})
case reflect.Bool:
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
if utils.ToString(metaValue.Value) == "true" {
field.SetBool(true)
} else {
field.SetBool(false)
}
})
default:
if _, ok := field.Addr().Interface().(sql.Scanner); ok {
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
if scanner, ok := field.Addr().Interface().(sql.Scanner); ok {
if metaValue.Value == nil && len(metaValue.MetaValues.Values) > 0 {
decodeMetaValuesToField(meta.Resource, field, metaValue, context)
return
}
if scanner.Scan(metaValue.Value) != nil {
if err := scanner.Scan(utils.ToString(metaValue.Value)); err != nil {
context.AddError(err)
return
}
}
}
})
} else if reflect.TypeOf("").ConvertibleTo(field.Type()) {
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
field.Set(reflect.ValueOf(utils.ToString(metaValue.Value)).Convert(field.Type()))
})
} else if reflect.TypeOf([]string{}).ConvertibleTo(field.Type()) {
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
field.Set(reflect.ValueOf(utils.ToArray(metaValue.Value)).Convert(field.Type()))
})
} else if _, ok := field.Addr().Interface().(*time.Time); ok {
meta.Setter = commonSetter(func(field reflect.Value, metaValue *MetaValue, context *qor.Context, record interface{}) {
if str := utils.ToString(metaValue.Value); str != "" {
if newTime, err := utils.ParseTime(str, context); err == nil {
field.Set(reflect.ValueOf(newTime))
}
} else {
field.Set(reflect.Zero(field.Type()))
}
})
}
}
}
func getNestedModel(value interface{}, fieldName string, context *qor.Context) interface{} {
model := reflect.Indirect(reflect.ValueOf(value))
fields := strings.Split(fieldName, ".")
for _, field := range fields[:len(fields)-1] {
if model.CanAddr() {
submodel := model.FieldByName(field)
if context != nil && context.GetDB() != nil && context.GetDB().NewRecord(submodel.Interface()) && !context.GetDB().NewRecord(model.Addr().Interface()) {
if submodel.CanAddr() {
context.GetDB().Model(model.Addr().Interface()).Association(field).Find(submodel.Addr().Interface())
model = submodel
} else {
break
}
} else {
model = submodel
}
}
}
if model.CanAddr() {
return model.Addr().Interface()
}
return nil
}