/
model.go
478 lines (416 loc) · 13.7 KB
/
model.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package norm
import (
"database/sql"
"errors"
"fmt"
"reflect"
"github.com/gocraft/dbr"
"github.com/picatic/norm/field"
"github.com/picatic/norm/validate"
"github.com/picatic/norm/atomiccache"
)
var (
modelFieldsCache atomiccache.Cache
fieldType = reflect.TypeOf((*field.Field)(nil)).Elem()
modelType = reflect.TypeOf((*Model)(nil)).Elem()
NameNotFoundErr = errors.New("Name not found")
)
// Model This interface provides basic information to help with building queries and behaviours in dbr.
type Model interface {
// TableName is the name used in the name used in database.
//
// Typically implemented to return in storage table name
//
// // TableName returns database table name
// func (u *User) TableName() string {
// return "norm_users"
// }
TableName() string
// PrimaryKey returns a PrimaryKeyer which can be one of a few provided implementations
//
// // PrimaryKey returns SinglePrimaryKey
// func (u *User) PrimaryKey() PrimaryKeyer {
// return norm.NewSinglePrimaryKey(field.Name("Id"))
// }
//
// // PrimaryKey returns Composite Key as MultiplePrimaryKey
// func (u *User) PrimaryKey() PrimaryKeyer {
// return norm.NewMultiplePrimaryKey(field.Names{"OrgId", "AccountId"})
// }
//
// // PrimaryKey returns CustomPrimaryKey example for generating a uuid
// func (t OauthAccessToken) PrimaryKey() norm.PrimaryKeyer {
// return norm.NewCustomPrimaryKey(Names{"Id"}, func(pk norm.PrimaryKeyer, model norm.Model) (Names, error) {
// f, _ := norm.ModelGetField(model, "Id")
// f.Scan(newUuid())
// return Names{"Id"}, nil
// })
// }
//
// Must not be called more than once and should only be done by norm.
PrimaryKey() PrimaryKeyer
// IsNew returns if the the model does not already exist in storage.
// The Primarykey on the model should not be Valid.
//
// Typically this can be implemented as
//
// // IsNew indicates that model instance does not already exist.
// func (u *User) IsNew() bool {
// return !u.Id.Valid
// }
IsNew() bool
}
// GetFieldByNamer provides a way to implement explicit GetFieldByName
type GetFieldByNamer interface {
GetFieldByName(field.Name) field.Field
}
// ModelFields Fetch list of fields on this model via reflection of fields that are from norm/field
// If model fails to be a Ptr to a Struct we return an error
func ModelFields(model Model) field.Names {
modelType := reflect.TypeOf(model)
return modelFieldsCache.Get(modelType, func() interface{} {
if modelType.Kind() != reflect.Ptr {
panic("Expected Model to be a Ptr")
}
if modelType.Elem().Kind() != reflect.Struct {
panic("Expected Model to be a Ptr to a Struct")
}
return modelFields(model)
}).(field.Names)
}
func modelFields(model interface{}) field.Names {
fields := make(field.Names, 0)
// Value of model
ifv := reflect.ValueOf(model)
if ifv.Kind() == reflect.Ptr {
ifv = ifv.Elem()
}
// Type of Model
itf := reflect.TypeOf(model)
if itf.Kind() == reflect.Ptr {
itf = itf.Elem()
}
for i := 0; i < itf.NumField(); i++ {
v := ifv.Field(i)
// Straight up struct field of type field.Field
if v.CanAddr() == true && v.Addr().Type().Implements(fieldType) == true {
fields = append(fields, field.Name(itf.Field(i).Name))
} else {
t := itf.Field(i)
// Embedded Struct with potential field.Field fields
if t.Anonymous == true && v.CanAddr() == true && v.Kind() == reflect.Struct {
fields = append(fields, modelFields(v.Addr().Interface())...)
} else if t.Anonymous == true && v.CanAddr() == true && v.Kind() == reflect.Interface { // Embedded Model interface
fields = append(fields, modelFields(v.Elem().Interface())...)
}
}
}
return fields
}
// ModelGetField Get a field on a model by name.
// This function will just returns field.Field or NameNotFoundErr error
func ModelGetField(model Model, fieldName field.Name) (field.Field, error) {
if getter, ok := model.(GetFieldByNamer); ok {
modelField := getter.GetFieldByName(fieldName)
if modelField == nil {
return nil, NameNotFoundErr
}
return modelField, nil
}
modelType := reflect.TypeOf(model)
if modelType.Kind() != reflect.Ptr {
panic("Expected Model to be a Ptr")
}
if modelType.Elem().Kind() != reflect.Struct {
panic("Expected Model to be a Ptr to Struct")
}
return modelGetField(model, fieldName)
}
func modelGetField(model interface{}, fieldName field.Name) (field.Field, error) {
modelType := reflect.TypeOf(model)
if modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
if _, ok := modelType.FieldByName(string(fieldName)); ok == true {
modelValue := reflect.ValueOf(model).Elem().FieldByName(string(fieldName)).Addr().Interface()
return modelValue.(field.Field), nil
} else {
modelValue := reflect.ValueOf(model)
if modelValue.Kind() == reflect.Ptr {
modelValue = modelValue.Elem()
}
for i := 0; i < modelType.NumField(); i++ {
t := modelType.Field(i)
v := modelValue.Field(i)
if t.Anonymous == true && v.CanAddr() == true && v.Kind() == reflect.Interface {
if m, ok := v.Elem().Interface().(Model); ok == true {
if f, err := modelGetField(m, fieldName); err == nil {
return f, nil
}
}
}
}
}
return nil, NameNotFoundErr
}
// ModelGetSetFields is named poorly but returns all the fields on a model that have been set.
// For a field to be set, it must of been successfully called with Scan at least once.
func ModelGetSetFields(model Model) (field.Names, error) {
fields := ModelFields(model)
var setFields field.Names
for _, field := range fields {
modelField, err := ModelGetField(model, field)
if err != nil {
return nil, err
}
if modelField.IsSet() {
setFields = append(setFields, field)
}
}
return setFields, nil
}
// ModelTableName get the complete table name including the database
func ModelTableName(s Session, m Model) string {
return fmt.Sprintf("%s.%s", s.Connection().Database(), m.TableName())
}
//
// TODO: Would be nice to have the Session reliant code in a sub package...maybe.
// This is kind of an ActiveRecord/RemoteProxy/RecordGateway pattern
//
// NewSelect builds a select from the Model and Fields
// Selects all fields if no fields provided
func NewSelect(s Session, m Model, fields field.Names) *dbr.SelectBuilder {
return s.Select(defaultFieldsEscaped(m, fields)...).From(ModelTableName(s, m))
}
// NewUpdate builds an update from the Model and Fields
func NewUpdate(s Session, m Model, fields field.Names) *dbr.UpdateBuilder {
if fields == nil {
fields = ModelFields(m)
}
fields = fields.Remove(m.PrimaryKey().Fields())
setMap := defaultUpdate(m, fields)
return s.Update(ModelTableName(s, m)).SetMap(setMap)
}
// NewInsert create an insert from the Model and Fields
func NewInsert(s Session, m Model, fields field.Names) *dbr.InsertBuilder {
if fields == nil {
fields = ModelFields(m)
}
pk := m.PrimaryKey()
fields = fields.Remove(pk.Fields())
// TODO do not eat this error
setFields, _ := pk.Generator(m)
fields = fields.Add(setFields)
return s.InsertInto(ModelTableName(s, m)).Columns(fields.SnakeCase()...)
}
// NewDelete creates a delete from the Model
func NewDelete(s Session, m Model) *dbr.DeleteBuilder {
return s.DeleteFrom(ModelTableName(s, m))
}
// ModelSave Save a model, calls appropriate Insert or Update based on Model.IsNew()
func ModelSave(dbrSess Session, model Model, fields field.Names) (sql.Result, error) {
if model.IsNew() == true {
return nil, errors.New("ModelSave for when IsNew Not Implement")
}
// TODO: handle composite primary keys
if len(model.PrimaryKey().Fields()) > 1 {
panic("ModelSave Composite Primary Keys not yet implemented")
}
primaryFieldName := model.PrimaryKey().Fields()[0]
idField, err := ModelGetField(model, primaryFieldName)
if err != nil {
return nil, err
}
id, err := idField.Value()
if err != nil {
return nil, err
}
return NewUpdate(dbrSess, model, fields).Where(fmt.Sprintf("`%s`=?", primaryFieldName.SnakeCase()), id).Exec()
}
// ModelLoadMap load a map into a model
func ModelLoadMap(model Model, data map[string]interface{}) error {
for k, v := range data {
modelField, err := ModelGetField(model, field.NewNameFromSnakeCase(k))
if err != nil {
continue
}
err = modelField.Scan(v)
if err != nil {
return err
}
}
return nil
}
// ModelSetDefaults load a map into a model to set default values
func ModelSetDefaults(model Model, defaults map[string]interface{}) error {
for k, v := range defaults {
modelField, err := ModelGetField(model, field.NewNameFromSnakeCase(k))
if err != nil {
continue
}
if !modelField.IsSet() {
err = modelField.Scan(v)
if err != nil {
return err
}
}
}
return nil
}
// ModelDirtyFields return Names of the fields that are dirty
func ModelDirtyFields(model Model) (field.Names, error) {
dirtyFields := make(field.Names, 0)
fields := ModelFields(model)
for _, fieldName := range fields {
mf, err := ModelGetField(model, fieldName)
if err != nil {
return nil, err
}
if mf.IsDirty() == true {
dirtyFields = append(dirtyFields, fieldName)
}
}
return dirtyFields, nil
}
// ModelValidate fields provided on model, if no fields validate all fields
func ModelValidate(sess Session, model Model, fields field.Names) error {
if validator, ok := model.(validate.ModelValidator); ok {
return validator.Validator().Fields(fields).Validate(model)
}
validators := sess.Connection().ValidatorCache()
if fields == nil {
fields = ModelFields(model)
}
if len(validators.Get(model)) == 0 {
if vm, ok := model.(ModelValidators); ok == true {
validators.Set(model, vm.Validators())
} else {
return nil
}
}
err := validators.Validate(sess, model, fields)
if err == nil { // a ptr to a struct cast into an interface is no longer really nil
return nil
}
return err
}
//MapFields makes a copy from a origin model to a destination model fields with the same name or
//provided by a mapping table
func MapFields(originModel Model, destinationModel Model, mappings map[field.Name]field.Name) {
originFieldNames := ModelFields(originModel)
for _, originFieldName := range originFieldNames {
var (
destinationFieldName field.Name
ok bool
)
if destinationFieldName, ok = mappings[originFieldName]; !ok {
destinationFieldName = originFieldName
}
originField, err := ModelGetField(originModel, originFieldName)
if err == NameNotFoundErr {
continue
}
destinationField, err := ModelGetField(destinationModel, destinationFieldName)
if err == NameNotFoundErr {
continue
}
destinationField.Scan(originField)
}
}
//MapSetFields makes a copy from a origin model to a destination model fields that are set with the same name or
//provided by a mapping table
func MapSetFields(originModel Model, destinationModel Model, mappings map[field.Name]field.Name) {
originFieldNames := ModelFields(originModel)
for _, originFieldName := range originFieldNames {
var (
destinationFieldName field.Name
ok bool
)
if destinationFieldName, ok = mappings[originFieldName]; !ok {
destinationFieldName = originFieldName
}
originField, err := ModelGetField(originModel, originFieldName)
if err == NameNotFoundErr {
continue
}
if originField.IsSet() {
destinationField, err := ModelGetField(destinationModel, destinationFieldName)
if err == NameNotFoundErr {
continue
}
destinationField.Scan(originField)
}
}
}
func getAllFields(model interface{}) map[field.Name]reflect.Value {
fields := map[field.Name]reflect.Value{}
// Value of model
modelValue := reflect.ValueOf(model)
if modelValue.Kind() == reflect.Ptr {
modelValue = modelValue.Elem()
}
// Type of Model
modelType := reflect.TypeOf(model)
if modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
for i := 0; i < modelType.NumField(); i++ {
v := modelValue.Field(i)
// Straight up struct field of type field.Field
if v.CanAddr() == true && v.Addr().Type().Implements(fieldType) == true {
fieldName := field.Name(modelType.Field(i).Name)
fields[fieldName] = modelValue.Field(i)
} else if v.Kind() == reflect.Struct {
//t := modelType.Field(i)
// Embedded Struct with potential field.Field fields
fieldName := field.Name(modelType.Field(i).Name)
fields[fieldName] = modelValue.Field(i)
}
}
return fields
}
//ModelToMap return a map with Model fields
func ModelToMap(model interface{}, fields field.Names, mapEmbedded bool) (map[string]interface{}, error) {
return modelToMap(model, fields, mapEmbedded)
}
func modelToMap(model interface{}, fields field.Names, mapEmbedded bool) (map[string]interface{}, error) {
resultMap := map[string]interface{}{}
allFields := getAllFields(model)
if fields == nil {
allFieldsKeys := reflect.ValueOf(allFields).MapKeys()
fields = make(field.Names, len(allFieldsKeys))
for i, key := range allFieldsKeys {
fields[i] = key.Interface().(field.Name)
}
}
for _, fieldName := range fields {
fieldValue, ok := allFields[fieldName]
if !ok {
return nil, fmt.Errorf("Field name '%s' not found", fieldName)
}
//if it's a norm fields return the value
if fieldValue.CanSet() == true && fieldValue.CanAddr() == true &&
fieldValue.Addr().Type().Implements(fieldType) == true {
field := fieldValue.Addr().Interface().(field.Field)
value, err := field.Value()
if err != nil {
return nil, err
}
resultMap[fieldName.SnakeCase()] = value
//if it's a embedded struct/model return the object map recursively
} else if fieldValue.CanSet() == true && fieldValue.Kind() == reflect.Struct && mapEmbedded {
var valueInterface interface{}
if fieldValue.Kind() == reflect.Ptr {
valueInterface = fieldValue.Interface()
} else {
valueInterface = fieldValue.Addr().Interface()
}
innerMap, err := modelToMap(valueInterface, nil, mapEmbedded)
if err != nil {
return nil, err
}
resultMap[fieldName.SnakeCase()] = innerMap
}
}
return resultMap, nil
}