Skip to content

Commit 4fad829

Browse files
committed
create new json-to-go-v2 using named parameters
1 parent c162664 commit 4fad829

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Diff for: json-to-go.js renamed to json-to-go-v2.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
/*
2-
JSON-to-Go
2+
JSON-to-Go v2
33
by Matt Holt
44
55
https://github.com/mholt/json-to-go
66
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+
function jsonToGo(json, options = {}) {
11+
const typename = options.typename === undefined || typeof options.typename !== "string" ? "AutoGenerated" : options.typename
12+
const flatten = options.flatten === undefined ? true : options.flatten
13+
const example = options.example === undefined ? false : options.example
14+
const allOmitempty = options.allOmitempty === undefined ? false : options.allOmitempty
15+
1216
let data;
1317
let scope;
1418
let go = "";
@@ -35,8 +39,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
3539
};
3640
}
3741

38-
typename = format(typename || "AutoGenerated");
39-
append(`type ${typename} `);
42+
append(`type ${format(typename)} `);
4043

4144
parseScope(scope);
4245

@@ -528,7 +531,6 @@ if (typeof module != 'undefined') {
528531
const fs = require('fs');
529532
const json = fs.readFileSync(filename, 'utf8');
530533
jsonToGoWithErrorHandling(json)
531-
return
532534
}
533535

534536
if (!filename) {
@@ -540,7 +542,6 @@ if (typeof module != 'undefined') {
540542
const json = Buffer.concat(bufs).toString('utf8')
541543
jsonToGoWithErrorHandling(json)
542544
})
543-
return
544545
}
545546
} else {
546547
module.exports = jsonToGo

0 commit comments

Comments
 (0)