-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
encode_test.go
207 lines (190 loc) · 4.39 KB
/
encode_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
package dynamo
import (
"reflect"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
var itemEncodeOnlyTests = []struct {
name string
in interface{}
out Item
}{
{
name: "omitemptyelem",
in: struct {
L []*string `dynamo:",omitemptyelem"`
SS []string `dynamo:",omitemptyelem,set"`
M map[string]string `dynamo:",omitemptyelem"`
Other bool
}{
L: []*string{nil, aws.String("")},
SS: []string{""},
M: map[string]string{"test": ""},
Other: true,
},
out: Item{
"L": &types.AttributeValueMemberL{Value: []types.AttributeValue{}},
"M": &types.AttributeValueMemberM{Value: Item{}},
"Other": &types.AttributeValueMemberBOOL{Value: true},
},
},
{
name: "omitemptyelem + omitempty",
in: struct {
L []*string `dynamo:",omitemptyelem,omitempty"`
M map[string]string `dynamo:",omitemptyelem,omitempty"`
Other bool
}{
L: []*string{nil, aws.String("")},
M: map[string]string{"test": ""},
Other: true,
},
out: Item{
"Other": &types.AttributeValueMemberBOOL{Value: (true)},
},
},
{
name: "allowemptyelem flag on map with a struct element that has a map field",
in: struct {
M map[string]interface{} `dynamo:",allowemptyelem"`
}{
M: map[string]interface{}{
"struct": struct {
InnerMap map[string]interface{} // no struct tags, empty elems not encoded
}{
InnerMap: map[string]interface{}{
"empty": "",
},
},
},
},
out: Item{
"M": &types.AttributeValueMemberM{
Value: Item{
"struct": &types.AttributeValueMemberM{
Value: Item{
"InnerMap": &types.AttributeValueMemberM{
Value: Item{
// expected empty inside
},
},
},
},
},
},
},
},
{
name: "unexported field",
in: struct {
Public int
private int
private2 *int
}{
Public: 555,
private: 1337,
private2: new(int),
},
out: Item{
"Public": &types.AttributeValueMemberN{Value: ("555")},
},
},
{
name: "nil exported pointer embedded struct",
in: struct {
ID string
*ExportedEmbedded
}{
ID: "abc",
},
out: Item{
"ID": &types.AttributeValueMemberS{Value: "abc"},
},
},
}
func TestMarshal(t *testing.T) {
for _, tc := range encodingTests {
t.Run(tc.name, func(t *testing.T) {
item, err := marshal(tc.in, flagNone)
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
if !reflect.DeepEqual(item, tc.out) {
t.Errorf("%s: bad result: %#v ≠ %#v", tc.name, item, tc.out)
}
})
}
}
func TestMarshalItem(t *testing.T) {
for _, tc := range itemEncodingTests {
t.Run(tc.name, func(t *testing.T) {
item, err := marshalItem(tc.in)
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
if !reflect.DeepEqual(item, tc.out) {
t.Errorf("%s: bad result: %#v ≠ %#v", tc.name, item, tc.out)
}
})
}
}
func TestMarshalItemAsymmetric(t *testing.T) {
for _, tc := range itemEncodeOnlyTests {
t.Run(tc.name, func(t *testing.T) {
item, err := marshalItem(tc.in)
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
if !reflect.DeepEqual(item, tc.out) {
t.Errorf("%s: bad result: %#v ≠ %#v", tc.name, item, tc.out)
}
})
}
}
type isValue_Kind interface {
isValue_Kind()
}
type myStruct struct {
OK bool
Value isValue_Kind
}
func (ms *myStruct) MarshalDynamoItem() (map[string]types.AttributeValue, error) {
world := "world"
return map[string]types.AttributeValue{
"hello": &types.AttributeValueMemberS{Value: world},
}, nil
}
func (ms *myStruct) UnmarshalDynamoItem(item map[string]types.AttributeValue) error {
hello := item["hello"]
if h, ok := hello.(*types.AttributeValueMemberS); ok && h.Value == "world" {
ms.OK = true
} else {
ms.OK = false
}
return nil
}
var _ ItemMarshaler = &myStruct{}
var _ ItemUnmarshaler = &myStruct{}
func TestMarshalItemBypass(t *testing.T) {
something := &myStruct{}
got, err := MarshalItem(something)
if err != nil {
t.Fatal(err)
}
world := "world"
expect := map[string]types.AttributeValue{
"hello": &types.AttributeValueMemberS{Value: world},
}
if !reflect.DeepEqual(got, expect) {
t.Error("bad marshal. want:", expect, "got:", got)
}
var dec myStruct
err = UnmarshalItem(got, &dec)
if err != nil {
t.Fatal(err)
}
if !dec.OK {
t.Error("bad unmarshal")
}
}