-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructmapper_test.go
292 lines (264 loc) · 7.67 KB
/
structmapper_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
package structmapper_test
import (
"github.com/AnimationMentor/structmapper"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Structmapper", func() {
type testStruct2 struct {
Cake string
}
type testStruct struct {
Tuna string `json:"tuna"`
Songs []string `json:"songs"`
FavNumber int `json:"favnum"`
Temperature float64 `json:"temp"`
LikeCandy bool `json:"candy"`
Quiet string `json:"quiet,omitempty"`
Skip string `json:"-"`
NoTag string
unexportedString string
unexportedPtr *testStruct2
}
It("handles bad inputs to StructToStringMap", func() {
a := "a"
Expect(structmapper.StructToStringMap(a)).Error().To(HaveOccurred(),
"passing string value to StructToStringMap should return an error")
Expect(structmapper.StructToStringMap(&a)).Error().To(HaveOccurred(),
"passing string pointer to StructToStringMap should return an error")
type testing struct{}
var b *testing
Expect(structmapper.StructToStringMap(b)).Error().To(HaveOccurred(),
"passing nil struct pointer to StructToStringMap should return an error")
})
It("handles bad inputs to StringMapToStruct", func() {
m := map[string]string{}
a := "a"
Expect(structmapper.StringMapToStruct(m, a, true)).
To(MatchError("second param must be a pointer to a struct",
"passing string value to StringMapToStruct should return an error"))
Expect(structmapper.StringMapToStruct(m, &a, true)).
To(MatchError("second param must be a pointer to a struct",
"passing string pointer to StringMapToStruct should return an error"))
type testing struct{}
var b *testing
Expect(structmapper.StringMapToStruct(m, b, true)).To(MatchError("second param must be a pointer to a struct",
"passing nil struct pointer to StringMapToStruct should return an error"))
b = &testing{}
m = nil
Expect(structmapper.StringMapToStruct(m, b, true)).To(MatchError("first param must not be nil"))
})
DescribeTable("StructToStringMap",
func(expectedToError bool, input *testStruct, expectedMap map[string]string) {
m, err := structmapper.StructToStringMap(input)
if expectedToError {
Expect(err).To(HaveOccurred())
} else {
Expect(m).To(Equal(expectedMap))
}
},
Entry(nil,
true,
nil,
nil,
),
Entry(nil,
false,
&testStruct{"hello", []string{"hi", "nice"}, 2, 20.5, true, "", "", "", "", nil},
map[string]string{
"tuna": "hello", "songs": "[\"hi\",\"nice\"]", "favnum": "2",
"temp": "20.5", "candy": "true", "NoTag": "",
},
),
Entry(nil,
false,
&testStruct{"hello", []string{"hi", "nice"}, 2, 20.5, true, "A", "", "", "", nil},
map[string]string{
"tuna": "hello", "songs": "[\"hi\",\"nice\"]", "favnum": "2",
"temp": "20.5", "candy": "true", "quiet": "A", "NoTag": "",
},
),
Entry(nil,
false,
&testStruct{},
map[string]string{
"tuna": "", "songs": "null", "favnum": "0", "temp": "0", "candy": "false", "NoTag": "",
},
),
)
DescribeTable("StringMapToStruct",
func(expectedToError bool, inputMap map[string]string, expectedStruct *testStruct) {
s := &testStruct{}
err := structmapper.StringMapToStruct(inputMap, s, true)
if expectedToError {
Expect(err).To(HaveOccurred())
} else {
Expect(s).To(Equal(expectedStruct))
}
},
Entry(nil,
true,
nil,
nil,
),
// Note these two are a reverse of the StructToStringMap tests.
Entry(nil,
false,
map[string]string{
"tuna": "hello", "songs": "[\"hi\",\"nice\"]", "favnum": "2", "temp": "20.5", "candy": "true", "NoTag": "",
},
&testStruct{"hello", []string{"hi", "nice"}, 2, 20.5, true, "", "", "", "", nil},
),
Entry(nil,
false,
map[string]string{
"tuna": "", "songs": "null", "favnum": "0", "temp": "0", "candy": "false", "quiet": "A", "NoTag": "",
},
&testStruct{Quiet: "A"},
),
Entry(nil,
false,
map[string]string{
"tuna": "", "songs": "null", "favnum": "0", "temp": "0", "candy": "false", "NoTag": "",
},
&testStruct{},
),
// These test more crazier inputs.
Entry(nil,
false,
map[string]string{
"tuna": "hello", "songs": "[]", "favnum": "2", "temp": "20.5", "candy": "true", "NoTag": "",
},
&testStruct{"hello", []string{}, 2, 20.5, true, "", "", "", "", nil},
),
Entry(nil,
false,
map[string]string{
"tuna": "hello", "songs": "", "favnum": "2", "temp": "20.5", "candy": "true", "NoTag": "",
},
&testStruct{"hello", nil, 2, 20.5, true, "", "", "", "", nil},
),
Entry(nil,
false,
map[string]string{
"tuna": "hello", "favnum": "2", "temp": "20.5", "candy": "true", "NoTag": "",
},
&testStruct{"hello", nil, 2, 20.5, true, "", "", "", "", nil},
),
Entry(nil,
false,
map[string]string{
"tuna": "", "songs": "null", "favnum": "0", "temp": "0", "candy": "false", "Skip": "cake", "NoTag": "",
},
&testStruct{},
),
)
DescribeTable("NonStrictStringMapToStruct",
func(expectedToError bool, inputMap map[string]string, expectedStruct *testStruct) {
s := &testStruct{}
err := structmapper.StringMapToStruct(inputMap, s, false)
if expectedToError {
Expect(err).To(HaveOccurred())
} else {
Expect(s).To(Equal(expectedStruct))
}
},
Entry(nil,
false,
map[string]string{"songs": "hi,nice"},
&testStruct{Songs: []string{"hi", "nice"}},
),
Entry(nil,
false,
map[string]string{"songs": "hi, nice"},
&testStruct{Songs: []string{"hi", "nice"}},
),
Entry(nil,
false,
map[string]string{"songs": "hi"},
&testStruct{Songs: []string{"hi"}},
),
Entry(nil,
false,
map[string]string{"candy": "True"},
&testStruct{LikeCandy: true},
),
Entry(nil,
false,
map[string]string{"candy": "1"},
&testStruct{LikeCandy: true},
),
Entry(nil,
false,
map[string]string{"candy": "99"},
&testStruct{LikeCandy: true},
),
Entry(nil,
false,
map[string]string{"candy": "cake"},
&testStruct{LikeCandy: false},
),
)
DescribeTable("GetJSONTag",
func(inName string, inTag string, expectedTag string, expectedOmitEmpty bool) {
gotTag, gotOmitEmpty, _ := structmapper.GetJSONTag(inName, inTag)
Expect(gotTag).To(Equal(expectedTag))
Expect(gotOmitEmpty).To(Equal(expectedOmitEmpty))
},
Entry(nil, "", "", "", false),
Entry(nil, "Cake", "", "Cake", false),
Entry(nil, "Mars", "tuna", "tuna", false),
Entry(nil, "Mars", "tuna,", "tuna", false),
Entry(nil, "Mars", "tuna,omitempty", "tuna", true),
Entry(nil, "Mars", ",omitempty", "Mars", true),
Entry(nil, "Mars", "-,omitempty", "-", true),
Entry(nil, "Mars", "-,", "-", false),
Entry(nil, "Mars", "-", "", false),
)
DescribeTable("StringToBool",
func(in string, expected bool) {
Expect(structmapper.StringToBool(in)).To(Equal(expected))
},
Entry(nil, "", false),
Entry(nil, "0", false),
Entry(nil, "-1", false),
Entry(nil, "False", false),
Entry(nil, "false", false),
Entry(nil, "cheese", false),
Entry(nil, "1", true),
Entry(nil, "99", true),
Entry(nil, "t", true),
Entry(nil, "true", true),
Entry(nil, "True", true),
Entry(nil, "tonsils", true),
)
It("handles AnonymousFields", func() {
type T2 struct {
F21 int `json:"f21"`
F22 []string `json:"f22"`
}
type T1 struct {
T2
F11 int `json:"f11"`
F12 []string `json:"f12"`
}
t1 := T1{
F11: 11,
F12: []string{"f12"},
}
t1.F21 = 21
t1.F22 = []string{"f22"}
m, err := structmapper.StructToStringMap(&t1)
expected := map[string]string{
"f11": "11",
"f12": "[\"f12\"]",
"f21": "21",
"f22": "[\"f22\"]",
}
Expect(err).To(Succeed())
Expect(m).To(Equal(expected))
t2 := T1{}
Expect(structmapper.StringMapToStruct(m, &t2, true)).To(Succeed())
Expect(t1).To(Equal(t2))
})
})