@@ -7,64 +7,102 @@ function quote(str) {
7
7
. replace ( / ' / g, "\\'" ) + "'"
8
8
}
9
9
10
- function test ( ) {
10
+ function test ( includeExampleData ) {
11
11
const testCases = [
12
12
{
13
- input : '{"SourceCode": ""}' ,
13
+ input : '{"SourceCode": "exampleDataHere "}' ,
14
14
expected :
15
- 'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode"`\n}'
15
+ 'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode"`\n}' ,
16
+ expectedWithExample :
17
+ 'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode" example:"exampleDataHere"`\n}' ,
16
18
} ,
17
19
{
18
- input : '{"source_code": ""}' ,
20
+ input : '{"source_code": "exampleDataHere "}' ,
19
21
expected :
20
- 'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}'
21
- } ,
22
+ 'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}' ,
23
+ expectedWithExample :
24
+ 'type AutoGenerated struct {\n\tSourceCode string `json:"source_code" example:"exampleDataHere"`\n}'
25
+ } ,
22
26
{
23
- input : '{"sourceCode": ""}' ,
27
+ input : '{"sourceCode": "exampleDataHere "}' ,
24
28
expected :
25
- 'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}'
26
- } ,
29
+ 'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}' ,
30
+ expectedWithExample :
31
+ 'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode" example:"exampleDataHere"`\n}' } ,
27
32
{
28
33
input : '{"SOURCE_CODE": ""}' ,
29
34
expected :
35
+ 'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}' ,
36
+ expectedWithExample :
30
37
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}'
31
38
} ,
32
39
{
33
40
input : '{"PublicIP": ""}' ,
34
41
expected :
35
- 'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}'
42
+ 'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}' ,
43
+ expectedWithExample :
44
+ 'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}'
36
45
} ,
37
46
{
38
47
input : '{"public_ip": ""}' ,
39
48
expected :
49
+ 'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}' ,
50
+ expectedWithExample :
40
51
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}'
41
52
} ,
42
53
{
43
54
input : '{"publicIP": ""}' ,
44
55
expected :
56
+ 'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}' ,
57
+ expectedWithExample :
45
58
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}'
46
59
} ,
47
60
{
48
61
input : '{"PUBLIC_IP": ""}' ,
49
62
expected :
63
+ 'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}' ,
64
+ expectedWithExample :
50
65
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}'
51
66
} ,
52
67
{
53
68
input : '{"+1": "Fails", "-1": "This should not cause duplicate field name"}' ,
54
69
expected :
55
- 'type AutoGenerated struct {\n\tNum1 string `json:"+1"`\n\tNum10 string `json:"-1"`\n}'
56
- }
57
- ] ;
70
+ 'type AutoGenerated struct {\n\tNum1 string `json:"+1"`\n\tNum10 string `json:"-1"`\n}' ,
71
+ expectedWithExample :
72
+ 'type AutoGenerated struct {\n\tNum1 string `json:"+1" example:"Fails"`\n\tNum10 string `json:"-1" example:"This should not cause duplicate field name"`\n}'
73
+ } ,
74
+ {
75
+ input : '{"age": 46}' ,
76
+ expected :
77
+ 'type AutoGenerated struct {\n\tAge int `json:"age"`\n}' ,
78
+ expectedWithExample :
79
+ 'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}'
80
+ } ,
81
+ {
82
+ input : '{"topLevel": { "secondLevel": "exampleDataHere"} }' ,
83
+ expected :
84
+ 'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel"`\n\t} `json:"topLevel"`\n}' ,
85
+ expectedWithExample :
86
+ 'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel" example:"exampleDataHere"`\n\t} `json:"topLevel"`\n}'
87
+ } ,
88
+ {
89
+ input : '{"people": [{ "name": "Frank"}, {"name": "Dennis"}, {"name": "Dee"}, {"name": "Charley"}, {"name":"Mac"}] }' ,
90
+ expected :
91
+ 'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name"`\n\t} `json:"people"`\n}' ,
92
+ expectedWithExample :
93
+ 'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name" example:"Frank"`\n\t} `json:"people"`\n}'
94
+ } ] ;
58
95
59
96
for ( const testCase of testCases ) {
60
- const got = jsonToGo ( testCase . input ) ;
97
+ const got = jsonToGo ( testCase . input , null , null , includeExampleData ) ;
61
98
if ( got . error ) {
62
99
console . assert ( ! got . error , `format('${ testCase . input } '): ${ got . error } ` ) ;
63
100
} else {
101
+ exp = includeExampleData ? testCase . expectedWithExample : testCase . expected
64
102
console . assert (
65
- got . go === testCase . expected ,
103
+ got . go === exp ,
66
104
`format('${ testCase . input } '): \n\tgot: ${ quote ( got . go ) } \n\twant: ${
67
- quote ( testCase . expected )
105
+ quote ( exp )
68
106
} `
69
107
) ;
70
108
}
@@ -73,4 +111,5 @@ function test() {
73
111
console . log ( "done" )
74
112
}
75
113
76
- test ( ) ;
114
+ test ( false ) ;
115
+ test ( true )
0 commit comments