-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
decodefunc.go
301 lines (273 loc) · 8.73 KB
/
decodefunc.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
package dynamo
import (
"fmt"
"reflect"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
type decodeFunc func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error
func decodePtr(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
var elem reflect.Value
if rv.IsNil() {
if rv.CanSet() {
elem = reflect.New(rv.Type().Elem())
rv.Set(elem)
} else {
return nil
}
} else {
elem = rv.Elem()
}
if err := plan.decodeAttr(flags, av, elem); err != nil {
return err
}
return nil
}
func decodeNull(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
if !rv.IsValid() {
return nil
}
if rv.CanSet() {
rv.SetZero()
return nil
}
return nil
}
func decodeString(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
v.SetString(av.(*types.AttributeValueMemberS).Value)
return nil
}
func decodeInt(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
n, err := strconv.ParseInt(av.(*types.AttributeValueMemberN).Value, 10, 64)
if err != nil {
return err
}
v.SetInt(n)
return nil
}
func decodeUint(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
n, err := strconv.ParseUint(av.(*types.AttributeValueMemberN).Value, 10, 64)
if err != nil {
return err
}
v.SetUint(n)
return nil
}
func decodeFloat(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
f, err := strconv.ParseFloat(av.(*types.AttributeValueMemberN).Value, 64)
if err != nil {
return err
}
v.SetFloat(f)
return nil
}
func decodeBool(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
v.SetBool(av.(*types.AttributeValueMemberBOOL).Value)
return nil
}
func decodeBytes(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
v.SetBytes(av.(*types.AttributeValueMemberB).Value)
return nil
}
func decodeSliceL(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
list := av.(*types.AttributeValueMemberL).Value
reallocSlice(v, len(list))
for i, innerAV := range list {
innerRV := v.Index(i).Addr()
if err := plan.decodeAttr(flags, innerAV, innerRV); err != nil {
return err
}
// debugf("slice[i=%d] %#v <- %v", i, v.Index(i).Interface(), innerAV)
}
return nil
}
// func decodeSliceB(plan *decodePlan, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
// reallocSlice(v, len(av.B))
// for i, b := range av.B {
// innerB := reflect.ValueOf(b).Convert(v.Type().Elem())
// innerRV := v.Index(i)
// innerRV.Set(innerB)
// }
// return nil
// }
func decodeSliceBS(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
set := av.(*types.AttributeValueMemberBS).Value
reallocSlice(v, len(set))
for i, b := range set {
innerRV := v.Index(i).Addr()
if err := plan.decodeAttr(flags, &types.AttributeValueMemberB{Value: b}, innerRV); err != nil {
return err
}
}
return nil
}
func decodeSliceSS(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
set := av.(*types.AttributeValueMemberSS).Value
reallocSlice(v, len(set))
for i, s := range set {
innerRV := v.Index(i).Addr()
if err := plan.decodeAttr(flags, &types.AttributeValueMemberS{Value: s}, innerRV); err != nil {
return err
}
}
return nil
}
func decodeSliceNS(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
set := av.(*types.AttributeValueMemberNS).Value
reallocSlice(v, len(set))
for i, n := range set {
innerRV := v.Index(i).Addr()
if err := plan.decodeAttr(flags, &types.AttributeValueMemberN{Value: n}, innerRV); err != nil {
return err
}
}
return nil
}
func decodeArrayB(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
bs := av.(*types.AttributeValueMemberB).Value
if len(bs) > v.Len() {
return fmt.Errorf("dynamo: cannot marshal %s into %s; too small (dst len: %d, src len: %d)", avTypeName(av), v.Type().String(), v.Len(), len(bs))
}
vt := v.Type()
array := reflect.ValueOf(bs)
reflect.Copy(v, array.Convert(vt))
return nil
}
func decodeArrayL(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
list := av.(*types.AttributeValueMemberL).Value
if len(list) > v.Len() {
return fmt.Errorf("dynamo: cannot marshal %s into %s; too small (dst len: %d, src len: %d)", avTypeName(av), v.Type().String(), v.Len(), len(list))
}
for i, innerAV := range list {
if err := plan.decodeAttr(flags, innerAV, v.Index(i)); err != nil {
return err
}
}
return nil
}
func decodeStruct(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
m := av.(*types.AttributeValueMemberM).Value
return visitFields(m, rv, nil, func(av types.AttributeValue, flags encodeFlags, v reflect.Value) error {
if av == nil {
if v.CanSet() && !nullish(v) {
v.SetZero()
}
return nil
}
return plan.decodeAttr(flags, av, v)
})
}
func decodeMap(decodeKey func(reflect.Value, string) error) func(plan *typedef, _ encodeFlags, av types.AttributeValue, v reflect.Value) error {
/*
Something like:
if out == nil {
out = make(map[K]V, len(item))
}
kp := new(K)
for name, av := range item {
vp := new(V)
decodeKey(kp, name)
decodeAttr(av, vp) // TODO: make argument order consistent
out[*kp] = *vp
}
*/
return func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
m := av.(*types.AttributeValueMemberM).Value
reallocMap(rv, len(m))
kp := reflect.New(rv.Type().Key())
for name, v := range m {
if err := decodeKey(kp, name); err != nil {
return fmt.Errorf("error decoding key %q into %v", name, kp.Type().Elem())
}
innerRV := reflect.New(rv.Type().Elem())
if err := plan.decodeAttr(flags, v, innerRV.Elem()); err != nil {
return fmt.Errorf("error decoding map entry %q (%s) into type %v", name, avTypeName(v), innerRV.Type().Elem())
}
rv.SetMapIndex(kp.Elem(), innerRV.Elem())
}
return nil
}
}
func decodeMapSS(decodeKey decodeKeyFunc, truthy reflect.Value) func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
return func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
set := av.(*types.AttributeValueMemberSS).Value
reallocMap(rv, len(set))
kp := reflect.New(rv.Type().Key())
for _, s := range set {
if err := decodeKey(kp, s); err != nil {
return err
}
rv.SetMapIndex(kp.Elem(), truthy)
}
return nil
}
}
func decodeMapNS(decodeKey decodeKeyFunc, truthy reflect.Value) func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
return func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
set := av.(*types.AttributeValueMemberNS).Value
reallocMap(rv, len(set))
kv := reflect.New(rv.Type().Key()).Elem()
for _, n := range set {
if err := plan.decodeAttr(flagNone, &types.AttributeValueMemberN{Value: n}, kv); err != nil {
return err
}
rv.SetMapIndex(kv, truthy)
}
return nil
}
}
func decodeMapBS(decodeKey decodeKeyFunc, truthy reflect.Value) func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
return func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
set := av.(*types.AttributeValueMemberBS).Value
reallocMap(rv, len(set))
kv := reflect.New(rv.Type().Key()).Elem()
for _, bb := range set {
reflect.Copy(kv, reflect.ValueOf(bb))
rv.SetMapIndex(kv, truthy)
}
return nil
}
}
func decode2[T any](fn func(t T, av types.AttributeValue) error) func(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
return func(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
if !rv.CanInterface() {
return nil
}
var value interface{}
if rv.Kind() != reflect.Pointer && rv.CanAddr() {
value = rv.Addr().Interface()
} else {
if rv.IsNil() {
if rv.CanSet() {
rv.Set(reflect.New(rv.Type()).Elem())
} else {
return nil
}
}
value = rv.Interface()
}
return fn(value.(T), av)
}
}
func decodeAny(plan *typedef, flags encodeFlags, av types.AttributeValue, v reflect.Value) error {
iface, err := av2iface(av)
if err != nil {
return err
}
if iface == nil {
v.SetZero()
} else {
v.Set(reflect.ValueOf(iface))
}
return nil
}
func decodeUnixTime(plan *typedef, flags encodeFlags, av types.AttributeValue, rv reflect.Value) error {
rv = indirect(rv)
ts, err := strconv.ParseInt(av.(*types.AttributeValueMemberN).Value, 10, 64)
if err != nil {
return err
}
rv.Set(reflect.ValueOf(time.Unix(ts, 0).UTC()))
return nil
}