Skip to content

Commit af002ea

Browse files
fix: camelcase name handling (#94)
* fix: camelcase name handling Previous change broke handling of json with camelcase. This CL changes to call toLower() iff arguments are SCREAMING_SNAKE_CASE. Also add simple test to prevent regression. * chore: cleanup the code
1 parent 1f14697 commit af002ea

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

json-to-go.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ function jsonToGo(json, typename, flatten = true)
321321
function toProperCase(str)
322322
{
323323
// ensure that the SCREAMING_SNAKE_CASE is converted to snake_case
324-
str=str.toLowerCase();
324+
if (str.match(/^[_A-Z0-9]+$/)) {
325+
str = str.toLowerCase();
326+
}
325327

326328
// https://github.com/golang/lint/blob/5614ed5bae6fb75893070bdc0996a68765fdd275/lint.go#L771-L810
327329
const commonInitialisms = [
@@ -419,4 +421,4 @@ if (typeof module != 'undefined') {
419421
} else {
420422
module.exports = jsonToGo
421423
}
422-
}
424+
}

json-to-go.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const jsonToGo = require("./json-to-go");
2+
3+
function quote(str) {
4+
return "'" + str
5+
.replace(/\t/g, "\\t")
6+
.replace(/\n/g, "\\n")
7+
.replace(/'/g, "\\'") + "'"
8+
}
9+
10+
function test() {
11+
const testCases = [
12+
{
13+
input: '{"SourceCode": ""}',
14+
expected:
15+
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode"`\n}'
16+
},
17+
{
18+
input: '{"source_code": ""}',
19+
expected:
20+
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}'
21+
},
22+
{
23+
input: '{"sourceCode": ""}',
24+
expected:
25+
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}'
26+
},
27+
{
28+
input: '{"SOURCE_CODE": ""}',
29+
expected:
30+
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}'
31+
},
32+
{
33+
input: '{"PublicIP": ""}',
34+
expected:
35+
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}'
36+
},
37+
{
38+
input: '{"public_ip": ""}',
39+
expected:
40+
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}'
41+
},
42+
{
43+
input: '{"publicIP": ""}',
44+
expected:
45+
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}'
46+
},
47+
{
48+
input: '{"PUBLIC_IP": ""}',
49+
expected:
50+
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}'
51+
}
52+
];
53+
54+
for (const testCase of testCases) {
55+
const got = jsonToGo(testCase.input);
56+
if (got.error) {
57+
console.assert(!got.error, `format('${testCase.input}'): ${got.error}`);
58+
} else {
59+
console.assert(
60+
got.go === testCase.expected,
61+
`format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${
62+
quote(testCase.expected)
63+
}`
64+
);
65+
}
66+
}
67+
68+
console.log("done")
69+
}
70+
71+
test();

0 commit comments

Comments
 (0)