Skip to content

Commit

Permalink
create new json-to-go-v2 using named parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
grische committed Jul 26, 2024
1 parent c162664 commit 4fad829
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions json-to-go.js → json-to-go-v2.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/*
JSON-to-Go
JSON-to-Go v2
by Matt Holt
https://github.com/mholt/json-to-go
A simple utility to translate JSON into a Go type definition.
*/

function jsonToGo(json, typename, flatten = true, example = false, allOmitempty = false)
{
function jsonToGo(json, options = {}) {
const typename = options.typename === undefined || typeof options.typename !== "string" ? "AutoGenerated" : options.typename
const flatten = options.flatten === undefined ? true : options.flatten
const example = options.example === undefined ? false : options.example
const allOmitempty = options.allOmitempty === undefined ? false : options.allOmitempty

let data;
let scope;
let go = "";
Expand All @@ -35,8 +39,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
};
}

typename = format(typename || "AutoGenerated");
append(`type ${typename} `);
append(`type ${format(typename)} `);

parseScope(scope);

Expand Down Expand Up @@ -528,7 +531,6 @@ if (typeof module != 'undefined') {
const fs = require('fs');
const json = fs.readFileSync(filename, 'utf8');
jsonToGoWithErrorHandling(json)
return
}

if (!filename) {
Expand All @@ -540,7 +542,6 @@ if (typeof module != 'undefined') {
const json = Buffer.concat(bufs).toString('utf8')
jsonToGoWithErrorHandling(json)
})
return
}
} else {
module.exports = jsonToGo
Expand Down

0 comments on commit 4fad829

Please sign in to comment.