forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencode.go
695 lines (648 loc) · 17.9 KB
/
encode.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
package helixddb
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// Marshaler is the interface implemented by objects that can marshal themselves into
// an AttributeValue.
type Marshaler interface {
MarshalDynamo() (types.AttributeValue, error)
}
// ItemMarshaler is the interface implemented by objects that can marshal themselves
// into an Item (a map of strings to AttributeValues).
type ItemMarshaler interface {
MarshalDynamoItem() (map[string]types.AttributeValue, error)
}
// MarshalItem converts the given struct into a DynamoDB item.
func MarshalItem(v interface{}) (map[string]types.AttributeValue, error) {
return marshalItem(v)
}
func marshalItem(v interface{}) (map[string]types.AttributeValue, error) {
switch x := v.(type) {
case map[string]types.AttributeValue:
return x, nil
case awsEncoder:
// special case for AWSEncoding
return attributevalue.MarshalMap(x.iface)
case ItemMarshaler:
return x.MarshalDynamoItem()
}
rv := reflect.ValueOf(v)
switch rv.Type().Kind() {
case reflect.Ptr:
return marshalItem(rv.Elem().Interface())
case reflect.Struct:
return marshalStruct(rv)
case reflect.Map:
return marshalItemMap(rv.Interface())
}
return nil, fmt.Errorf("dynamo: marshal item: unsupported type %T: %v", rv.Interface(), rv.Interface())
}
func marshalItemMap(v interface{}) (map[string]types.AttributeValue, error) {
// TODO: maybe unify this with the map stuff in marshal
av, err := marshal(v, flagNone)
if err != nil {
return nil, err
}
m, _ := av.(*types.AttributeValueMemberM)
if m == nil {
return nil, fmt.Errorf("dynamo: internal error: encoding map but M was empty")
}
return m.Value, nil
}
func marshalStruct(rv reflect.Value) (map[string]types.AttributeValue, error) {
item := make(map[string]types.AttributeValue)
var err error
for i := 0; i < rv.Type().NumField(); i++ {
field := rv.Type().Field(i)
fv := rv.Field(i)
name, flags := fieldInfo(field)
omitempty := flags&flagOmitEmpty != 0
anonStruct := fv.Type().Kind() == reflect.Struct && field.Anonymous
pointerAnonStruct := fv.Type().Kind() == reflect.Ptr && fv.Type().Elem().Kind() == reflect.Struct && field.Anonymous
switch {
case !fv.CanInterface():
// skip unexported unembedded fields
if !anonStruct && !pointerAnonStruct {
continue
}
case name == "-":
continue
case omitempty:
if isZero(fv) {
continue
}
}
// embed anonymous structs
if anonStruct || pointerAnonStruct {
if pointerAnonStruct {
if fv.IsNil() {
continue
}
fv = fv.Elem()
}
avs, err := marshalStruct(fv)
if err != nil {
return nil, err
}
for k, v := range avs {
// don't clobber pre-existing fields
if _, exists := item[k]; exists {
continue
}
item[k] = v
}
continue
}
av, err := marshal(fv.Interface(), flags)
if err != nil {
return nil, err
}
if av != nil {
item[name] = av
}
}
return item, err
}
// Marshal converts the given value into a DynamoDB attribute value.
func Marshal(v interface{}) (types.AttributeValue, error) {
return marshal(v, flagNone)
}
func marshal(v interface{}, flags encodeFlags) (types.AttributeValue, error) {
// encoders with precedence over interfaces
if flags&flagUnixTime != 0 {
switch x := v.(type) {
case *time.Time:
if x != nil {
return marshal(*x, flags)
}
case time.Time:
if x.IsZero() {
// omitempty behaviour
return nil, nil
}
ts := strconv.FormatInt(x.Unix(), 10)
return &types.AttributeValueMemberN{Value: ts}, nil
}
}
rv := reflect.ValueOf(v)
switch x := v.(type) {
case types.AttributeValue:
return x, nil
case Marshaler:
if rv.Kind() == reflect.Ptr && rv.IsNil() {
if _, ok := rv.Type().Elem().MethodByName("MarshalDynamo"); ok {
// MarshalDynamo is defined on value type, but this is a nil ptr
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
}
return x.MarshalDynamo()
case attributevalue.Marshaler:
if rv.Kind() == reflect.Ptr && rv.IsNil() {
if _, ok := rv.Type().Elem().MethodByName("MarshalDynamoDBAttributeValue"); ok {
// MarshalDynamoDBAttributeValue is defined on value type, but this is a nil ptr
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
}
return x.MarshalDynamoDBAttributeValue()
case encoding.TextMarshaler:
if rv.Kind() == reflect.Ptr && rv.IsNil() {
if _, ok := rv.Type().Elem().MethodByName("MarshalText"); ok {
// MarshalText is defined on value type, but this is a nil ptr
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
}
text, err := x.MarshalText()
if err != nil {
return nil, err
}
if len(text) == 0 {
if flags&flagAllowEmpty != 0 {
return &types.AttributeValueMemberS{Value: ""}, nil
}
return nil, nil
}
return &types.AttributeValueMemberS{Value: string(text)}, err
case nil:
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
return marshalReflect(rv, flags)
}
var nilTm encoding.TextMarshaler
var tmType = reflect.TypeOf(&nilTm).Elem()
func marshalReflect(rv reflect.Value, flags encodeFlags) (types.AttributeValue, error) {
switch rv.Kind() {
case reflect.Ptr:
if rv.IsNil() {
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
return marshal(rv.Elem().Interface(), flags)
case reflect.Bool:
return &types.AttributeValueMemberBOOL{Value: rv.Bool()}, nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return &types.AttributeValueMemberN{Value: strconv.FormatInt(rv.Int(), 10)}, nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
return &types.AttributeValueMemberN{Value: strconv.FormatUint(rv.Uint(), 10)}, nil
case reflect.Float32, reflect.Float64:
return &types.AttributeValueMemberN{Value: strconv.FormatFloat(rv.Float(), 'f', -1, 64)}, nil
case reflect.String:
s := rv.String()
if len(s) == 0 {
if flags&flagAllowEmpty != 0 {
return &types.AttributeValueMemberS{Value: ""}, nil
}
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
return &types.AttributeValueMemberS{Value: s}, nil
case reflect.Map:
if flags&flagSet != 0 {
// sets can't be empty
if rv.Len() == 0 {
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
return marshalSet(rv, flags)
}
// automatically omit nil maps
if rv.IsNil() {
if flags&flagAllowEmpty != 0 {
return &types.AttributeValueMemberM{Value: map[string]types.AttributeValue{}}, nil
}
if flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
return nil, nil
}
var keyString func(k reflect.Value) (string, error)
if ktype := rv.Type().Key(); ktype.Implements(tmType) {
keyString = func(k reflect.Value) (string, error) {
tm := k.Interface().(encoding.TextMarshaler)
txt, err := tm.MarshalText()
if err != nil {
return "", fmt.Errorf("dynamo: marshal map: key error: %v", err)
}
return string(txt), nil
}
} else if ktype.Kind() == reflect.String {
keyString = func(k reflect.Value) (string, error) {
return k.String(), nil
}
} else {
return nil, fmt.Errorf("dynamo marshal: map key must be string: %T", rv.Interface())
}
avs := make(map[string]types.AttributeValue)
subflags := flagNone
if flags&flagAllowEmptyElem != 0 {
subflags |= flagAllowEmpty | flagNull
// child containers of a map also have the allowEmptyElem behavior
// i.e. lists inside a map or maps inside a map
subflags |= flagAllowEmptyElem
} else if flags&flagOmitEmptyElem != 0 {
subflags |= flagOmitEmpty
}
for _, key := range rv.MapKeys() {
v, err := marshal(rv.MapIndex(key).Interface(), subflags)
if err != nil {
return nil, err
}
kstr, err := keyString(key)
if err != nil {
return nil, err
}
if v != nil {
avs[kstr] = v
}
}
if flags&flagOmitEmpty != 0 && len(avs) == 0 {
return nil, nil
}
return &types.AttributeValueMemberM{Value: avs}, nil
case reflect.Struct:
avs, err := marshalStruct(rv)
if err != nil {
return nil, err
}
return &types.AttributeValueMemberM{Value: avs}, nil
case reflect.Slice, reflect.Array:
// special case: byte slice is B
if rv.Type().Elem().Kind() == reflect.Uint8 {
if rv.Len() == 0 {
if rv.IsNil() && flags&flagNull != 0 {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
if flags&flagAllowEmpty != 0 {
return &types.AttributeValueMemberB{Value: []byte{}}, nil
}
return nil, nil
}
var data []byte
if rv.Kind() == reflect.Array {
data = make([]byte, rv.Len())
reflect.Copy(reflect.ValueOf(data), rv)
} else {
data = rv.Bytes()
}
return &types.AttributeValueMemberB{Value: data}, nil
}
if flags&flagNull != 0 && rv.IsNil() {
return &types.AttributeValueMemberNULL{Value: true}, nil
}
// sets
if flags&flagSet != 0 {
// sets can't be empty
if rv.Len() == 0 {
return nil, nil
}
return marshalSet(rv, flags)
}
// lists CAN be empty
avs := make([]types.AttributeValue, 0, rv.Len())
subflags := flagNone
if flags&flagOmitEmptyElem == 0 {
// unless "omitemptyelem" flag is set, include empty/null values
// this will preserve the position of items in the list
subflags |= flagAllowEmpty | flagNull
}
if flags&flagAllowEmptyElem != 0 {
// child containers of a list also have the allowEmptyElem behavior
// e.g. maps inside a list
subflags |= flagAllowEmptyElem
}
for i := 0; i < rv.Len(); i++ {
innerVal := rv.Index(i)
av, err := marshal(innerVal.Interface(), subflags)
if err != nil {
return nil, err
}
if av != nil {
avs = append(avs, av)
}
}
if flags&flagOmitEmpty != 0 && len(avs) == 0 {
return nil, nil
}
return &types.AttributeValueMemberL{Value: avs}, nil
default:
return nil, fmt.Errorf("dynamo marshal: unknown type %s", rv.Type().String())
}
}
func marshalSet(rv reflect.Value, flags encodeFlags) (types.AttributeValue, error) {
iface := reflect.Zero(rv.Type().Elem()).Interface()
switch iface.(type) {
case encoding.TextMarshaler:
ss := make([]string, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
tm := rv.Index(i).Interface().(encoding.TextMarshaler)
text, err := tm.MarshalText()
if err != nil {
return nil, err
}
if flags&flagOmitEmptyElem != 0 && len(text) == 0 {
continue
}
ss = append(ss, string(text))
}
if len(ss) == 0 {
return nil, nil
}
return &types.AttributeValueMemberSS{Value: ss}, nil
}
switch rv.Type().Kind() {
case reflect.Slice:
switch rv.Type().Elem().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
ns := make([]string, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
n := rv.Index(i).Int()
if flags&flagOmitEmptyElem != 0 && n == 0 {
continue
}
ns = append(ns, strconv.FormatInt(n, 10))
}
if len(ns) == 0 {
return nil, nil
}
return &types.AttributeValueMemberNS{Value: ns}, nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
ns := make([]string, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
n := rv.Index(i).Uint()
if flags&flagOmitEmptyElem != 0 && n == 0 {
continue
}
ns = append(ns, strconv.FormatUint(n, 10))
}
if len(ns) == 0 {
return nil, nil
}
return &types.AttributeValueMemberNS{Value: ns}, nil
case reflect.Float32, reflect.Float64:
ns := make([]string, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
n := rv.Index(i).Float()
if flags&flagOmitEmptyElem != 0 && n == 0 {
continue
}
ns = append(ns, strconv.FormatFloat(n, 'f', -1, 64))
}
if len(ns) == 0 {
return nil, nil
}
return &types.AttributeValueMemberNS{Value: ns}, nil
case reflect.String:
ss := make([]string, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
s := rv.Index(i).String()
if flags&flagOmitEmptyElem != 0 && s == "" {
continue
}
ss = append(ss, s)
}
if len(ss) == 0 {
return nil, nil
}
return &types.AttributeValueMemberSS{Value: ss}, nil
case reflect.Slice:
if rv.Type().Elem().Elem().Kind() == reflect.Uint8 {
bs := make([][]byte, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
b := rv.Index(i).Bytes()
if flags&flagOmitEmptyElem != 0 && len(b) == 0 {
continue
}
bs = append(bs, b)
}
if len(bs) == 0 {
return nil, nil
}
return &types.AttributeValueMemberBS{Value: bs}, nil
}
}
case reflect.Map:
useBool := rv.Type().Elem().Kind() == reflect.Bool
if !useBool && rv.Type().Elem() != emptyStructType && !(rv.Type().Elem().Kind() == reflect.Struct && rv.Type().Elem().NumField() == 0) {
return nil, fmt.Errorf("dynamo: cannot marshal type %v into a set", rv.Type())
}
if rv.Type().Key().Implements(tmType) {
ss := make([]string, 0, rv.Len())
for _, k := range rv.MapKeys() {
if !useBool || rv.MapIndex(k).Bool() {
txt, err := k.Interface().(encoding.TextMarshaler).MarshalText()
if err != nil {
return nil, err
}
if flags&flagOmitEmptyElem != 0 && len(txt) == 0 {
continue
}
ss = append(ss, string(txt))
}
}
if len(ss) == 0 {
return nil, nil
}
return &types.AttributeValueMemberSS{Value: ss}, nil
}
switch rv.Type().Key().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
ns := make([]string, 0, rv.Len())
for _, k := range rv.MapKeys() {
if !useBool || rv.MapIndex(k).Bool() {
n := k.Int()
if flags&flagOmitEmptyElem != 0 && n == 0 {
continue
}
ns = append(ns, strconv.FormatInt(n, 10))
}
}
if len(ns) == 0 {
return nil, nil
}
return &types.AttributeValueMemberNS{Value: ns}, nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
ns := make([]string, 0, rv.Len())
for _, k := range rv.MapKeys() {
if !useBool || rv.MapIndex(k).Bool() {
n := k.Uint()
if flags&flagOmitEmptyElem != 0 && n == 0 {
continue
}
ns = append(ns, strconv.FormatUint(n, 10))
}
}
if len(ns) == 0 {
return nil, nil
}
return &types.AttributeValueMemberNS{Value: ns}, nil
case reflect.Float32, reflect.Float64:
ns := make([]string, 0, rv.Len())
for _, k := range rv.MapKeys() {
if !useBool || rv.MapIndex(k).Bool() {
n := k.Float()
if flags&flagOmitEmptyElem != 0 && n == 0 {
continue
}
ns = append(ns, strconv.FormatFloat(n, 'f', -1, 64))
}
}
if len(ns) == 0 {
return nil, nil
}
return &types.AttributeValueMemberNS{Value: ns}, nil
case reflect.String:
ss := make([]string, 0, rv.Len())
for _, k := range rv.MapKeys() {
if !useBool || rv.MapIndex(k).Bool() {
s := k.String()
if flags&flagOmitEmptyElem != 0 && s == "" {
continue
}
ss = append(ss, s)
}
}
if len(ss) == 0 {
return nil, nil
}
return &types.AttributeValueMemberSS{Value: ss}, nil
case reflect.Array:
if rv.Type().Key().Elem().Kind() == reflect.Uint8 {
bs := make([][]byte, 0, rv.Len())
for _, k := range rv.MapKeys() {
if useBool && !rv.MapIndex(k).Bool() {
continue
}
key := make([]byte, k.Len())
reflect.Copy(reflect.ValueOf(key), k)
bs = append(bs, key)
}
if len(bs) == 0 {
return nil, nil
}
return &types.AttributeValueMemberBS{Value: bs}, nil
}
}
}
return nil, fmt.Errorf("dynamo marshal: unknown type for sets %s", rv.Type().String())
}
var emptyStructType = reflect.TypeOf(struct{}{})
func marshalSlice(values []interface{}) ([]types.AttributeValue, error) {
avs := make([]types.AttributeValue, 0, len(values))
for _, v := range values {
av, err := marshal(v, flagAllowEmpty)
if err != nil {
return nil, err
}
avs = append(avs, av)
}
return avs, nil
}
type encodeFlags uint
const (
flagSet encodeFlags = 1 << iota
flagOmitEmpty
flagOmitEmptyElem
flagAllowEmpty
flagAllowEmptyElem
flagNull
flagUnixTime
flagNone encodeFlags = 0
)
func fieldInfo(field reflect.StructField) (name string, flags encodeFlags) {
tags := strings.Split(field.Tag.Get("dynamo"), ",")
if len(tags) == 0 {
return field.Name, flagNone
}
name = tags[0]
if name == "" {
name = field.Name
}
for _, t := range tags[1:] {
switch t {
case "set":
flags |= flagSet
case "omitempty":
flags |= flagOmitEmpty
case "omitemptyelem":
flags |= flagOmitEmptyElem
case "allowempty":
flags |= flagAllowEmpty
case "allowemptyelem":
flags |= flagAllowEmptyElem
case "null":
flags |= flagNull
case "unixtime":
flags |= flagUnixTime
}
}
return
}
type isZeroer interface {
IsZero() bool
}
// thanks James Henstridge
func isZero(rv reflect.Value) bool {
// use IsZero for supported types
if rv.CanInterface() {
if zeroer, ok := rv.Interface().(isZeroer); ok {
if rv.Kind() == reflect.Ptr && rv.IsNil() {
if _, cantCall := rv.Type().Elem().MethodByName("IsZero"); cantCall {
// can't call a value method on a nil pointer type
return true
}
}
return zeroer.IsZero()
}
}
// always return false for certain interfaces, check these later
iface := rv.Interface()
switch iface.(type) {
case Marshaler:
return false
case encoding.TextMarshaler:
return false
}
switch rv.Kind() {
case reflect.Func, reflect.Map, reflect.Slice:
return rv.IsNil()
case reflect.Array:
z := true
for i := 0; i < rv.Len(); i++ {
z = z && isZero(rv.Index(i))
}
return z
case reflect.Struct:
z := true
for i := 0; i < rv.NumField(); i++ {
z = z && isZero(rv.Field(i))
}
return z
}
// Compare other types directly:
z := reflect.Zero(rv.Type())
return rv.Interface() == z.Interface()
}