forked from ghetzel/pivot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
494 lines (415 loc) · 12.6 KB
/
record.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package dal
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
var FieldNestingSeparator string = `.`
type Record struct {
ID interface{} `json:"id"`
Fields map[string]interface{} `json:"fields,omitempty"`
Data []byte `json:"data,omitempty"`
Error error `json:"error,omitempty"`
CollectionName string `json:"collection,omitempty"`
Operation string `json:"operation,omitempty"`
Optional bool `json:"optional,omitempty"` // Specifies that the record is "optional", which is namely used in fixtures to indicate that a missing collection should not be considered fatal.
}
func NewRecord(id interface{}, data ...map[string]interface{}) *Record {
record := &Record{
ID: id,
}
if len(data) > 0 && len(data[0]) > 0 {
record.Fields = data[0]
} else {
record.Fields = make(map[string]interface{})
}
return record
}
func NewRecordErr(id interface{}, err error) *Record {
return &Record{
ID: id,
Fields: make(map[string]interface{}),
Error: err,
}
}
func (self *Record) init() {
if self.Fields == nil {
self.Fields = make(map[string]interface{})
}
}
func (self *Record) Keys(collection *Collection) []interface{} {
values := sliceutil.Sliceify(self.ID)
if collection != nil {
for _, field := range collection.Fields {
// if there are more keys than we currently have values...
if collection.KeyCount() > len(values) {
if field.Key {
if value := self.Get(field.Name); value != nil {
// append this key value
values = append(values, value)
}
}
} else {
break
}
}
}
return values
}
func (self *Record) SetKeys(collection *Collection, op FieldOperation, keys ...interface{}) error {
if collection != nil {
if len(keys) > 0 {
self.ID = collection.ConvertValue(collection.GetIdentityFieldName(), keys[0])
i := 1
for _, field := range collection.Fields {
// if there are more keys than we currently have values...
if i < len(keys) {
if field.Key {
if value, err := collection.ValueForField(field.Name, keys[i], op); err == nil {
self.Set(field.Name, value)
i += 1
} else {
return err
}
}
} else {
break
}
}
}
}
return nil
}
func (self *Record) Copy(other *Record, schema ...*Collection) error {
var collection *Collection
if len(schema) > 0 && schema[0] != nil {
collection = schema[0]
}
if other != nil {
self.Data = other.Data
if collection != nil {
collection.FillDefaults(self)
}
for key, value := range other.Fields {
if collection != nil {
if collectionField, ok := collection.GetField(key); ok {
// use the field's type in the collection schema to convert the value
if v, err := collectionField.ConvertValue(value); err == nil {
value = v
} else {
log.Warningf("error populating field %q: %v", key, err)
continue
}
// apply formatters to this value
if v, err := collectionField.Format(value, RetrieveOperation); err == nil {
value = v
} else {
log.Warningf("error formatting field %q: %v", key, err)
continue
}
// this specifies that we should double-check the validity of the values coming in
if collectionField.ValidateOnPopulate {
// validate the value
if err := collectionField.Validate(value); err != nil {
return fmt.Errorf("Cannot validate field %q: %v", collectionField.Name, err)
}
}
}
}
self.Set(key, value)
}
self.ID = other.ID
if collection != nil {
if idI, err := collection.formatAndValidateId(self.ID, RetrieveOperation, self); err == nil {
self.ID = idI
} else {
log.Warningf("error formatting ID: %v", err)
}
}
}
return nil
}
func (self *Record) Get(key string, fallback ...interface{}) interface{} {
self.init()
if key == DefaultIdentityField {
return self.ID
} else if v, ok := self.Fields[key]; ok {
return v
} else {
return self.GetNested(key, fallback...)
}
}
func (self *Record) GetNested(key string, fallback ...interface{}) interface{} {
self.init()
var fb interface{}
if len(fallback) > 0 {
fb = fallback[0]
}
return maputil.DeepGet(
self.Fields,
strings.Split(key, FieldNestingSeparator),
fb,
)
}
func (self *Record) GetString(key string, fallback ...string) string {
if v := self.Get(key); v == nil {
if len(fallback) > 0 {
return fallback[0]
} else {
return ``
}
} else {
return fmt.Sprintf("%v", v)
}
}
func (self *Record) Set(key string, value interface{}) *Record {
self.init()
self.Fields[key] = value
return self
}
func (self *Record) SetNested(key string, value interface{}) *Record {
self.init()
parts := strings.Split(key, FieldNestingSeparator)
maputil.DeepSet(self.Fields, parts, value)
return self
}
func (self *Record) SetFields(values map[string]interface{}) *Record {
for k, v := range values {
self.Set(k, v)
}
return self
}
func (self *Record) SetData(data []byte) *Record {
self.Data = data
return self
}
func (self *Record) String() string {
if data, err := json.Marshal(self); err == nil {
return string(data)
} else {
return fmt.Sprintf("Record<%v + %d fields>", self.ID, len(self.Fields))
}
}
func (self *Record) Append(key string, value ...interface{}) *Record {
return self.Set(key, self.appendValue(key, value...))
}
func (self *Record) AppendNested(key string, value ...interface{}) *Record {
return self.SetNested(key, self.appendValue(key, value...))
}
// Populates a given struct with with the values in this record.
func (self *Record) Populate(into interface{}, collection *Collection) error {
// special case for what is essentially copying another record into this one
if record, ok := into.(*Record); ok {
return self.Copy(record, collection)
} else {
if err := validatePtrToStructType(into); err != nil {
return fmt.Errorf("Cannot validate input variable: %v", err)
}
var idFieldName string
var fallbackIdFieldName string
// if a collection is specified, set the fallback identity field name to what the collection
// knows the ID field is called. This is used for input structs that don't explicitly tag
// a field with the ",identity" option
if collection != nil {
idFieldName = collection.GetIdentityFieldName()
}
// get the name of the identity field from the given struct
if _, key, err := getIdentityFieldNameFromStruct(into, fallbackIdFieldName); err == nil && key != `` {
idFieldName = key
} else if err != nil {
return fmt.Errorf("Cannot get identity field name: %v", err)
} else {
return fmt.Errorf("Could not determine identity field name")
}
if data, err := self.toMap(collection, idFieldName, into); err == nil {
return maputil.TaggedStructFromMap(data, into, RecordStructTag)
} else {
return fmt.Errorf("Cannot create map from collection: %v", err)
}
}
}
func (self *Record) OnlyFields(fields []string) *Record {
if len(fields) > 0 {
for field, _ := range self.Fields {
if !sliceutil.ContainsString(fields, field) {
delete(self.Fields, field)
}
}
}
return self
}
func (self *Record) Map(fields ...string) map[string]interface{} {
out := make(map[string]interface{})
if self.ID != nil {
out[`id`] = self.ID
}
if len(fields) > 0 {
for _, field := range sliceutil.IntersectStrings(fields, maputil.StringKeys(self.Fields)) {
out[field] = self.Fields[field]
}
} else {
for k, v := range self.Fields {
out[k] = v
}
}
return out
}
func (self *Record) toMap(collection *Collection, idFieldName string, into interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{})
// populate defaults
if collection != nil {
for _, field := range collection.Fields {
data[field.Name] = field.GetDefaultValue()
}
}
// get identity field name
if idFieldName == `` {
if collection != nil {
idFieldName = collection.GetIdentityFieldName()
} else {
idFieldName = DefaultIdentityField
}
}
// special form that interprets an record ID whose value is an array as a sequence of key values
// these values are unwrapped into the appropriate struct fields.
if typeutil.IsArray(self.ID) {
keyFields := collection.KeyFields()
keyValues := sliceutil.Sliceify(self.ID)
if len(keyFields) == len(keyValues) {
for i, keyField := range keyFields {
data[keyField.Name] = keyValues[i]
}
} else {
return nil, fmt.Errorf("Incorrect number of key values (%d) for composite key fields (%d)", len(keyValues), len(keyFields))
}
} else {
// set values
data[idFieldName] = self.ID
}
for k, v := range self.Fields {
// if the field we're setting already exists (i.e.: has a default value), that value
// isn't a zero value, but the incoming one IS a zero value, skip.
//
// this prevents clobbering specifically chosen defaults with type-specific zero values
//
if existing, ok := data[k]; ok {
if tm, ok := existing.(time.Time); ok && tm.IsZero() {
continue
} else if _, ok := existing.(bool); ok {
data[k] = v
continue
} else if !typeutil.IsZero(existing) && typeutil.IsZero(v) {
continue
}
}
data[k] = v
}
if collection != nil {
// format and validate values (including identity)
for key, value := range data {
if converted, err := self.convertRecordValueToStructValue(collection, key, value, into); err == nil {
value = converted
} else {
continue
}
if key == idFieldName {
if idI, err := collection.formatAndValidateId(value, RetrieveOperation, self); err == nil {
value = idI
} else {
return nil, err
}
} else if collectionField, ok := collection.GetField(key); ok {
// apply formatters to this value
if v, err := collectionField.Format(value, RetrieveOperation); err == nil {
value = v
} else {
log.Warningf("error formatting field %q: %v", key, err)
continue
}
// this specifies that we should double-check the validity of the values coming in
if collectionField.ValidateOnPopulate {
// validate the value
if err := collectionField.Validate(value); err != nil {
return nil, err
}
}
}
data[key] = value
}
// cull values that aren't fields in the collection
for k, _ := range data {
if k == idFieldName {
continue
} else if _, ok := collection.GetField(k); !ok {
delete(data, k)
}
}
}
return data, nil
}
func (self *Record) appendValue(key string, value ...interface{}) []interface{} {
newValue := make([]interface{}, 0)
if v := self.Get(key); v != nil {
valueV := reflect.ValueOf(v)
switch valueV.Type().Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < valueV.Len(); i++ {
newValue = append(newValue, valueV.Index(i).Interface())
}
default:
newValue = append(newValue, v)
}
}
return append(newValue, value...)
}
func (self *Record) convertRecordValueToStructValue(collection *Collection, key string, value interface{}, into interface{}) (interface{}, error) {
if collection != nil {
// 1. get field from collection
// 2. get relationship from field
// 3. get data type of output field
// 4. set directly if possible, else:
// 5. get related collection
// 6. instantate new instance of output field type
// 7. populate that instance with record.Populate(newInstance, relatedCollection)
//
if field, ok := collection.GetField(key); ok {
if intoField, err := getFieldForStruct(into, key); err == nil {
if constraint := field.BelongsToConstraint(); constraint != nil {
if related, err := collection.GetRelatedCollection(constraint.Collection); err == nil {
if intoField.FieldType.Kind() == reflect.Ptr && intoField.FieldType.Elem().Kind() == reflect.Struct {
instance := reflect.New(intoField.FieldType.Elem())
if instance.CanInterface() {
intoNew := instance.Interface()
return intoNew, maputil.TaggedStructFromMap(map[string]interface{}{
related.GetIdentityFieldName(): value,
}, intoNew, RecordStructTag)
} else {
return nil, fmt.Errorf("Cannot populate output field %q of type %v", key, intoField.FieldType)
}
}
} else {
return nil, err
}
}
} else {
return nil, err
}
}
}
return value, nil
}
func (self *Record) identityFieldDescription() *fieldDescription {
desc := new(fieldDescription)
val := reflect.ValueOf(self)
val = val.Elem()
desc.FieldType = val.Type()
desc.FieldValue = val.FieldByName(`ID`)
desc.OriginalName = `ID`
return desc
}