-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil_test.go
314 lines (260 loc) · 8.56 KB
/
util_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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2022 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package ear
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_getExtraKeys(t *testing.T) {
m := map[string]interface{}{
"name": "String Archer",
"alias": "duchess",
"age": 42,
"height": "6'2\"",
"eyes": "blue",
"hair": "black",
}
extras := getExtraKeys(m, []string{"name", "age", "height"})
assert.ElementsMatch(t, []string{"alias", "eyes", "hair"}, extras)
extras = getExtraKeys(m, []string{"name", "alias", "age", "height", "eyes", "hair"})
assert.ElementsMatch(t, []string{}, extras)
extras = getExtraKeys(m, []string{"name", "age", "family", "language"})
assert.ElementsMatch(t, []string{"alias", "height", "eyes", "hair"}, extras)
extras = getExtraKeys(m, []string{})
assert.ElementsMatch(t, []string{"name", "alias", "age", "height", "eyes", "hair"}, extras)
extras = getExtraKeys(map[string]interface{}{}, []string{})
assert.ElementsMatch(t, []string{}, extras)
}
type TestEmbedded struct {
FieldOne string `json:"embedded-field,omitempty"`
}
type testStruct struct {
FieldOne string `json:"optional-field,omitempty"`
FieldTwo *int64 `json:"mandatory-field"`
FieldThree *string // untagged
FieldFour *string `cbor:"4,keyasint"` // tagged, but no json
FieldFive *testField `json:"custom,omitempty"`
TestEmbedded
}
type testField struct {
I int `json:"eye"`
}
func Test_populateStructFromMap(t *testing.T) {
parsers := map[string]parser{
// optional-field will rely on the default string parser
"mandatory-field": int64PtrParser,
}
var s testStruct
var i int
m := map[string]interface{}{
"optional-field": "foo",
"mandatory-field": 42,
}
err := populateStructFromMap(&i, m, "json", parsers, stringParser, true)
assert.EqualError(t, err, "wrong type: must be a Struct pointer")
err = populateStructFromMap(s, m, "json", parsers, stringParser, true)
assert.EqualError(t, err, "wrong type: must be a Struct pointer")
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
require.NoError(t, err)
assert.Equal(t, "foo", s.FieldOne)
assert.Equal(t, int64(42), *s.FieldTwo)
m = map[string]interface{}{
"mandatory-field": 42.0,
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
require.NoError(t, err)
assert.Equal(t, int64(42), *s.FieldTwo)
m = map[string]interface{}{
"optional-field": "foo",
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
assert.EqualError(t, err, "missing mandatory 'mandatory-field'")
m = map[string]interface{}{
"optional-field": 7,
"mandatory-field": 42,
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
assert.EqualError(t, err, "invalid value(s) for 'optional-field' (not a string)")
m = map[string]interface{}{
"mandatory-field": "foo",
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
assert.EqualError(t, err, "invalid value(s) for 'mandatory-field' (not an int64)")
m = map[string]interface{}{
"embedded-field": "bar",
"mandatory-field": 42,
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
require.NoError(t, err)
assert.Equal(t, "bar", s.TestEmbedded.FieldOne)
m = map[string]interface{}{
"embedded-field": false,
"mandatory-field": 42,
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
assert.EqualError(t, err, "invalid value(s) for 'embedded-field' (not a string)")
m = map[string]interface{}{
"mandatory-field": 42,
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
require.NoError(t, err)
m = map[string]interface{}{
"mandatory-field": 42,
"unexpected-field": "nothing to see here",
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, true)
assert.NoError(t, err)
m = map[string]interface{}{
"mandatory-field": 42,
"unexpected-field": "nothing to see here",
}
err = populateStructFromMap(&s, m, "json", parsers, stringParser, false)
assert.EqualError(t, err, "unexpected: unexpected-field")
}
func Test_populateStructFromInterface(t *testing.T) {
parsers := map[string]parser{
// optional-field will rely on the default string parser
"mandatory-field": int64PtrParser,
}
var s testStruct
m := map[string]interface{}{
"optional-field": "foo",
"mandatory-field": 42,
}
err := populateStructFromInterface(&s, m, "json", parsers, stringParser, true)
require.NoError(t, err)
assert.Equal(t, "foo", s.FieldOne)
assert.Equal(t, int64(42), *s.FieldTwo)
f2 := int64(7)
f3 := "field-three"
other := testStruct{
FieldOne: "test",
FieldTwo: &f2,
FieldThree: &f3,
TestEmbedded: TestEmbedded{
FieldOne: "embedded-test",
},
}
s = testStruct{}
err = populateStructFromInterface(&s, other, "json", parsers, stringParser, true)
require.NoError(t, err)
assert.Equal(t, "test", s.FieldOne)
assert.Equal(t, int64(7), *s.FieldTwo)
assert.Equal(t, "embedded-test", s.TestEmbedded.FieldOne)
s = testStruct{}
err = populateStructFromInterface(&s, &other, "json", parsers, stringParser, true)
require.NoError(t, err)
assert.Equal(t, "test", s.FieldOne)
assert.Equal(t, int64(7), *s.FieldTwo)
assert.Equal(t, "embedded-test", s.TestEmbedded.FieldOne)
i := 7
err = populateStructFromInterface(&s, i, "json", parsers, stringParser, true)
assert.EqualError(t, err, "invalid value '7': expected a testStruct, but found int")
err = populateStructFromInterface(&s, &i, "json", parsers, stringParser, true)
assert.EqualError(t, err, "invalid value: expected a *testStruct, but found *int")
type test2 struct{}
t2 := test2{}
err = populateStructFromInterface(&s, t2, "json", parsers, stringParser, true)
assert.EqualError(t, err, "invalid value '{}': expected a testStruct, but found ear.test2")
err = populateStructFromInterface(&s, &t2, "json", parsers, stringParser, true)
assert.EqualError(t, err, "invalid value: expected a *testStruct, but found *ear.test2")
}
type myMapable struct {
Key string `json:"key"`
}
func (o myMapable) AsMap() map[string]interface{} {
return map[string]interface{}{
o.Key: 1,
}
}
func Test_structAsMap(t *testing.T) {
type MyEmbedded struct {
FieldOne *string `json:"embedded-field-one,omitempty"`
}
type MyStruct struct {
FieldOne *string `json:"field-one"`
FieldTwo string `json:"field-two"`
FieldThree *int `json:"field-three,omitempty"`
FieldFour *myMapable `json:"field-four,omitempty"`
FieldFive myMapable `json:"field-five"`
FieldSix string // untagged
FieldSeven string `cbor:"7,keyasint"` // tagged by no "json"
FieldEight map[string]*myMapable `json:"field-eight,omitempty"`
FieldNine []*myMapable `json:"field-nine,omitempty"`
MyEmbedded
}
f1 := "first field"
f3 := 1337
f4 := myMapable{Key: "inner"}
fe := "embedded field"
v := MyStruct{
FieldOne: &f1,
FieldTwo: "second field",
FieldThree: &f3,
FieldFour: &f4,
FieldFive: myMapable{Key: "inner"},
FieldSix: "sixth field",
FieldSeven: "Seventh field",
FieldEight: map[string]*myMapable{
"field-eight-sub-one": &f4,
},
FieldNine: []*myMapable{&f4},
MyEmbedded: MyEmbedded{
FieldOne: &fe,
},
}
expected := map[string]interface{}{
"field-one": "first field",
"field-two": "second field",
"field-three": 1337,
"field-four": map[string]interface{}{
"inner": 1,
},
"field-five": map[string]interface{}{
"inner": 1,
},
"field-eight": map[string]interface{}{
"field-eight-sub-one": map[string]interface{}{
"key": "inner",
},
},
"field-nine": []interface{}{
map[string]interface{}{
"key": "inner",
},
},
"embedded-field-one": "embedded field",
}
m, err := structAsMap(v, "json")
require.NoError(t, err)
assert.Equal(t, expected, m)
v = MyStruct{
FieldOne: nil,
FieldTwo: "second field",
FieldThree: &f3,
FieldFour: nil,
FieldFive: myMapable{Key: "inner"},
FieldSix: "sixth field",
FieldSeven: "Seventh field",
MyEmbedded: MyEmbedded{
FieldOne: &fe,
},
}
expected = map[string]interface{}{
"field-one": nil,
"field-two": "second field",
"field-three": 1337,
"field-five": map[string]interface{}{
"inner": 1,
},
"field-eight": map[string]interface{}{},
"field-nine": []interface{}{},
"embedded-field-one": "embedded field",
}
m, err = structAsMap(v, "json")
require.NoError(t, err)
assert.Equal(t, expected, m)
_, err = structAsMap(7, "json")
assert.EqualError(t, err, "invalid value: must be a Struct or a *Struct")
}