-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
decode_aux_test.go
159 lines (140 loc) · 3.45 KB
/
decode_aux_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
package dynamo_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/guregu/dynamo/v2"
)
type Coffee struct {
Name string
}
func TestEncodingAux(t *testing.T) {
// This tests behavior of embedded anonymous (unexported) structs
// using the "aux" unmarshaling trick.
// See: https://github.com/guregu/dynamo/issues/181
in := dynamo.Item{
"ID": &types.AttributeValueMemberS{Value: "intenso"},
"Name": &types.AttributeValueMemberS{Value: "Intenso 12"},
}
type coffeeItemDefault struct {
ID string
Coffee
}
tests := []struct {
name string
out interface{}
}{
{name: "no custom unmarshalling", out: coffeeItemDefault{ID: "intenso", Coffee: Coffee{Name: "Intenso 12"}}},
{name: "AWS SDK pointer", out: coffeeItemSDKEmbeddedPointer{ID: "intenso", Coffee: &Coffee{Name: "Intenso 12"}}},
{name: "flat", out: coffeeItemFlat{ID: "intenso", Name: "Intenso 12"}},
{name: "flat (invalid)", out: coffeeItemInvalid{}}, // want to make sure this doesn't panic
{name: "embedded", out: coffeeItemEmbedded{ID: "intenso", Coffee: Coffee{Name: "Intenso 12"}}},
{name: "embedded pointer", out: coffeeItemEmbeddedPointer{ID: "intenso", Coffee: &Coffee{Name: "Intenso 12"}}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
out := reflect.New(reflect.TypeOf(test.out)).Interface()
if err := dynamo.UnmarshalItem(in, out); err != nil {
t.Fatal(err)
}
got := reflect.ValueOf(out).Elem().Interface()
if !reflect.DeepEqual(test.out, got) {
t.Error("bad value. want:", test.out, "got:", got)
}
})
}
}
type coffeeItemFlat struct {
ID string
Name string
}
func (c *coffeeItemFlat) UnmarshalDynamoItem(item dynamo.Item) error {
type alias coffeeItemFlat
aux := struct {
*alias
}{
alias: (*alias)(c),
}
if err := dynamo.UnmarshalItem(item, &aux); err != nil {
return err
}
return nil
}
type coffeeItemInvalid struct {
ID string
Name string
}
func (c *coffeeItemInvalid) UnmarshalDynamoItem(item dynamo.Item) error {
type alias coffeeItemInvalid
aux := struct {
*alias
}{
alias: (*alias)(nil),
}
if err := dynamo.UnmarshalItem(item, &aux); err != nil {
return err
}
return nil
}
type coffeeItemEmbedded struct {
ID string
Coffee
}
func (c *coffeeItemEmbedded) UnmarshalDynamoItem(item dynamo.Item) error {
type alias coffeeItemEmbedded
aux := struct {
*alias
}{
alias: (*alias)(c),
}
if err := dynamo.UnmarshalItem(item, &aux); err != nil {
return err
}
return nil
}
type coffeeItemEmbeddedPointer struct {
ID string
*Coffee
}
func (c *coffeeItemEmbeddedPointer) UnmarshalDynamoItem(item dynamo.Item) error {
type alias coffeeItemEmbeddedPointer
aux := struct {
*alias
}{
alias: (*alias)(c),
}
if err := dynamo.UnmarshalItem(item, &aux); err != nil {
return err
}
return nil
}
func (c *coffeeItemEmbeddedPointer) UnmarshalJSON(data []byte) error {
type alias coffeeItemEmbeddedPointer
aux := struct {
*alias
}{
alias: (*alias)(c),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
return nil
}
type coffeeItemSDKEmbeddedPointer struct {
ID string
*Coffee
}
func (c *coffeeItemSDKEmbeddedPointer) UnmarshalDynamoItem(item dynamo.Item) error {
type alias coffeeItemEmbeddedPointer
aux := struct {
*alias
}{
alias: (*alias)(c),
}
if err := attributevalue.UnmarshalMap(item, &aux); err != nil {
return err
}
return nil
}