forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
495 lines (462 loc) · 12.2 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
package dynamo
import (
"encoding"
"fmt"
"reflect"
"strconv"
"time"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
// Unmarshaler is the interface implemented by objects that can unmarshal
// an AttributeValue into themselves.
type Unmarshaler interface {
UnmarshalDynamo(av *dynamodb.AttributeValue) error
}
// Unmarshal decodes a DynamoDB item into out, which must be a pointer.
func UnmarshalItem(item map[string]*dynamodb.AttributeValue, out interface{}) error {
return unmarshalItem(item, out)
}
// Unmarshal decodes a DynamoDB value into out, which must be a pointer.
func Unmarshal(av *dynamodb.AttributeValue, out interface{}) error {
rv := reflect.ValueOf(out)
return unmarshalReflect(av, rv)
}
// used in iterators for unmarshaling one item
type unmarshalFunc func(map[string]*dynamodb.AttributeValue, interface{}) error
var nilTum encoding.TextUnmarshaler
var tumType = reflect.TypeOf(&nilTum).Elem()
// unmarshals one value
func unmarshalReflect(av *dynamodb.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 && av.N != nil {
// implicit unixtime
// TODO(guregu): have unixtime unmarshal explicitly check struct tags
ts, err := strconv.ParseInt(*av.N, 10, 64)
if err != nil {
return err
}
*x = time.Unix(ts, 0).UTC()
return nil
}
switch x := iface.(type) {
case *dynamodb.AttributeValue:
*x = *av
return nil
case Unmarshaler:
return x.UnmarshalDynamo(av)
case dynamodbattribute.Unmarshaler:
return x.UnmarshalDynamoDBAttributeValue(av)
case encoding.TextUnmarshaler:
if av.S != nil {
return x.UnmarshalText([]byte(*av.S))
}
}
}
if av.NULL != nil {
rv.Set(reflect.Zero(rv.Type()))
return nil
}
switch rv.Kind() {
case reflect.Ptr:
pt := reflect.New(rv.Type().Elem())
rv.Set(pt)
if av.NULL == nil || !(*av.NULL) {
return unmarshalReflect(av, rv.Elem())
}
return nil
case reflect.Bool:
if av.BOOL == nil {
return fmt.Errorf("dynamo: cannot unmarshal %s data into bool", avTypeName(av))
}
rv.SetBool(*av.BOOL)
return nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
if av.N == nil {
return fmt.Errorf("dynamo: cannot unmarshal %s data into int", avTypeName(av))
}
n, err := strconv.ParseInt(*av.N, 10, 64)
if err != nil {
return err
}
rv.SetInt(n)
return nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
if av.N == nil {
return fmt.Errorf("dynamo: cannot unmarshal %s data into uint", avTypeName(av))
}
n, err := strconv.ParseUint(*av.N, 10, 64)
if err != nil {
return err
}
rv.SetUint(n)
return nil
case reflect.Float64, reflect.Float32:
if av.N == nil {
return fmt.Errorf("dynamo: cannot unmarshal %s data into float", avTypeName(av))
}
n, err := strconv.ParseFloat(*av.N, 64)
if err != nil {
return err
}
rv.SetFloat(n)
return nil
case reflect.String:
if av.S == nil {
return fmt.Errorf("dynamo: cannot unmarshal %s data into string", avTypeName(av))
}
rv.SetString(*av.S)
return nil
case reflect.Struct:
if av.M == nil {
return fmt.Errorf("dynamo: cannot unmarshal %s data into struct", avTypeName(av))
}
if err := unmarshalItem(av.M, 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:
truthy = reflect.ValueOf(struct{}{})
default:
if av.M == nil {
return fmt.Errorf("dynamo: unmarshal map set: value type must be struct{} or bool, got %v", rv.Type())
}
}
switch {
case av.M != nil:
// TODO: this is probably slow
kp := reflect.New(rv.Type().Key())
kv := kp.Elem()
for k, v := range av.M {
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 av.SS != nil:
kp := reflect.New(rv.Type().Key())
kv := kp.Elem()
for _, s := range av.SS {
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 av.NS != nil:
kv := reflect.New(rv.Type().Key()).Elem()
for _, n := range av.NS {
if err := unmarshalReflect(&dynamodb.AttributeValue{N: n}, kv); err != nil {
return nil
}
rv.SetMapIndex(kv, truthy)
}
return nil
case av.BS != nil:
for _, bb := range av.BS {
kv := reflect.New(rv.Type().Key()).Elem()
for i, b := range bb {
kv.Index(i).Set(reflect.ValueOf(b))
}
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 {
case av.B != nil:
for i, b := range av.B {
arr.Index(i).Set(reflect.ValueOf(b))
}
rv.Set(arr)
return nil
case av.L != nil:
for i, innerAV := range av.L {
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 *dynamodb.AttributeValue, rv reflect.Value) error {
switch {
case av.B != nil:
rv.SetBytes(av.B)
return nil
case av.L != nil:
slicev := reflect.MakeSlice(rv.Type(), 0, len(av.L))
for _, innerAV := range av.L {
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 av.BS != nil:
slicev := reflect.MakeSlice(rv.Type(), 0, len(av.L))
for _, b := range av.BS {
innerRV := reflect.New(rv.Type().Elem()).Elem()
if err := unmarshalReflect(&dynamodb.AttributeValue{B: b}, innerRV); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV)
}
rv.Set(slicev)
return nil
case av.SS != nil:
slicev := reflect.MakeSlice(rv.Type(), 0, len(av.L))
for _, str := range av.SS {
innerRV := reflect.New(rv.Type().Elem()).Elem()
if err := unmarshalReflect(&dynamodb.AttributeValue{S: str}, innerRV); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV)
}
rv.Set(slicev)
return nil
case av.NS != nil:
slicev := reflect.MakeSlice(rv.Type(), 0, len(av.L))
for _, n := range av.NS {
innerRV := reflect.New(rv.Type().Elem()).Elem()
if err := unmarshalReflect(&dynamodb.AttributeValue{N: 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)
name, _, _ := fieldInfo(field)
if name == "-" {
// skip
continue
}
// embed anonymous structs
if fv.Type().Kind() == reflect.Struct && field.Anonymous {
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]*dynamodb.AttributeValue, out interface{}) error {
if out, ok := out.(*map[string]*dynamodb.AttributeValue); ok {
*out = item
return nil
}
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
rv.Elem().Set(reflect.Zero(rv.Type().Elem()))
fields := fieldsInStruct(rv.Elem())
for name, fv := range fields {
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]*dynamodb.AttributeValue, out interface{}) error {
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 *dynamodb.AttributeValue) (interface{}, error) {
switch {
case av.B != nil:
return av.B, nil
case av.BS != nil:
return av.BS, nil
case av.BOOL != nil:
return *av.BOOL, nil
case av.N != nil:
return strconv.ParseFloat(*av.N, 64)
case av.S != nil:
return *av.S, nil
case av.L != nil:
list := make([]interface{}, 0, len(av.L))
for _, item := range av.L {
iface, err := av2iface(item)
if err != nil {
return nil, err
}
list = append(list, iface)
}
return list, nil
case av.NS != nil:
set := make([]float64, 0, len(av.NS))
for _, n := range av.NS {
f, err := strconv.ParseFloat(*n, 64)
if err != nil {
return nil, err
}
set = append(set, f)
}
return set, nil
case av.SS != nil:
set := make([]string, 0, len(av.SS))
for _, s := range av.SS {
set = append(set, *s)
}
return set, nil
case av.M != nil:
m := make(map[string]interface{}, len(av.M))
for k, v := range av.M {
iface, err := av2iface(v)
if err != nil {
return nil, err
}
m[k] = iface
}
return m, nil
case av.NULL != nil:
return nil, nil
}
return nil, fmt.Errorf("dynamo: unsupported AV: %#v", *av)
}
func avTypeName(av *dynamodb.AttributeValue) string {
if av == nil {
return "<nil>"
}
switch {
case av.B != nil:
return "binary"
case av.BS != nil:
return "binary set"
case av.BOOL != nil:
return "boolean"
case av.N != nil:
return "number"
case av.S != nil:
return "string"
case av.L != nil:
return "list"
case av.NS != nil:
return "number set"
case av.SS != nil:
return "string set"
case av.M != nil:
return "map"
case av.NULL != nil:
return "null"
}
return "<empty>"
}