Skip to content

Commit a3e7457

Browse files
committed
add jsonToGov2() with named parameters
1 parent c162664 commit a3e7457

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

json-to-go.js

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

10-
function jsonToGo(json, typename, flatten = true, example = false, allOmitempty = false)
11-
{
10+
// for backwards compatibility
11+
function jsonToGo(json, typename = "AutoGenerated", flatten = true, example = false, allOmitempty = false) {
12+
return jsonToGov2(
13+
json,
14+
{
15+
typename: typename,
16+
flatten: flatten,
17+
example: example,
18+
allOmitempty: allOmitempty,
19+
})
20+
}
21+
22+
// new jsonToGo interface
23+
function jsonToGov2(json, options = {}) {
24+
const typename = format(options.typename || "AutoGenerated")
25+
const flatten = options.flatten === undefined ? true : options.flatten
26+
const example = options.example === undefined ? false : options.example
27+
const allOmitempty = options.allOmitempty === undefined ? false : options.allOmitempty
28+
1229
let data;
1330
let scope;
1431
let go = "";
@@ -35,7 +52,6 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
3552
};
3653
}
3754

38-
typename = format(typename || "AutoGenerated");
3955
append(`type ${typename} `);
4056

4157
parseScope(scope);
@@ -498,7 +514,7 @@ if (typeof module != 'undefined') {
498514
let filename = null
499515

500516
function jsonToGoWithErrorHandling(json) {
501-
const output = jsonToGo(json)
517+
const output = jsonToGov2(json)
502518
if (output.error) {
503519
console.error(output.error)
504520
process.exitCode = 1

0 commit comments

Comments
 (0)