From 4fad829245d692386fc4a3a24b75e93f9c55a941 Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 13:34:41 +0200 Subject: [PATCH 1/6] create new json-to-go-v2 using named parameters --- json-to-go.js => json-to-go-v2.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) rename json-to-go.js => json-to-go-v2.js (96%) diff --git a/json-to-go.js b/json-to-go-v2.js similarity index 96% rename from json-to-go.js rename to json-to-go-v2.js index 870ce0b..27e471a 100644 --- a/json-to-go.js +++ b/json-to-go-v2.js @@ -1,5 +1,5 @@ /* - JSON-to-Go + JSON-to-Go v2 by Matt Holt https://github.com/mholt/json-to-go @@ -7,8 +7,12 @@ 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 = ""; @@ -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); @@ -528,7 +531,6 @@ if (typeof module != 'undefined') { const fs = require('fs'); const json = fs.readFileSync(filename, 'utf8'); jsonToGoWithErrorHandling(json) - return } if (!filename) { @@ -540,7 +542,6 @@ if (typeof module != 'undefined') { const json = Buffer.concat(bufs).toString('utf8') jsonToGoWithErrorHandling(json) }) - return } } else { module.exports = jsonToGo From 0f504683d468d866c3939f2ea4358b4c2b35463f Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 20:24:11 +0200 Subject: [PATCH 2/6] add backwards-compatible json-to-go.js --- json-to-go-v2.js | 86 ++++++++++++++++++++++++------------------------ json-to-go.js | 40 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 json-to-go.js diff --git a/json-to-go-v2.js b/json-to-go-v2.js index 27e471a..05cffa5 100644 --- a/json-to-go-v2.js +++ b/json-to-go-v2.js @@ -496,54 +496,54 @@ function jsonToGo(json, options = {}) { } } -if (typeof module != 'undefined') { - if (!module.parent) { - let filename = null - - function jsonToGoWithErrorHandling(json) { - const output = jsonToGo(json) - if (output.error) { - console.error(output.error) - process.exitCode = 1 - } - process.stdout.write(output.go) +if (typeof module === 'undefined' || !module.parent) { + let filename = null + + function jsonToGoWithErrorHandling(json) { + const output = jsonToGo(json) + if (output.error) { + console.error(output.error) + process.exitCode = 1 } + process.stdout.write(output.go) + } - process.argv.forEach((val, index) => { - if (index < 2) - return - - if (!val.startsWith('-')) { - filename = val - return - } - - const argument = val.replace(/-/g, '') - if (argument === "big") - console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`) - else { - console.error(`Unexpected argument ${val} received`) - process.exit(1) - } - }) + process.argv.forEach((val, index) => { + if (index < 2) + return - if (filename) { - const fs = require('fs'); - const json = fs.readFileSync(filename, 'utf8'); - jsonToGoWithErrorHandling(json) + if (!val.startsWith('-')) { + filename = val + return } - if (!filename) { - bufs = [] - process.stdin.on('data', function(buf) { - bufs.push(buf) - }) - process.stdin.on('end', function() { - const json = Buffer.concat(bufs).toString('utf8') - jsonToGoWithErrorHandling(json) - }) + const argument = val.replace(/-/g, '') + if (argument === "big") + console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`) + else { + console.error(`Unexpected argument ${val} received`) + process.exit(1) } - } else { - module.exports = jsonToGo + }) + + if (filename) { + const fs = require('fs'); + const json = fs.readFileSync(filename, 'utf8'); + jsonToGoWithErrorHandling(json) + } + + if (!filename) { + bufs = [] + process.stdin.on('data', function (buf) { + bufs.push(buf) + }) + process.stdin.on('end', function () { + const json = Buffer.concat(bufs).toString('utf8') + jsonToGoWithErrorHandling(json) + }) } } + +if (typeof module !== 'undefined') { + module.exports = jsonToGo +} diff --git a/json-to-go.js b/json-to-go.js new file mode 100644 index 0000000..ac665de --- /dev/null +++ b/json-to-go.js @@ -0,0 +1,40 @@ +/* + JSON-to-Go + by Matt Holt + + https://github.com/mholt/json-to-go + + A backwards compatible integration of json-to-go-v2 +*/ + +const jsonToGov2 = require('./json-to-go-v2') + +// for backwards compatibility +function jsonToGo(json, typename = "AutoGenerated", flatten = true, example = false, allOmitempty = false) { + return jsonToGov2( + json, + { + typename: typename, + flatten: flatten, + example: example, + allOmitempty: allOmitempty, + }) +} + +if (typeof module === 'undefined' || !module.parent) { + // being able to run json-to-go backwards compatible + const vm = require('vm'); + const fs = require('fs'); + const jscode = fs.readFileSync('./json-to-go-v2.js'); + const context = { + Buffer, + console, + process, + require, + }; + vm.runInNewContext(jscode, context); +} + +if (typeof module !== 'undefined') { + module.exports = jsonToGo +} From 68b69637bade3e52c2167a54bf7b2eb6fa884c89 Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 13:37:41 +0200 Subject: [PATCH 3/6] use switch/case for argument detection --- json-to-go-v2.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/json-to-go-v2.js b/json-to-go-v2.js index 05cffa5..afe8753 100644 --- a/json-to-go-v2.js +++ b/json-to-go-v2.js @@ -518,11 +518,13 @@ if (typeof module === 'undefined' || !module.parent) { } const argument = val.replace(/-/g, '') - if (argument === "big") - console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`) - else { - console.error(`Unexpected argument ${val} received`) - process.exit(1) + switch (argument) { + case "big": + console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`) + break + default: + console.error(`Unexpected argument ${val} received`) + process.exit(1) } }) From 0fa4579c72832b92f26f1812bac5282f4d0fd4af Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 14:15:11 +0200 Subject: [PATCH 4/6] add all jsonToGo options as program arguments --- json-to-go-v2.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/json-to-go-v2.js b/json-to-go-v2.js index afe8753..0150fca 100644 --- a/json-to-go-v2.js +++ b/json-to-go-v2.js @@ -498,9 +498,10 @@ function jsonToGo(json, options = {}) { if (typeof module === 'undefined' || !module.parent) { let filename = null + let options = {} - function jsonToGoWithErrorHandling(json) { - const output = jsonToGo(json) + function jsonToGoWithErrorHandling(json, options) { + const output = jsonToGo(json, options) if (output.error) { console.error(output.error) process.exitCode = 1 @@ -517,11 +518,32 @@ if (typeof module === 'undefined' || !module.parent) { return } - const argument = val.replace(/-/g, '') - switch (argument) { + let argument = { + arg: val.split("=")[0].replace(/^-+/, ''), + value: val.split("=")[1] || true, + } + + if (argument.arg.startsWith("no-")) { + argument.arg = argument.arg.replace(/^no-/, '') + argument.value = !argument.value + } + + switch (argument.arg) { case "big": console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`) break + case "typename": + options.typename = argument.value + break + case "flatten": + options.flatten = argument.value + break + case "examples": + options.example = argument.value + break + case "all-omitempty": + options.allOmitempty = argument.value + break default: console.error(`Unexpected argument ${val} received`) process.exit(1) @@ -531,7 +553,7 @@ if (typeof module === 'undefined' || !module.parent) { if (filename) { const fs = require('fs'); const json = fs.readFileSync(filename, 'utf8'); - jsonToGoWithErrorHandling(json) + jsonToGoWithErrorHandling(json, options) } if (!filename) { @@ -541,7 +563,7 @@ if (typeof module === 'undefined' || !module.parent) { }) process.stdin.on('end', function () { const json = Buffer.concat(bufs).toString('utf8') - jsonToGoWithErrorHandling(json) + jsonToGoWithErrorHandling(json, options) }) } } From bfc29a7db4c7146bbdf1296615c829c28c1b9d72 Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 18:52:06 +0200 Subject: [PATCH 5/6] add help command-line argument --- README.md | 6 ++++++ json-to-go-v2.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 35e85c9..3bf4b8a 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ Contributions are welcome! Open a pull request to fix a bug, or open an issue to cat sample.json | node json-to-go.js ``` +- For more options, check the help page + + ```sh + node json-to-go.js --help + ``` + ### Credits JSON-to-Go is brought to you by Matt Holt ([mholt6](https://twitter.com/mholt6)). diff --git a/json-to-go-v2.js b/json-to-go-v2.js index 0150fca..eb2d551 100644 --- a/json-to-go-v2.js +++ b/json-to-go-v2.js @@ -509,6 +509,25 @@ if (typeof module === 'undefined' || !module.parent) { process.stdout.write(output.go) } + function printHelp() { + const path = require('path'); + const scriptname = path.basename(process.argv[1]) + + console.log(`\ +Usage: node ${scriptname} [OPTION]... [FILE] +Convert json to go file and prints the result on stdout. + +Optional arguments: + --help Print this help page + --all-omitempty Make all fields "omitempty" (Default: false) + --examples Add examples to go struct. Currently only works without flatten. (Default: false) + --flatten Flatten go struct (Default: true) + --typename=NAME Use a specific name for typename (Default: "AutoGenerated") + +All arguments can be inverted by specifing "--no-...", for example "--no-examples". + `) + } + process.argv.forEach((val, index) => { if (index < 2) return @@ -544,8 +563,12 @@ if (typeof module === 'undefined' || !module.parent) { case "all-omitempty": options.allOmitempty = argument.value break + case "help": + printHelp() + process.exit(0) default: console.error(`Unexpected argument ${val} received`) + printHelp() process.exit(1) } }) From 364794865e90c145ef16403eaf98f38db4abdb9c Mon Sep 17 00:00:00 2001 From: Grische Date: Fri, 26 Jul 2024 20:31:52 +0200 Subject: [PATCH 6/6] make json-to-go.js directly executable --- README.md | 12 +++++++++--- json-to-go-v2.js | 3 ++- json-to-go.js | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) mode change 100644 => 100755 json-to-go-v2.js mode change 100644 => 100755 json-to-go.js diff --git a/README.md b/README.md index 3bf4b8a..015a7df 100644 --- a/README.md +++ b/README.md @@ -20,17 +20,23 @@ Contributions are welcome! Open a pull request to fix a bug, or open an issue to node json-to-go.js sample.json ``` +- Read JSON file on Unix systems: + + ```sh + ./json-to-go.js sample.json + ``` + - Read JSON file from stdin: ```sh - node json-to-go.js < sample.json - cat sample.json | node json-to-go.js + ./json-to-go.js < sample.json + cat sample.json | ./json-to-go.js ``` - For more options, check the help page ```sh - node json-to-go.js --help + ./json-to-go.js --help ``` ### Credits diff --git a/json-to-go-v2.js b/json-to-go-v2.js old mode 100644 new mode 100755 index eb2d551..d025779 --- a/json-to-go-v2.js +++ b/json-to-go-v2.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node /* JSON-to-Go v2 by Matt Holt @@ -514,7 +515,7 @@ if (typeof module === 'undefined' || !module.parent) { const scriptname = path.basename(process.argv[1]) console.log(`\ -Usage: node ${scriptname} [OPTION]... [FILE] +Usage: ${scriptname} [OPTION]... [FILE] Convert json to go file and prints the result on stdout. Optional arguments: diff --git a/json-to-go.js b/json-to-go.js old mode 100644 new mode 100755 index ac665de..b650949 --- a/json-to-go.js +++ b/json-to-go.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node /* JSON-to-Go by Matt Holt