-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_to_go_test.go
710 lines (686 loc) · 12.7 KB
/
json_to_go_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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package core
import (
"json-to-go/jsonparser"
"strings"
"testing"
)
func TestGenerate(t *testing.T) {
type args struct {
jsonStr string
config *Config
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "基础对象解析",
args: args{
jsonStr: `{
"k1": "v1",
"k2": true,
"k3": 3
}`,
config: &Config{},
},
want: `type AutoGenerated struct {
K1 string |json:"k1"|
K2 bool |json:"k2"|
K3 int |json:"k3"|
}`,
wantErr: false,
},
{
name: "数组类型解析",
args: args{
jsonStr: `{
"k1": "v1",
"k2": true,
"k3": 3,
"array1": [
1,
2
],
"array2": [
{
"a": "b"
}
],
"array3": [
[
1,
2
]
],
"array4": [
[
{
"a": "b"
}
]
]
}`,
config: &Config{},
},
want: `type AutoGenerated struct {
K1 string |json:"k1"|
K2 bool |json:"k2"|
K3 int |json:"k3"|
Array1 []int |json:"array1"|
Array2 []Array2 |json:"array2"|
Array3 [][]int |json:"array3"|
Array4 [][]Array4 |json:"array4"|
}
type Array2 struct {
A string |json:"a"|
}
type Array4 struct {
A string |json:"a"|
}`,
wantErr: false,
},
{
name: "测试属性的命名,支持中文,处理特殊字符",
args: args{
jsonStr: `{
"地址": "山东枣庄",
"地质": "中文解析后重名了,自动数字编号",
"中文++))": "自动忽略不识别的字符",
"id": "golint代码检查优化",
"user_name": "",
"3只松鼠": "",
"doc_url": "http://localhost",
"docUrl": "http://localhost",
"curl": ""
}`,
config: &Config{},
},
want: `type AutoGenerated struct {
Dz string |json:"地址"|
Dz1 string |json:"地质"|
Zw string |json:"中文++))"|
ID string |json:"id"|
UserName string |json:"user_name"|
Threezss string |json:"3只松鼠"|
DocURL string |json:"doc_url"|
DocURL1 string |json:"docUrl"|
Curl string |json:"curl"|
}`,
wantErr: false,
},
{
name: "测试属性类型推断",
args: args{
jsonStr: `{
"float": 1.15,
"arrayFloat": [
1,
2.1
],
"arrayObjFloat1": [
{
"cost": 10
},
{
"cost": 10.2
}
],
"arrayObjFloat2": [
[
{
"cost": 10
},
{
"cost": 10.2
}
]
]
}`,
config: &Config{},
},
want: `type AutoGenerated struct {
Float float64 |json:"float"|
ArrayFloat []float64 |json:"arrayFloat"|
ArrayObjFloat1 []ArrayObjFloat1 |json:"arrayObjFloat1"|
ArrayObjFloat2 [][]ArrayObjFloat2 |json:"arrayObjFloat2"|
}
type ArrayObjFloat1 struct {
Cost float64 |json:"cost"|
}
type ArrayObjFloat2 struct {
Cost float64 |json:"cost"|
}`,
wantErr: false,
},
{
name: "测试空对象处理",
args: args{
jsonStr: `{}`,
config: &Config{},
},
want: `type AutoGenerated struct {
}`,
wantErr: false,
},
{
name: "测试属性为空对象处理",
args: args{
jsonStr: `{
"a": {}
}`,
config: &Config{},
},
want: `type AutoGenerated struct {
A A |json:"a"|
}
type A struct {
}`,
wantErr: false,
},
{
name: "测试空值,空数组的处理",
args: args{
jsonStr: `{
"array": [
{
"options": [],
"other": null
}
],
"obj": {
"options": [],
"other": null
},
"options": [],
"other": null
}`,
config: &Config{},
},
want: `type AutoGenerated struct {
Array []Array |json:"array"|
Obj Obj |json:"obj"|
Options []interface{} |json:"options"|
Other interface{} |json:"other"|
}
type Array struct {
Options []interface{} |json:"options"|
Other interface{} |json:"other"|
}
type Obj struct {
Options []interface{} |json:"options"|
Other interface{} |json:"other"|
}`,
wantErr: false,
},
{
name: "支持入参是数组",
args: args{
jsonStr: `[
{
"id": 1,
"name": "",
"location": {
"area": "",
"province": ""
}
},
{
"id": 1,
"age": 1,
"location": {
"city": ""
}
}
]`,
config: &Config{},
},
want: `type AutoGenerated struct {
ID int |json:"id"|
Name string |json:"name"|
Location Location |json:"location"|
Age int |json:"age"|
}
type Location struct {
Area string |json:"area"|
Province string |json:"province"|
City string |json:"city"|
}`,
wantErr: false,
},
{
name: "测试数组属性合并",
args: args{
jsonStr: `{
"a": [
{
"a1": {
"a2": [
{
"a3": ""
},
{
"b3": ""
}
]
}
},
{
"a1": {
"a2": [
{
"a3": ""
},
{
"c3": ""
}
],
"d2": "3"
},
"b1": {
"b2": "3"
}
}
],
"b": 1
}`,
config: &Config{
NestFlag: true,
},
},
want: `type AutoGenerated struct {
A []struct {
A1 struct {
A2 []struct {
A3 string |json:"a3"|
B3 string |json:"b3"|
C3 string |json:"c3"|
} |json:"a2"|
D2 string |json:"d2"|
} |json:"a1"|
B1 struct {
B2 string |json:"b2"|
} |json:"b1"|
} |json:"a"|
B int |json:"b"|
}`,
wantErr: false,
},
{
name: "测试数组合并,同一个属性出现多个类型时,依赖类型判断来推断",
args: args{
jsonStr: `{
"questionArray": [
{
"type": "select",
"props": {
"info": ""
},
"options": [],
"other": null
},
{
"type": "select",
"props": {
"info": "",
"value": ""
},
"options": [
{
"label": "",
"value": ""
}
],
"other": "",
"required": false
}
]
}`,
config: &Config{},
},
want: `type AutoGenerated struct {
QuestionArray []QuestionArray |json:"questionArray"|
}
type QuestionArray struct {
Type string |json:"type"|
Props Props |json:"props"|
Options []Options |json:"options"|
Other string |json:"other"|
Required bool |json:"required"|
}
type Props struct {
Info string |json:"info"|
Value string |json:"value"|
}
type Options struct {
Label string |json:"label"|
Value string |json:"value"|
}`,
wantErr: false,
},
{
name: "测试使用指针",
args: args{
jsonStr: `{
"location": {
"province": ""
},
"location1": [
{
"province": ""
}
],
"location2": [
[
{
"province": ""
}
]
]
}`,
config: &Config{
PointerFlag: true,
},
},
want: `type AutoGenerated struct {
Location *Location |json:"location"|
Location1 []*Location1 |json:"location1"|
Location2 [][]*Location2 |json:"location2"|
}
type Location struct {
Province string |json:"province"|
}
type Location1 struct {
Province string |json:"province"|
}
type Location2 struct {
Province string |json:"province"|
}`,
wantErr: false,
},
{
name: "测试注释",
args: args{
jsonStr: `{
// 1.支持单行注释提取,可在上一行或行尾
"k1": "v1",
"k2": "v2", // 行尾注释
"k3": {
"k3_1": {
"k3_1_1": "" // 递归解析注释
}
},
"k4": [{ // 这个注释会跟着k41
"k41": ""
}, {
"k41": "" // 注释合并
}]
}`,
config: &Config{
Comment: Comment1,
},
},
want: `type AutoGenerated struct {
// 1.支持单行注释提取,可在上一行或行尾
K1 string |json:"k1"|
// 行尾注释
K2 string |json:"k2"|
K3 K3 |json:"k3"|
K4 []K4 |json:"k4"|
}
type K3 struct {
K31 K31 |json:"k3_1"|
}
type K31 struct {
// 递归解析注释
K311 string |json:"k3_1_1"|
}
type K4 struct {
// 这个注释会跟着k41
K41 string |json:"k41"|
}`,
wantErr: false,
},
{
name: "测试注释中带双引号",
args: args{
jsonStr: `{
"a": "", // 注释 ""
"b": "", // 注释 ""
"c": "", // 注释 ""
}`,
config: &Config{
Comment: Comment2,
},
},
want: `type AutoGenerated struct {
A string |json:"a"| // 注释 ""
B string |json:"b"| // 注释 ""
C string |json:"c"| // 注释 ""
}`,
wantErr: false,
},
{
name: "测试对象的复杂注释",
args: args{
jsonStr: `{
// k1的注释
// k1的注释2
// k1的注释3
"k1": "v1", // k1的注释4
// k2的注释
// k2的注释2
// k2的注释3
"k2": 2,
// k3的注释
"k3": 3 // k3的注释2
// 这个注释去掉
// 这个注释去掉
}`,
config: &Config{
Comment: Comment1,
},
},
want: `type AutoGenerated struct {
// k1的注释
// k1的注释2
// k1的注释3
// k1的注释4
K1 string |json:"k1"|
// k2的注释
// k2的注释2
// k2的注释3
K2 int |json:"k2"|
// k3的注释
// k3的注释2
K3 int |json:"k3"|
}`,
wantErr: false,
},
{
name: "测试数组的复杂注释",
args: args{
jsonStr: `{
"k1": [ // k1的注释
// 这个注释去掉
1 // 这个注释去掉
// 这个注释去掉
, // 这个注释去掉
// 这个注释去掉
2 // 这个注释去掉
// 这个注释去掉
// 这个注释去掉
],
// k2的注释
"k2": [ // 这个注释去掉
// 这个注释去掉
[ // 这个注释去掉
1 // 这个注释去掉
// 这个注释去掉
, // 这个注释去掉
// 这个注释去掉
2 // 这个注释去掉
// 这个注释去掉
] // 这个注释去掉
// 这个注释去掉
]
}`,
config: &Config{
Comment: Comment2,
},
},
want: `type AutoGenerated struct {
K1 []int |json:"k1"| // k1的注释
K2 [][]int |json:"k2"| // k2的注释
}`,
wantErr: false,
},
{
name: "web使用的测试case",
args: args{
jsonStr: `{
// 支持中文key
"地址": "",
"doc_url": "http://localhost", // golint命名优化
// 重名会在末尾进行递增编号
"docUrl": "http://localhost",
// 整形数字,小于等于int32设置为int,大于int32设置为int64
"int1": 1,
"int2": 3000000000,
// 浮点型数字,全部设置为float64
"float": 1.15,
// 如果是数组,会合并对象的属性和属性类型
"a": [
[{
// 这个a1对象属性不全,会合并数组内其他a1对象的属性
"a1": {
"a2": [{
"a3": "123" // 类型不同,判断为interface{}
}, {
"b3": ""
}]
}
}, {
"a1": {
"a2": [{
"a3": 123 // 类型不同,判断为interface{}
}, {
"c3": ""
}],
"b2": ""
},
"b1": {
"b2": ""
}
}]
]
}`,
config: &Config{
Comment: Comment1,
},
},
want: `type AutoGenerated struct {
// 支持中文key
Dz string |json:"地址"|
// golint命名优化
DocURL string |json:"doc_url"|
// 重名会在末尾进行递增编号
DocURL1 string |json:"docUrl"|
// 整形数字,小于等于int32设置为int,大于int32设置为int64
Int1 int |json:"int1"|
Int2 int64 |json:"int2"|
// 浮点型数字,全部设置为float64
Float float64 |json:"float"|
// 如果是数组,会合并对象的属性和属性类型
A [][]A |json:"a"|
}
type A struct {
// 这个a1对象属性不全,会合并数组内其他a1对象的属性
A1 A1 |json:"a1"|
B1 B1 |json:"b1"|
}
type A1 struct {
A2 []A2 |json:"a2"|
B2 string |json:"b2"|
}
type A2 struct {
// 类型不同,判断为interface{}
A3 interface{} |json:"a3"|
B3 string |json:"b3"|
C3 string |json:"c3"|
}
type B1 struct {
B2 string |json:"b2"|
}`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.want = strings.ReplaceAll(tt.want, "|", "`")
got, err := Generate(tt.args.jsonStr, tt.args.config)
if (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Generate() got = %s, want %s", got, tt.want)
}
})
}
}
func Test_getJSONType(t *testing.T) {
type args struct {
value []byte
t jsonparser.ValueType
}
tests := []struct {
name string
args args
want string
}{
{
args: args{
value: []byte("10.5"),
t: jsonparser.Number,
},
want: TypeFloat64,
},
{
args: args{
value: []byte("10.0"),
t: jsonparser.Number,
},
want: TypeFloat64,
},
{
args: args{
value: []byte("0.0"),
t: jsonparser.Number,
},
want: TypeFloat64,
},
{
args: args{
value: []byte("100"),
t: jsonparser.Number,
},
want: TypeInt,
},
{
args: args{
value: []byte("3000000000"),
t: jsonparser.Number,
},
want: TypeInt64,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getJSONType(tt.args.value, tt.args.t); got != tt.want {
t.Errorf("getJSONType() = %v, want %v", got, tt.want)
}
})
}
}