Skip to content

Commit b271f84

Browse files
authored
add final newline (#128)
replace console.log with stdout.write to avoid creating multiple newlines
1 parent 086b5b2 commit b271f84

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

json-to-go.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
3838

3939
parseScope(scope);
4040

41+
if (flatten)
42+
go += accumulator
43+
44+
// add final newline for POSIX 3.206
45+
if (!go.endsWith(`\n`))
46+
go += `\n`
47+
4148
return {
42-
go: flatten
43-
? go += accumulator
44-
: go
49+
go: go
4550
};
4651

4752

@@ -452,12 +457,12 @@ if (typeof module != 'undefined') {
452457
})
453458
process.stdin.on('end', function() {
454459
const json = Buffer.concat(bufs).toString('utf8')
455-
console.log(jsonToGo(json).go)
460+
process.stdout.write(jsonToGo(json).go)
456461
})
457462
} else {
458463
process.stdin.on('data', function(buf) {
459464
const json = buf.toString('utf8')
460-
console.log(jsonToGo(json).go)
465+
process.stdout.write(jsonToGo(json).go)
461466
})
462467
}
463468
} else {

json-to-go.test.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,128 +12,130 @@ function test(includeExampleData) {
1212
{
1313
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}\n',
1616
expectedWithExample:
17-
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode" example:"exampleDataHere"`\n}',
17+
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode" example:"exampleDataHere"`\n}\n',
1818
},
1919
{
2020
input: '{"source_code": "exampleDataHere"}',
2121
expected:
22-
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}',
22+
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}\n',
2323
expectedWithExample:
24-
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code" example:"exampleDataHere"`\n}'
25-
},
24+
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code" example:"exampleDataHere"`\n}\n',
25+
},
2626
{
2727
input: '{"sourceCode": "exampleDataHere"}',
2828
expected:
29-
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}',
29+
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}\n',
3030
expectedWithExample:
31-
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode" example:"exampleDataHere"`\n}' },
31+
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode" example:"exampleDataHere"`\n}\n',
32+
},
3233
{
3334
input: '{"SOURCE_CODE": ""}',
3435
expected:
35-
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}',
36+
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}\n',
3637
expectedWithExample:
37-
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}'
38+
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}\n',
3839
},
3940
{
4041
input: '{"PublicIP": ""}',
4142
expected:
42-
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}',
43+
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}\n',
4344
expectedWithExample:
44-
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}'
45+
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}\n',
4546
},
4647
{
4748
input: '{"public_ip": ""}',
4849
expected:
49-
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}',
50+
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}\n',
5051
expectedWithExample:
51-
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}'
52+
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}\n',
5253
},
5354
{
5455
input: '{"publicIP": ""}',
5556
expected:
56-
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}',
57+
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}\n',
5758
expectedWithExample:
58-
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}'
59+
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}\n',
5960
},
6061
{
6162
input: '{"PUBLIC_IP": ""}',
6263
expected:
63-
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}',
64+
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}\n',
6465
expectedWithExample:
65-
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}'
66+
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}\n',
6667
},
6768
{
6869
input: '{"+1": "Fails", "-1": "This should not cause duplicate field name"}',
6970
expected:
70-
'type AutoGenerated struct {\n\tNum1 string `json:"+1"`\n\tNum10 string `json:"-1"`\n}',
71+
'type AutoGenerated struct {\n\tNum1 string `json:"+1"`\n\tNum10 string `json:"-1"`\n}\n',
7172
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+
'type AutoGenerated struct {\n\tNum1 string `json:"+1" example:"Fails"`\n\tNum10 string `json:"-1" example:"This should not cause duplicate field name"`\n}\n',
7374
},
7475
{
7576
input: '{"age": 46}',
7677
expected:
77-
'type AutoGenerated struct {\n\tAge int `json:"age"`\n}',
78+
'type AutoGenerated struct {\n\tAge int `json:"age"`\n}\n',
7879
expectedWithExample:
79-
'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}'
80+
'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}\n',
8081
},
8182
{
8283
input: '{"negativeFloat": -1.00}',
8384
expected:
84-
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}',
85+
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}\n',
8586
expectedWithExample:
86-
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.1"`\n}'
87+
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.1"`\n}\n',
8788
},
8889
{
8990
input: '{"zeroFloat": 0.00}',
9091
expected:
91-
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}',
92+
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}\n',
9293
expectedWithExample:
93-
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}'
94+
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}\n',
9495
},
9596
{
9697
input: '{"positiveFloat": 1.00}',
9798
expected:
98-
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}',
99+
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}\n',
99100
expectedWithExample:
100-
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}'
101+
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}\n',
101102
},
102103
{
103104
input: '{"negativeFloats": [-1.00, -2.00, -3.00]}',
104105
expected:
105-
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}',
106+
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}\n',
106107
expectedWithExample:
107-
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}'
108+
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}\n',
108109
},
109110
{
110111
input: '{"zeroFloats": [0.00, 0.00, 0.00]}',
111112
expected:
112-
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}',
113+
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}\n',
113114
expectedWithExample:
114-
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}'
115+
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}\n',
115116
},
116117
{
117118
input: '{"positiveFloats": [1.00, 2.00, 3.00]}',
118119
expected:
119-
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}',
120+
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}\n',
120121
expectedWithExample:
121-
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}'
122+
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}\n',
122123
},
123124
{
124125
input: '{"topLevel": { "secondLevel": "exampleDataHere"} }',
125126
expected:
126-
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel"`\n\t} `json:"topLevel"`\n}',
127+
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel"`\n\t} `json:"topLevel"`\n}\n',
127128
expectedWithExample:
128-
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel" example:"exampleDataHere"`\n\t} `json:"topLevel"`\n}'
129+
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel" example:"exampleDataHere"`\n\t} `json:"topLevel"`\n}\n',
129130
},
130131
{
131132
input: '{"people": [{ "name": "Frank"}, {"name": "Dennis"}, {"name": "Dee"}, {"name": "Charley"}, {"name":"Mac"}] }',
132133
expected:
133-
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name"`\n\t} `json:"people"`\n}',
134+
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name"`\n\t} `json:"people"`\n}\n',
134135
expectedWithExample:
135-
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name" example:"Frank"`\n\t} `json:"people"`\n}'
136-
} ];
136+
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name" example:"Frank"`\n\t} `json:"people"`\n}\n',
137+
},
138+
];
137139

138140
for (const testCase of testCases) {
139141
const got = jsonToGo(testCase.input, null, null, includeExampleData);
@@ -143,9 +145,7 @@ function test(includeExampleData) {
143145
exp = includeExampleData ? testCase.expectedWithExample : testCase.expected
144146
console.assert(
145147
got.go === exp,
146-
`format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${
147-
quote(exp)
148-
}`
148+
`format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${quote(exp)}`
149149
);
150150
}
151151
}
@@ -154,4 +154,4 @@ function test(includeExampleData) {
154154
}
155155

156156
test(false);
157-
test(true)
157+
test(true)

0 commit comments

Comments
 (0)