-
Notifications
You must be signed in to change notification settings - Fork 6
/
json_test.go
417 lines (365 loc) · 10.4 KB
/
json_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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package dstruct
import (
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
/****** 辅助测试定义 ******/
type testStruct struct {
Str string `json:"str"`
Int int `json:"int"`
}
type kv map[string]interface{}
func (k *kv) UnmarshalJSON(data []byte) error {
kv := make(map[string]interface{}, 0)
err := json.Unmarshal(data, &kv)
if err != nil {
return err
}
*k = kv
return nil
}
/****** 测试用例 ******/
// 综合测试
func TestUnmarshalJSON(t *testing.T) {
testStrJSON := `{"int":1,"str":"str","bl":true, "arr_str":["1","2"],"arr_int":[1,2], "map":{"a":1,"b":64}, ` +
`"test_struct":{"str":"string", "int":1},"interface":{"str":"string", "bl":true}}`
ds := &DStruct{}
var i interface{}
ds.SetFields(map[string]reflect.Type{
"int": TypeInt,
"str": TypeString,
"bl": TypeBool,
"arr_str": TypeArrayStr,
"arr_int": TypeArrayInt,
"map": reflect.TypeOf(map[string]int64{}),
"test_struct": reflect.TypeOf(testStruct{}),
"interface": reflect.TypeOf(i),
})
err := json.Unmarshal([]byte(testStrJSON), ds)
if err != nil {
t.Error("unmarshal: " + err.Error())
return
}
require.Equal(t, ds.kv["int"], 1, "test int.")
require.Equal(t, ds.kv["str"], "str", "test str.")
require.Equal(t, ds.kv["bl"], true, "test bool.")
require.Equal(t, ds.kv["arr_str"], []string{"1", "2"}, "test []string.")
require.Equal(t, ds.kv["arr_int"], []int{1, 2}, "test []int.")
require.Equal(t, ds.kv["map"], map[string]int64{"a": 1, "b": 64}, "test map[string]int64.")
require.Equal(t, ds.kv["test_struct"], testStruct{Str: "string", Int: 1}, "test struct.")
require.Equal(t, ds.kv["interface"], map[string]interface{}{"str": "string", "bl": true}, "test interface{}.")
}
func TestMapUnmarshalJSON(t *testing.T) {
dataMap := map[string]map[int]map[string]testStruct{
"l1-1": map[int]map[string]testStruct{
1: map[string]testStruct{
"l1-3": testStruct{Str: "l1-3 string", Int: 1},
},
},
"l2-1": map[int]map[string]testStruct{
2: map[string]testStruct{
"l2-1": testStruct{Str: "l2-3 string", Int: 1},
},
},
"l3-1": map[int]map[string]testStruct{
2: nil,
},
}
jsonWrap := map[string]interface{}{"test_field": dataMap}
testStrJSONBytes, err := json.Marshal(jsonWrap)
if err != nil {
t.Error(err)
return
}
ds := &DStruct{}
ds.SetFields(map[string]reflect.Type{
"test_field": reflect.TypeOf(dataMap),
})
err = json.Unmarshal(testStrJSONBytes, ds)
if err != nil {
t.Error("unmarshal: " + err.Error())
return
}
rawData := make(map[string]map[int]map[string]testStruct, 0)
ok, err := ds.Value("test_field", &rawData)
if err != nil {
t.Errorf(err.Error())
return
}
if !ok {
t.Errorf("not found test field")
return
}
require.Equal(t, dataMap, rawData, "test many level map.")
}
func TestCustomUnmarshalJSON(t *testing.T) {
ds := &DStruct{}
ds.SetFields(map[string]reflect.Type{
"test_unmarshaljson": reflect.TypeOf(&kv{}),
})
testStrJSONBytes := []byte(`{"test_unmarshaljson":{"k1":"v1", "k2":"k2"}}`)
err := json.Unmarshal(testStrJSONBytes, ds)
if err != nil {
t.Errorf(err.Error())
return
}
dataMap := kv{
"k1": "v1",
"k2": "k2",
}
rawData := &kv{}
// 这里需要取两次地址
ok, err := ds.Value("test_unmarshaljson", &rawData)
if err != nil {
t.Errorf(err.Error())
return
}
if !ok {
t.Errorf("not found test field")
return
}
require.Equal(t, dataMap, *rawData, "test user custom UnmarshalJSON map.")
}
func TestPtrUnmarshalJSON(t *testing.T) {
ds := &DStruct{}
ds.SetFields(map[string]reflect.Type{
"ptr": reflect.TypeOf(new(testStruct)),
})
testStrJSONBytes := []byte(`{"ptr":{"str":"string", "int":1}}`)
err := json.Unmarshal(testStrJSONBytes, ds)
if err != nil {
t.Errorf(err.Error())
return
}
rawData := &testStruct{
Str: "string",
Int: 1,
}
require.Equal(t, ds.kv["ptr"], rawData, "test ptr.")
}
func TestNone(t *testing.T) {
testStrJSON := `{}`
ds := &DStruct{}
var i interface{}
ds.SetFields(map[string]reflect.Type{
"int64": reflect.TypeOf(int64(1)),
"string": reflect.TypeOf(string("1")),
"bl": reflect.TypeOf(bool(true)),
"arr_str": reflect.TypeOf([]string{}),
"map_str_int64": reflect.TypeOf(map[string]int64{}),
"test_struct": reflect.TypeOf(testStruct{}),
"interface": reflect.TypeOf(i),
})
err := json.Unmarshal([]byte(testStrJSON), ds)
if err != nil {
t.Errorf(err.Error())
return
}
require.Equal(t, ds.kv["int64"], int64(0), "test int default.")
require.Equal(t, ds.kv["string"], "", "test string default.")
require.Equal(t, ds.kv["bl"], false, "test bool default.")
require.Equal(t, ds.kv["arr_str"], ([]string)(nil), "test []string default.")
require.Equal(t, ds.kv["test_struct"], testStruct{}, "test struct default.")
require.Equal(t, ds.kv["interface"], nil, "test interface default.")
}
func TestInterface(t *testing.T) {
testStrJSONBytes := []byte(`{"str":"str","bl":true}`)
ds := &DStruct{}
var i interface{}
ds.SetFields(map[string]reflect.Type{
"str": reflect.TypeOf(i),
"bl": reflect.TypeOf(i),
})
err := json.Unmarshal(testStrJSONBytes, ds)
if err != nil {
t.Errorf(err.Error())
return
}
require.Equal(t, ds.kv["str"], "str", "test string interface{}.")
}
func TestMapInterface(t *testing.T) {
testStrJSON := `{"map":{"str":"str","bl":true}, "arr":["1", "2"],` +
` "arr_map":[{"str":"str"}],"arr_arr":[["str1","str2"]]}`
ds := &DStruct{}
var i interface{}
ds.SetFields(map[string]reflect.Type{
"map": reflect.TypeOf(i),
"arr": reflect.TypeOf(i),
"arr_map": reflect.TypeOf(i),
"arr_arr": reflect.TypeOf(i),
})
err := json.Unmarshal([]byte(testStrJSON), ds)
if err != nil {
t.Errorf(err.Error())
return
}
require.Equal(t, ds.kv["map"], map[string]interface{}{"str": "str", "bl": true}, "test map interface{}.")
require.Equal(t, ds.kv["arr"], []interface{}{"1", "2"}, "test map interface{}.")
require.Equal(t, ds.kv["arr_map"], []interface{}{map[string]interface{}{"str": "str"}}, "test arr_map interface{}.")
require.Equal(t, ds.kv["arr_arr"], []interface{}{[]interface{}{"str1", "str2"}}, "test arr_arr interface{}.")
}
func TestJSONNumber(t *testing.T) {
testStrJSON := `{"int":10000, "arr":[1]}`
ds := &DStruct{}
var i interface{}
ds.SetFields(map[string]reflect.Type{
"int": reflect.TypeOf(i),
"arr": reflect.TypeOf(i),
})
ds.JSONNumber()
err := json.Unmarshal([]byte(testStrJSON), ds)
if err != nil {
t.Errorf(err.Error())
return
}
require.Equal(t, ds.kv["int"], (json.Number)("10000"), "test json number interface{}.")
require.Equal(t, ds.kv["arr"], []interface{}{(json.Number)("1")}, "test json number arr interface{}.")
var intVal interface{}
ok, err := ds.Value("int", &intVal)
if err != nil {
t.Errorf(err.Error())
return
}
if !ok {
t.Errorf("not found test int field")
return
}
intJSONNumberVal, ok := intVal.(json.Number)
if !ok {
t.Errorf("test int field. not json.Unmber")
return
}
require.Equal(t, intJSONNumberVal, (json.Number)("10000"), "test json number interface{}.")
}
func TestAliasType(t *testing.T) {
type MyInt int64
type MyStr string
testStrJSONBytes := `{"int":10000, "arr":[1], "str":"str"}`
ds := &DStruct{}
ds.SetFields(map[string]reflect.Type{
"int": reflect.TypeOf(MyInt(1)),
"arr": reflect.TypeOf([]MyInt{}),
"str": reflect.TypeOf(string("")),
})
ds.JSONNumber()
err := json.Unmarshal([]byte(testStrJSONBytes), ds)
if err != nil {
t.Errorf(err.Error())
return
}
require.Equal(t, ds.kv["int"], (MyInt)(10000), "test alias int .")
require.Equal(t, ds.kv["arr"], []MyInt{1}, "test alias int array arr.")
require.Equal(t, ds.kv["str"], "str", "test alias string .")
var intVal MyInt
ok, err := ds.Value("int", &intVal)
if err != nil {
t.Errorf(err.Error())
return
}
if !ok {
t.Errorf("not found test int field")
return
}
require.Equal(t, intVal, (MyInt)(10000), "test json number interface{}.")
}
func TestJSONMarshal(t *testing.T) {
type MyInt int64
type MyStr string
testStrJSON := `{"str":"str"}`
ds := &DStruct{}
ds.SetFields(map[string]reflect.Type{
"str": reflect.TypeOf(string("")),
})
err := json.Unmarshal([]byte(testStrJSON), ds)
if err != nil {
t.Errorf(err.Error())
return
}
require.Equal(t, ds.kv["str"], "str", "test alias string .")
marshalBytes, err := json.Marshal(ds)
if err != nil {
t.Errorf(err.Error())
return
}
if string(marshalBytes) != testStrJSON {
t.Errorf("not equal")
return
}
}
/****** 测试validator用例 ******/
type testValidateStruct struct {
Str string `json:"str" bson:"str" validate:"required,min=3,max=10"`
Int int `json:"int" bson:"int" validate:"required,gte=3,lte=10"`
}
func TestValidatorUnmarshalJSON(t *testing.T) {
suits := []struct {
input string
hasErr bool
}{
{
input: `{"int":4,"str":"str","test_struct":{"str":"123", "int":4}}`,
},
{
input: `{"int":4,"str":"str","test_struct":{"str":"1234567890", "int":10}}`,
},
{
input: `{"int":4,"str":"str","test_struct":{"str":"12345", "int":5}}`,
},
// str validator
{
input: `{"int":4,"str":"str","test_struct":{"str":"", "int":4}}`,
hasErr: true,
},
{
input: `{"int":4,"str":"str","test_struct":{"str":"123456789012121", "int":4}}`,
hasErr: true,
},
{
input: `{"int":4,"str":"str","test_struct":{"str":"12", "int":4}}`,
hasErr: true,
},
// int validator
{
input: `{"int":4,"str":"str","test_struct":{"str":"123", "int":1}}`,
hasErr: true,
},
{
input: `{"int":4,"str":"str","test_struct":{"str":"123", "int":11}}`,
hasErr: true,
},
{
input: `{"int":4,"str":"str","test_struct":{"str":"123", "int":-1}}`,
hasErr: true,
},
}
ds := &DStruct{}
ds.ValidateOn()
var i interface{}
ds.SetFields(map[string]reflect.Type{
"int": TypeInt,
"str": TypeString,
"bl": TypeBool,
"arr_str": TypeArrayStr,
"arr_int": TypeArrayInt,
"map": reflect.TypeOf(map[string]int64{}),
"test_struct": reflect.TypeOf(testValidateStruct{}),
"interface": reflect.TypeOf(i),
})
for idx, suit := range suits {
err := json.Unmarshal([]byte(suit.input), ds)
if suit.hasErr {
if err == nil {
t.Errorf("unmarshal index %d validator not work ", idx)
} else if !strings.HasPrefix(err.Error(), "validator: field(test_struct) type(testValidateStruct) Key:") {
t.Errorf("unmarshal index %d error %s ", idx, err.Error())
}
} else {
if err != nil {
t.Errorf("unmarshal index %d error %s ", idx, err.Error())
}
}
}
}