-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapping_test.go
More file actions
342 lines (314 loc) · 8 KB
/
mapping_test.go
File metadata and controls
342 lines (314 loc) · 8 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package probe
import (
"reflect"
"testing"
)
type TestStruct struct {
String string `map:"string"`
Number int `map:"number"`
SliceString []string `map:"slice_string"`
SliceAnyString []string `map:"slice_any_string"`
SliceStruct []TestEmbedStruct `map:"slice_struct"`
Bool bool `map:"bool"`
Bytes []byte `map:"bytes"`
Required string `map:"required" validate:"required"`
MapStrStr map[string]string `map:"map_str_str"`
EmbedStruct TestEmbedStruct `map:"embed_struct"`
Nested1 TestNested1 `map:"nested1"`
}
type TestEmbedStruct struct {
Name string `map:"name"`
}
type TestNested1 struct {
Nested2 TestNested2 `map:"nested2"`
}
type TestNested2 struct {
Nested3 TestNested3 `map:"nested3"`
}
type TestNested3 struct {
Nested4 TestNested4 `map:"nested4"`
}
type TestNested4 struct {
Nested5 []TestEmbedStruct `map:"nested5"`
}
func TestMapToStructByTags_Types(t *testing.T) {
got := TestStruct{
String: "hello, world!",
MapStrStr: map[string]string{
"foo": "bar",
"hello": "world",
},
}
params := map[string]any{
"string": "s-t-r-i-n-g",
"number": 123,
"slice_string": []string{
"a-a-a",
"b-b-b",
"c-c-c",
},
"slice_any_string": []any{
"a-a-a",
"b-b-b",
"c-c-c",
},
"slice_struct": []any{
map[string]any{"name": "foo"},
map[string]any{"name": "bar"},
},
"bool": false,
"bytes": "b-y-t-e-s",
"required": "required!",
"map_str_str": map[string]any{
"foo": "f-o-o",
"bar": "b-a-r",
"baz": "b-a-z",
},
"embed_struct": map[string]any{
"name": "probe",
},
}
expects := TestStruct{
String: "s-t-r-i-n-g",
Number: 123,
SliceString: []string{
"a-a-a",
"b-b-b",
"c-c-c",
},
SliceAnyString: []string{
"a-a-a",
"b-b-b",
"c-c-c",
},
SliceStruct: []TestEmbedStruct{
{Name: "foo"},
{Name: "bar"},
},
Bool: false,
Bytes: []byte("b-y-t-e-s"),
Required: "required!",
MapStrStr: map[string]string{
"foo": "f-o-o",
"bar": "b-a-r",
"baz": "b-a-z",
"hello": "world",
},
EmbedStruct: TestEmbedStruct{
Name: "probe",
},
}
if err := MapToStructByTags(params, &got); err != nil {
t.Errorf("MapToStructByTags error %s", err)
}
if !reflect.DeepEqual(got, expects) {
t.Errorf("\nExpected:\n%#v\nGot:\n%#v", expects, got)
}
}
func TestMapToStructByTags_Required(t *testing.T) {
got := TestStruct{}
params := map[string]any{"string": "yo"}
err := MapToStructByTags(params, &got)
if err.Error() != "required field 'required' is missing" {
t.Errorf("MapToStructByTags error is wrong: %s", err)
}
}
func TestMergeStringMaps(t *testing.T) {
tests := []struct {
name string
base map[string]string
over map[string]any
expected map[string]string
}{
{
name: "merge string values",
base: map[string]string{"a": "1", "b": "2"},
over: map[string]any{"b": "overridden", "c": "3"},
expected: map[string]string{"a": "1", "b": "overridden", "c": "3"},
},
{
name: "ignore non-string values",
base: map[string]string{"a": "1"},
over: map[string]any{"a": "overridden", "b": 123, "c": true},
expected: map[string]string{"a": "overridden"},
},
{
name: "empty base map",
base: map[string]string{},
over: map[string]any{"a": "1", "b": "2"},
expected: map[string]string{"a": "1", "b": "2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := MergeStringMaps(tt.base, tt.over)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("MergeStringMaps() = %v, want %v", result, tt.expected)
}
})
}
}
func TestMergeMaps(t *testing.T) {
tests := []struct {
name string
base map[string]any
over map[string]any
expected map[string]any
}{
{
name: "simple merge",
base: map[string]any{"a": 1, "b": 2},
over: map[string]any{"b": 3, "c": 4},
expected: map[string]any{"a": 1, "b": 3, "c": 4},
},
{
name: "recursive merge nested maps",
base: map[string]any{
"a": 1,
"nested": map[string]any{"x": 1, "y": 2},
},
over: map[string]any{
"nested": map[string]any{"y": 3, "z": 4},
"c": 5,
},
expected: map[string]any{
"a": 1,
"nested": map[string]any{"x": 1, "y": 3, "z": 4},
"c": 5,
},
},
{
name: "overwrite with non-map value",
base: map[string]any{"a": map[string]any{"x": 1}},
over: map[string]any{"a": "string"},
expected: map[string]any{"a": "string"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := MergeMaps(tt.base, tt.over)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("MergeMaps() = %v, want %v", result, tt.expected)
}
})
}
}
func TestStructToMapByTags(t *testing.T) {
input := TestStruct{
String: "test",
Number: 42,
SliceString: []string{"a", "b", "c"},
SliceAnyString: []string{},
SliceStruct: []TestEmbedStruct{{Name: "foo"}, {Name: "bar"}},
Bool: true,
Bytes: []byte("bytes"),
Required: "required",
MapStrStr: map[string]string{"key": "value"},
EmbedStruct: TestEmbedStruct{Name: "embedded"},
Nested1: TestNested1{
Nested2: TestNested2{
Nested3: TestNested3{
Nested4: TestNested4{
Nested5: []TestEmbedStruct{{Name: "aaa"}, {Name: "bbb"}},
},
},
},
},
}
expected := map[string]any{
"string": "test",
"number": 42,
"slice_string": []string{"a", "b", "c"},
"slice_any_string": []string{},
"slice_struct": []any{map[string]any{"name": "foo"}, map[string]any{"name": "bar"}},
"bool": true,
"bytes": "bytes",
"required": "required",
"map_str_str": map[string]string{"key": "value"},
"embed_struct": map[string]any{"name": "embedded"},
"nested1": map[string]any{
"nested2": map[string]any{
"nested3": map[string]any{
"nested4": map[string]any{
"nested5": []any{
map[string]any{"name": "aaa"},
map[string]any{"name": "bbb"},
},
},
},
},
},
}
result, err := StructToMapByTags(input)
if err != nil {
t.Errorf("StructToMapByTags() error = %v", err)
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("StructToMapByTags() = %v, want %v", result, expected)
}
}
func TestAnyToString(t *testing.T) {
tests := []struct {
name string
input any
expected string
ok bool
}{
{"string", "hello", "hello", true},
{"bool true", true, "true", true},
{"bool false", false, "false", true},
{"int", 42, "42", true},
{"int64", int64(42), "42", true},
{"float64", 3.14, "3.14", true},
{"[]byte", []byte("bytes"), "bytes", true},
{"nil", nil, "nil", true},
{"unsupported", []int{1, 2, 3}, "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, ok := AnyToString(tt.input)
if ok != tt.ok {
t.Errorf("AnyToString() ok = %v, want %v", ok, tt.ok)
}
if result != tt.expected {
t.Errorf("AnyToString() = %q, want %q", result, tt.expected)
}
})
}
}
func TestTitleCase(t *testing.T) {
tests := []struct {
name string
input string
char string
expected string
}{
{"hyphen separated", "content-type", "-", "Content-Type"},
{"underscore separated", "user_name", "_", "User_Name"},
{"single word", "hello", "-", "Hello"},
{"empty string", "", "-", ""},
{"multiple separators", "a-b-c-d", "-", "A-B-C-D"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TitleCase(tt.input, tt.char)
if result != tt.expected {
t.Errorf("TitleCase() = %q, want %q", result, tt.expected)
}
})
}
}
func TestStrmapToAnymap(t *testing.T) {
input := map[string]string{
"key1": "value1",
"key2": "value2",
}
expected := map[string]any{
"key1": "value1",
"key2": "value2",
}
result := StrmapToAnymap(input)
if !reflect.DeepEqual(result, expected) {
t.Errorf("StrmapToAnymap() = %v, want %v", result, expected)
}
}