forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
329 lines (304 loc) · 8.01 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
package dynamo
import (
"encoding"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Unmarshaler is the interface implemented by objects that can unmarshal
// an AttributeValue into themselves.
type Unmarshaler interface {
UnmarshalDynamo(av *dynamodb.AttributeValue) error
}
// used in iterators for unmarshaling one item
type unmarshalFunc func(map[string]*dynamodb.AttributeValue, interface{}) error
// 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 u, ok := iface.(Unmarshaler); ok {
return u.UnmarshalDynamo(av)
}
if u, ok := iface.(encoding.TextUnmarshaler); ok {
if av.S != nil {
return u.UnmarshalText([]byte(*av.S))
}
}
}
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 errors.New("dynamo: unmarshal bool: expected BOOL to be non-nil")
}
rv.SetBool(*av.BOOL)
return nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
if av.N == nil {
return errors.New("dynamo: unmarshal int: expected N to be non-nil")
}
n, err := strconv.ParseInt(*av.N, 10, 64)
if err != nil {
return err
}
rv.SetInt(n)
return nil
case reflect.Float64, reflect.Float32:
if av.N == nil {
return errors.New("dynamo: unmarshal int: expected N to be non-nil")
}
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 errors.New("dynamo: unmarshal string: expected S to be non-nil")
}
rv.SetString(*av.S)
return nil
case reflect.Struct:
if av.M == nil {
return errors.New("dynamo: unmarshal struct: expected M to be non-nil")
}
if err := unmarshalItem(av.M, rv.Addr().Interface()); err != nil {
return err
}
return nil
case reflect.Map:
if av.M == nil {
return errors.New("dynamo: unmarshal map: expected M to be non-nil")
}
if rv.IsNil() {
// TODO: maybe always remake this?
// I think the JSON library doesn't...
rv.Set(reflect.MakeMap(rv.Type()))
}
// TODO: this is probably slow
for k, v := range av.M {
innerRV := reflect.New(rv.Type().Elem())
if err := unmarshalReflect(v, innerRV.Elem()); err != nil {
return err
}
rv.SetMapIndex(reflect.ValueOf(k), innerRV.Elem())
}
return nil
case reflect.Slice:
return unmarshalSlice(av, rv)
case reflect.Interface:
// interface{}
if rv.NumMethod() == 0 {
iface, err := av2iface(av)
if err != nil {
return err
}
rv.Set(reflect.ValueOf(iface))
return nil
}
}
iface := rv.Interface()
return fmt.Errorf("dynamo: can't 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 errors.New("dynamo: unmarshal slice: B, L, BS, SS, NS are nil")
}
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 {
fields[k] = v
}
continue
}
fields[name] = fv
}
return fields
}
// unmarshals a struct
func unmarshalItem(item map[string]*dynamodb.AttributeValue, out interface{}) error {
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 {
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)
}