Skip to content

Commit 825f3da

Browse files
Optionally emit "example" annotation (#102)
* optionally emits "example" annotation now * oops strict checking...
1 parent a3f1f63 commit 825f3da

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

json-to-go.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
A simple utility to translate JSON into a Go type definition.
88
*/
99

10-
function jsonToGo(json, typename, flatten = true)
10+
function jsonToGo(json, typename, flatten = true, example = false)
1111
{
1212
let data;
1313
let scope;
@@ -227,6 +227,10 @@ function jsonToGo(json, typename, flatten = true)
227227
{
228228
append(',omitempty');
229229
}
230+
if (example && scope[keys[i]] !== "" && typeof scope[keys[i]] !== "object")
231+
{
232+
append('" example:"'+scope[keys[i]])
233+
}
230234
append('"`\n');
231235
}
232236
indent(--tabs);

json-to-go.test.js

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,102 @@ function quote(str) {
77
.replace(/'/g, "\\'") + "'"
88
}
99

10-
function test() {
10+
function test(includeExampleData) {
1111
const testCases = [
1212
{
13-
input: '{"SourceCode": ""}',
13+
input: '{"SourceCode": "exampleDataHere"}',
1414
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}',
1618
},
1719
{
18-
input: '{"source_code": ""}',
20+
input: '{"source_code": "exampleDataHere"}',
1921
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+
},
2226
{
23-
input: '{"sourceCode": ""}',
27+
input: '{"sourceCode": "exampleDataHere"}',
2428
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}' },
2732
{
2833
input: '{"SOURCE_CODE": ""}',
2934
expected:
35+
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}',
36+
expectedWithExample:
3037
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}'
3138
},
3239
{
3340
input: '{"PublicIP": ""}',
3441
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}'
3645
},
3746
{
3847
input: '{"public_ip": ""}',
3948
expected:
49+
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}',
50+
expectedWithExample:
4051
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}'
4152
},
4253
{
4354
input: '{"publicIP": ""}',
4455
expected:
56+
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}',
57+
expectedWithExample:
4558
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}'
4659
},
4760
{
4861
input: '{"PUBLIC_IP": ""}',
4962
expected:
63+
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}',
64+
expectedWithExample:
5065
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}'
5166
},
5267
{
5368
input: '{"+1": "Fails", "-1": "This should not cause duplicate field name"}',
5469
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+
} ];
5895

5996
for (const testCase of testCases) {
60-
const got = jsonToGo(testCase.input);
97+
const got = jsonToGo(testCase.input, null, null, includeExampleData);
6198
if (got.error) {
6299
console.assert(!got.error, `format('${testCase.input}'): ${got.error}`);
63100
} else {
101+
exp = includeExampleData ? testCase.expectedWithExample : testCase.expected
64102
console.assert(
65-
got.go === testCase.expected,
103+
got.go === exp,
66104
`format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${
67-
quote(testCase.expected)
105+
quote(exp)
68106
}`
69107
);
70108
}
@@ -73,4 +111,5 @@ function test() {
73111
console.log("done")
74112
}
75113

76-
test();
114+
test(false);
115+
test(true)

0 commit comments

Comments
 (0)