-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
decode_test.go
295 lines (260 loc) · 6.31 KB
/
decode_test.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
package dynamo
import (
"maps"
"reflect"
"strconv"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
var itemDecodeOnlyTests = []struct {
name string
given Item
expect interface{}
}{
{
// unexported embedded pointers should be ignored
name: "embedded unexported pointer",
given: Item{
"Embedded": &types.AttributeValueMemberBOOL{Value: true},
},
expect: struct {
*embedded
}{},
},
{
// unexported fields should be ignored
name: "unexported fields",
given: Item{
"a": &types.AttributeValueMemberBOOL{Value: true},
},
expect: struct {
a bool
}{},
},
{
// embedded pointers shouldn't clobber existing fields
name: "exported pointer embedded struct clobber",
given: Item{
"Embedded": &types.AttributeValueMemberS{Value: "OK"},
},
expect: struct {
Embedded string
*ExportedEmbedded
}{
Embedded: "OK",
ExportedEmbedded: &ExportedEmbedded{},
},
},
}
func TestUnmarshalAsymmetric(t *testing.T) {
for _, tc := range itemDecodeOnlyTests {
t.Run(tc.name, func(t *testing.T) {
rv := reflect.New(reflect.TypeOf(tc.expect))
expect := rv.Interface()
err := UnmarshalItem(tc.given, expect)
if err != nil {
t.Fatalf("%s: unexpected error: %v", tc.name, err)
}
if !reflect.DeepEqual(rv.Elem().Interface(), tc.expect) {
t.Errorf("%s: bad result: %#v ≠ %#v", tc.name, rv.Elem().Interface(), tc.expect)
}
})
}
}
func TestUnmarshalAppend(t *testing.T) {
var results []struct {
User *int `dynamo:"UserID"`
Page int
Limit uint
Null interface{}
}
id := "12345"
page := "5"
limit := "20"
null := true
item := Item{
"UserID": &types.AttributeValueMemberN{Value: id},
"Page": &types.AttributeValueMemberN{Value: page},
"Limit": &types.AttributeValueMemberN{Value: limit},
"Null": &types.AttributeValueMemberNULL{Value: null},
}
do := unmarshalAppendTo(&results)
for i := range [15]struct{}{} {
item2 := maps.Clone(item)
id := 12345 + i
idstr := strconv.Itoa(id)
item2["UserID"] = &types.AttributeValueMemberN{Value: idstr}
err := do(item2, &results)
if err != nil {
t.Fatal(err)
}
}
for i, h := range results {
if *h.User != 12345+i || h.Page != 5 || h.Limit != 20 || h.Null != nil {
t.Error("invalid hit", h, *h.User)
}
}
var mapResults []map[string]interface{}
for range [15]struct{}{} {
err := unmarshalAppend(item, &mapResults)
if err != nil {
t.Fatal(err)
}
}
for _, h := range mapResults {
if h["UserID"] != 12345.0 || h["Page"] != 5.0 || h["Limit"] != 20.0 || h["Null"] != nil {
t.Error("invalid interface{} hit", h)
}
}
}
func TestUnmarshal(t *testing.T) {
for _, tc := range encodingTests {
t.Run(tc.name, func(t *testing.T) {
rv := reflect.New(reflect.TypeOf(tc.in))
// dec := newDecodePlan(rv.Elem())
// err := dec.decodeAttr(flagNone, tc.out, rv)
err := Unmarshal(tc.out, rv.Interface())
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
got := rv.Elem().Interface()
if !reflect.DeepEqual(got, tc.in) {
t.Errorf("%s: bad result: \n%#v ≠\n%#v", tc.name, got, tc.out)
}
})
}
}
func TestUnmarshalItem(t *testing.T) {
for _, tc := range itemEncodingTests {
t.Run(tc.name, func(t *testing.T) {
rv := reflect.New(reflect.TypeOf(tc.in))
err := unmarshalItem(tc.out, rv.Interface())
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
iface := rv.Elem().Interface()
if !reflect.DeepEqual(iface, tc.in) {
t.Errorf("%s: bad result: %#v ≠ %#v", tc.name, iface, tc.in)
}
})
}
}
func TestUnmarshalMissing(t *testing.T) {
// This test makes sure we're zeroing out fields of structs even if the given data doesn't contain them
type widget2 struct {
widget
Inner struct {
Blarg string
}
Foo *struct {
Bar int
}
}
w := widget2{
widget: widget{
UserID: 111,
Time: time.Now().UTC(),
Msg: "hello",
},
}
w.Inner.Blarg = "AHH"
w.Foo = &struct{ Bar int }{Bar: 1337}
want := widget2{
widget: widget{
UserID: 112,
},
}
replace := Item{
"UserID": &types.AttributeValueMemberN{Value: "112"},
}
if err := UnmarshalItem(replace, &w); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, w) {
t.Error("bad unmarshal missing. want:", want, "got:", w)
}
replace2 := Item{
"UserID": &types.AttributeValueMemberN{Value: "113"},
"Foo": &types.AttributeValueMemberM{
Value: Item{
"Bar": &types.AttributeValueMemberN{Value: "1338"},
},
},
}
want = widget2{
widget: widget{
UserID: 113,
},
Foo: &struct{ Bar int }{Bar: 1338},
}
if err := UnmarshalItem(replace2, &w); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, w) {
t.Error("bad unmarshal missing. want:", want, "got:", w)
}
}
func TestUnmarshalClearFields(t *testing.T) {
// tests against a regression from v1.12.0 in which map fields were not properly getting reset
type Foo struct {
Map map[string]bool
}
items := []Foo{
{Map: map[string]bool{"a": true}},
{Map: map[string]bool{"b": true}}, // before fix: {a: true, b: true}
{Map: map[string]bool{"c": true}}, // before fix: {a: true, b: true, c: true}
}
var foo Foo
for _, item := range items {
raw, err := MarshalItem(item)
if err != nil {
t.Fatal(err)
}
if err := UnmarshalItem(raw, &foo); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(item, foo) {
t.Error("bad result. want:", item, "got:", foo)
}
}
}
func BenchmarkUnmarshalReflect(b *testing.B) {
var got widget
for i := 0; i < b.N; i++ {
unmarshalItem(exampleItem, &got)
}
}
func TestDecode3(t *testing.T) {
want := exampleWant
var got widget
rv := reflect.ValueOf(&got)
r, err := typedefOf(rv.Type())
if err != nil {
t.Fatal(err)
}
if err := r.decodeItem(exampleItem, rv); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(want, got) {
t.Error("bad decode. want:", want, "got:", got)
}
// spew.Dump(got)
// t.Fail()
}
var exampleItem = map[string]types.AttributeValue{
"UserID": &types.AttributeValueMemberN{Value: "555"},
"Msg": &types.AttributeValueMemberS{Value: "fux"},
"Count": &types.AttributeValueMemberN{Value: "1337"},
"Meta": &types.AttributeValueMemberM{Value: map[string]types.AttributeValue{
"Foo": &types.AttributeValueMemberS{Value: "1336"},
}},
}
var exampleWant = widget{
UserID: 555,
Msg: "fux",
Count: 1337,
Meta: map[string]string{
"Foo": "1336",
},
}