forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode.go
568 lines (527 loc) · 15.8 KB
/
decode.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package helixddb
import (
"encoding"
"fmt"
"reflect"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// Unmarshaler is the interface implemented by objects that can unmarshal
// an AttributeValue into themselves.
type Unmarshaler interface {
UnmarshalDynamo(av types.AttributeValue) error
}
// ItemUnmarshaler is the interface implemented by objects that can unmarshal
// an Item (a map of strings to AttributeValues) into themselves.
type ItemUnmarshaler interface {
UnmarshalDynamoItem(item map[string]types.AttributeValue) error
}
// Unmarshal decodes a DynamoDB item into out, which must be a pointer.
func UnmarshalItem(item map[string]types.AttributeValue, out interface{}) error {
return unmarshalItem(item, out)
}
// Unmarshal decodes a DynamoDB value into out, which must be a pointer.
func Unmarshal(av types.AttributeValue, out interface{}) error {
rv := reflect.ValueOf(out)
return unmarshalReflect(av, rv)
}
// used in iterators for unmarshaling one item
type unmarshalFunc func(map[string]types.AttributeValue, interface{}) error
var nilTum encoding.TextUnmarshaler
var tumType = reflect.TypeOf(&nilTum).Elem()
// unmarshals one value
func unmarshalReflect(av types.AttributeValue, rv reflect.Value) error {
// first try interface unmarshal stuff
if rv.CanInterface() {
var iface interface{}
if rv.CanAddr() {
iface = rv.Addr().Interface()
} else {
iface = rv.Interface()
}
if x, ok := iface.(*time.Time); ok {
if t, ok := av.(*types.AttributeValueMemberN); ok {
// implicit unixtime
// TODO(guregu): have unixtime unmarshal explicitly check struct tags
ts, err := strconv.ParseInt(t.Value, 10, 64)
if err != nil {
return err
}
*x = time.Unix(ts, 0).UTC()
return nil
}
}
switch x := iface.(type) {
case types.AttributeValue:
switch x.(type) {
case *types.AttributeValueMemberB:
res := av.(*types.AttributeValueMemberB)
*x.(*types.AttributeValueMemberB) = *res
return nil
case *types.AttributeValueMemberBOOL:
res := av.(*types.AttributeValueMemberBOOL)
*x.(*types.AttributeValueMemberBOOL) = *res
return nil
case *types.AttributeValueMemberBS:
res := av.(*types.AttributeValueMemberBS)
*x.(*types.AttributeValueMemberBS) = *res
return nil
case *types.AttributeValueMemberL:
res := av.(*types.AttributeValueMemberL)
*x.(*types.AttributeValueMemberL) = *res
return nil
case *types.AttributeValueMemberM:
res := av.(*types.AttributeValueMemberM)
*x.(*types.AttributeValueMemberM) = *res
return nil
case *types.AttributeValueMemberN:
res := av.(*types.AttributeValueMemberN)
*x.(*types.AttributeValueMemberN) = *res
return nil
case *types.AttributeValueMemberNS:
res := av.(*types.AttributeValueMemberNS)
*x.(*types.AttributeValueMemberNS) = *res
return nil
case *types.AttributeValueMemberNULL:
res := av.(*types.AttributeValueMemberNULL)
*x.(*types.AttributeValueMemberNULL) = *res
return nil
case *types.AttributeValueMemberS:
res := av.(*types.AttributeValueMemberS)
*x.(*types.AttributeValueMemberS) = *res
return nil
case *types.AttributeValueMemberSS:
res := av.(*types.AttributeValueMemberSS)
*x.(*types.AttributeValueMemberSS) = *res
return nil
}
case Unmarshaler:
return x.UnmarshalDynamo(av)
case attributevalue.Unmarshaler:
return x.UnmarshalDynamoDBAttributeValue(av)
case encoding.TextUnmarshaler:
if value, ok := av.(*types.AttributeValueMemberS); ok && value != nil {
return x.UnmarshalText([]byte(value.Value))
}
}
}
if !rv.CanSet() {
return nil
}
nullValue, valueIsNull := av.(*types.AttributeValueMemberNULL)
if valueIsNull {
rv.Set(reflect.Zero(rv.Type()))
return nil
}
switch rv.Kind() {
case reflect.Ptr:
pt := reflect.New(rv.Type().Elem())
rv.Set(pt)
if !valueIsNull || (valueIsNull && !nullValue.Value) {
return unmarshalReflect(av, rv.Elem())
}
return nil
case reflect.Bool:
boolValue, valueIsBool := av.(*types.AttributeValueMemberBOOL)
if !valueIsBool {
return fmt.Errorf("dynamo: cannot unmarshal %s data into bool", avTypeName(boolValue))
}
rv.SetBool(boolValue.Value)
return nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
nValue, valueIsN := av.(*types.AttributeValueMemberN)
if !valueIsN {
return fmt.Errorf("dynamo: cannot unmarshal %s data into int", avTypeName(av))
}
n, err := strconv.ParseInt(nValue.Value, 10, 64)
if err != nil {
return err
}
rv.SetInt(n)
return nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
nValue, valueIsN := av.(*types.AttributeValueMemberN)
if !valueIsN {
return fmt.Errorf("dynamo: cannot unmarshal %s data into uint", avTypeName(av))
}
n, err := strconv.ParseUint(nValue.Value, 10, 64)
if err != nil {
return err
}
rv.SetUint(n)
return nil
case reflect.Float64, reflect.Float32:
nValue, valueIsN := av.(*types.AttributeValueMemberN)
if !valueIsN {
return fmt.Errorf("dynamo: cannot unmarshal %s data into float", avTypeName(av))
}
n, err := strconv.ParseFloat(nValue.Value, 64)
if err != nil {
return err
}
rv.SetFloat(n)
return nil
case reflect.String:
sValue, valueIsS := av.(*types.AttributeValueMemberS)
if !valueIsS {
return fmt.Errorf("dynamo: cannot unmarshal %s data into string", avTypeName(av))
}
rv.SetString(sValue.Value)
return nil
case reflect.Struct:
mValue, valueIsM := av.(*types.AttributeValueMemberM)
if !valueIsM {
return fmt.Errorf("dynamo: cannot unmarshal %s data into struct", avTypeName(av))
}
if err := unmarshalItem(mValue.Value, rv.Addr().Interface()); err != nil {
return err
}
return nil
case reflect.Map:
if rv.IsNil() {
// TODO: maybe always remake this?
// I think the JSON library doesn't...
rv.Set(reflect.MakeMap(rv.Type()))
}
var truthy reflect.Value
switch {
case rv.Type().Elem().Kind() == reflect.Bool:
truthy = reflect.ValueOf(true)
case rv.Type().Elem() == emptyStructType:
fallthrough
case rv.Type().Elem().Kind() == reflect.Struct && rv.Type().Elem().NumField() == 0:
truthy = reflect.ValueOf(struct{}{})
default:
_, valueIsM := av.(*types.AttributeValueMemberM)
if !valueIsM {
return fmt.Errorf("dynamo: unmarshal map set: value type must be struct{} or bool, got %v", rv.Type())
}
}
switch item := av.(type) {
case *types.AttributeValueMemberM:
// TODO: this is probably slow
kp := reflect.New(rv.Type().Key())
kv := kp.Elem()
for k, v := range item.Value {
innerRV := reflect.New(rv.Type().Elem())
if err := unmarshalReflect(v, innerRV.Elem()); err != nil {
return err
}
if kp.Type().Implements(tumType) {
tm := kp.Interface().(encoding.TextUnmarshaler)
if err := tm.UnmarshalText([]byte(k)); err != nil {
return fmt.Errorf("dynamo: unmarshal map: key error: %v", err)
}
} else {
kv.SetString(k)
}
rv.SetMapIndex(kv, innerRV.Elem())
}
return nil
case *types.AttributeValueMemberSS:
kp := reflect.New(rv.Type().Key())
kv := kp.Elem()
for _, s := range item.Value {
if kp.Type().Implements(tumType) {
tm := kp.Interface().(encoding.TextUnmarshaler)
if err := tm.UnmarshalText([]byte(s)); err != nil {
return fmt.Errorf("dynamo: unmarshal map (SS): key error: %v", err)
}
} else {
kv.SetString(s)
}
rv.SetMapIndex(kv, truthy)
}
return nil
case *types.AttributeValueMemberNS:
kv := reflect.New(rv.Type().Key()).Elem()
for _, n := range item.Value {
if err := unmarshalReflect(&types.AttributeValueMemberN{Value: n}, kv); err != nil {
return err
}
rv.SetMapIndex(kv, truthy)
}
return nil
case *types.AttributeValueMemberBS:
for _, bb := range item.Value {
kv := reflect.New(rv.Type().Key()).Elem()
reflect.Copy(kv, reflect.ValueOf(bb))
rv.SetMapIndex(kv, truthy)
}
return nil
}
return fmt.Errorf("dynamo: cannot unmarshal %s data into map", avTypeName(av))
case reflect.Slice:
return unmarshalSlice(av, rv)
case reflect.Array:
arr := reflect.New(rv.Type()).Elem()
elemtype := arr.Type().Elem()
switch t := av.(type) {
case *types.AttributeValueMemberB:
if len(t.Value) > arr.Len() {
return fmt.Errorf("dynamo: cannot marshal %s into %s; too small (dst len: %d, src len: %d)", avTypeName(av), arr.Type().String(), arr.Len(), len(t.Value))
}
reflect.Copy(arr, reflect.ValueOf(t.Value))
rv.Set(arr)
return nil
case *types.AttributeValueMemberL:
if len(t.Value) > arr.Len() {
return fmt.Errorf("dynamo: cannot marshal %s into %s; too small (dst len: %d, src len: %d)", avTypeName(av), arr.Type().String(), arr.Len(), len(t.Value))
}
for i, innerAV := range t.Value {
innerRV := reflect.New(elemtype).Elem()
if err := unmarshalReflect(innerAV, innerRV); err != nil {
return err
}
arr.Index(i).Set(innerRV)
}
rv.Set(arr)
return nil
}
case reflect.Interface:
// interface{}
if rv.NumMethod() == 0 {
iface, err := av2iface(av)
if err != nil {
return err
}
if iface == nil {
rv.Set(reflect.Zero(rv.Type()))
} else {
rv.Set(reflect.ValueOf(iface))
}
return nil
}
}
iface := rv.Interface()
return fmt.Errorf("dynamo: cannot unmarshal to type: %T (%+v)", iface, iface)
}
// unmarshal for when rv's Kind is Slice
func unmarshalSlice(av types.AttributeValue, rv reflect.Value) error {
switch t := av.(type) {
case *types.AttributeValueMemberB:
rv.SetBytes(t.Value)
return nil
case *types.AttributeValueMemberL:
slicev := reflect.MakeSlice(rv.Type(), 0, len(t.Value))
for _, innerAV := range t.Value {
innerRV := reflect.New(rv.Type().Elem()).Elem()
if err := unmarshalReflect(innerAV, innerRV); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV)
}
rv.Set(slicev)
return nil
// there's probably a better way to do these
case *types.AttributeValueMemberBS:
slicev := reflect.MakeSlice(rv.Type(), 0, len(t.Value))
for _, b := range t.Value {
innerRV := reflect.New(rv.Type().Elem()).Elem()
if err := unmarshalReflect(&types.AttributeValueMemberB{Value: b}, innerRV); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV)
}
rv.Set(slicev)
return nil
case *types.AttributeValueMemberSS:
slicev := reflect.MakeSlice(rv.Type(), 0, len(t.Value))
for _, str := range t.Value {
innerRV := reflect.New(rv.Type().Elem()).Elem()
if err := unmarshalReflect(&types.AttributeValueMemberS{Value: str}, innerRV); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV)
}
rv.Set(slicev)
return nil
case *types.AttributeValueMemberNS:
slicev := reflect.MakeSlice(rv.Type(), 0, len(t.Value))
for _, n := range t.Value {
innerRV := reflect.New(rv.Type().Elem()).Elem()
if err := unmarshalReflect(&types.AttributeValueMemberN{Value: n}, innerRV); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV)
}
rv.Set(slicev)
return nil
}
return fmt.Errorf("dynamo: cannot unmarshal %s data into slice", avTypeName(av))
}
func fieldsInStruct(rv reflect.Value) map[string]reflect.Value {
if rv.Kind() == reflect.Ptr {
return fieldsInStruct(rv.Elem())
}
fields := make(map[string]reflect.Value)
for i := 0; i < rv.Type().NumField(); i++ {
field := rv.Type().Field(i)
fv := rv.Field(i)
isPtr := fv.Type().Kind() == reflect.Ptr
name, _ := fieldInfo(field)
if name == "-" {
// skip
continue
}
// embed anonymous structs, they could be pointers so test that too
if (fv.Type().Kind() == reflect.Struct || isPtr && fv.Type().Elem().Kind() == reflect.Struct) && field.Anonymous {
if isPtr {
if fv.CanSet() {
// set zero value for pointer
zero := reflect.New(fv.Type().Elem())
fv.Set(zero)
fv = zero
} else {
fv = reflect.Indirect(fv)
}
}
if !fv.IsValid() {
// inaccessible
continue
}
innerFields := fieldsInStruct(fv)
for k, v := range innerFields {
// don't clobber top-level fields
if _, exists := fields[k]; exists {
continue
}
fields[k] = v
}
continue
}
fields[name] = fv
}
return fields
}
// unmarshals a struct
func unmarshalItem(item map[string]types.AttributeValue, out interface{}) error {
switch x := out.(type) {
case *map[string]types.AttributeValue:
*x = item
return nil
case awsEncoder:
// special case for AWSEncoding
return attributevalue.UnmarshalMap(item, x.iface)
case ItemUnmarshaler:
return x.UnmarshalDynamoItem(item)
}
rv := reflect.ValueOf(out)
if rv.Kind() != reflect.Ptr {
return fmt.Errorf("dynamo: unmarshal: not a pointer: %T", out)
}
switch rv.Elem().Kind() {
case reflect.Ptr:
rv.Elem().Set(reflect.New(rv.Elem().Type().Elem()))
return unmarshalItem(item, rv.Elem().Interface())
case reflect.Struct:
var err error
fields := fieldsInStruct(rv.Elem())
for name, fv := range fields {
// we need to zero-out all fields to avoid weird data sticking around
// when iterating by unmarshaling to the same object over and over
if fv.CanSet() {
fv.Set(reflect.Zero(fv.Type()))
}
if av, ok := item[name]; ok {
if innerErr := unmarshalReflect(av, fv); innerErr != nil {
err = innerErr
}
}
}
return err
case reflect.Map:
mapv := rv.Elem()
if mapv.Type().Key().Kind() != reflect.String {
return fmt.Errorf("dynamo: unmarshal: map key must be a string: %T", mapv.Interface())
}
if mapv.IsNil() {
mapv.Set(reflect.MakeMap(mapv.Type()))
}
for k, av := range item {
innerRV := reflect.New(mapv.Type().Elem()).Elem()
if err := unmarshalReflect(av, innerRV); err != nil {
return err
}
mapv.SetMapIndex(reflect.ValueOf(k), innerRV)
}
return nil
}
return fmt.Errorf("dynamo: unmarshal: unsupported type: %T", out)
}
func unmarshalAppend(item map[string]types.AttributeValue, out interface{}) error {
if awsenc, ok := out.(awsEncoder); ok {
return unmarshalAppendAWS(item, awsenc.iface)
}
rv := reflect.ValueOf(out)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Slice {
return fmt.Errorf("dynamo: unmarshal append: result argument must be a slice pointer")
}
slicev := rv.Elem()
innerRV := reflect.New(slicev.Type().Elem())
if err := unmarshalItem(item, innerRV.Interface()); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV.Elem())
rv.Elem().Set(slicev)
return nil
}
// av2iface converts an av into interface{}.
func av2iface(av types.AttributeValue) (interface{}, error) {
switch v := av.(type) {
case *types.AttributeValueMemberB:
return v.Value, nil
case *types.AttributeValueMemberBS:
return v.Value, nil
case *types.AttributeValueMemberBOOL:
return v.Value, nil
case *types.AttributeValueMemberN:
return strconv.ParseFloat(v.Value, 64)
case *types.AttributeValueMemberS:
return v.Value, nil
case *types.AttributeValueMemberL:
list := make([]interface{}, 0, len(v.Value))
for _, item := range v.Value {
iface, err := av2iface(item)
if err != nil {
return nil, err
}
list = append(list, iface)
}
return list, nil
case *types.AttributeValueMemberNS:
set := make([]float64, 0, len(v.Value))
for _, n := range v.Value {
f, err := strconv.ParseFloat(n, 64)
if err != nil {
return nil, err
}
set = append(set, f)
}
return set, nil
case *types.AttributeValueMemberSS:
set := make([]string, 0, len(v.Value))
for _, s := range v.Value {
set = append(set, s)
}
return set, nil
case *types.AttributeValueMemberM:
m := make(map[string]interface{}, len(v.Value))
for k, v := range v.Value {
iface, err := av2iface(v)
if err != nil {
return nil, err
}
m[k] = iface
}
return m, nil
case *types.AttributeValueMemberNULL:
return nil, nil
}
return nil, fmt.Errorf("dynamo: unsupported AV: %#v", av)
}
func avTypeName(av types.AttributeValue) string {
return fmt.Sprintf("%T", av)
}