Skip to content

Commit 0f50468

Browse files
committed
add backwards-compatible json-to-go.js
1 parent 4fad829 commit 0f50468

File tree

2 files changed

+83
-43
lines changed

2 files changed

+83
-43
lines changed

json-to-go-v2.js

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -496,54 +496,54 @@ function jsonToGo(json, options = {}) {
496496
}
497497
}
498498

499-
if (typeof module != 'undefined') {
500-
if (!module.parent) {
501-
let filename = null
502-
503-
function jsonToGoWithErrorHandling(json) {
504-
const output = jsonToGo(json)
505-
if (output.error) {
506-
console.error(output.error)
507-
process.exitCode = 1
508-
}
509-
process.stdout.write(output.go)
499+
if (typeof module === 'undefined' || !module.parent) {
500+
let filename = null
501+
502+
function jsonToGoWithErrorHandling(json) {
503+
const output = jsonToGo(json)
504+
if (output.error) {
505+
console.error(output.error)
506+
process.exitCode = 1
510507
}
508+
process.stdout.write(output.go)
509+
}
511510

512-
process.argv.forEach((val, index) => {
513-
if (index < 2)
514-
return
515-
516-
if (!val.startsWith('-')) {
517-
filename = val
518-
return
519-
}
520-
521-
const argument = val.replace(/-/g, '')
522-
if (argument === "big")
523-
console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`)
524-
else {
525-
console.error(`Unexpected argument ${val} received`)
526-
process.exit(1)
527-
}
528-
})
511+
process.argv.forEach((val, index) => {
512+
if (index < 2)
513+
return
529514

530-
if (filename) {
531-
const fs = require('fs');
532-
const json = fs.readFileSync(filename, 'utf8');
533-
jsonToGoWithErrorHandling(json)
515+
if (!val.startsWith('-')) {
516+
filename = val
517+
return
534518
}
535519

536-
if (!filename) {
537-
bufs = []
538-
process.stdin.on('data', function(buf) {
539-
bufs.push(buf)
540-
})
541-
process.stdin.on('end', function() {
542-
const json = Buffer.concat(bufs).toString('utf8')
543-
jsonToGoWithErrorHandling(json)
544-
})
520+
const argument = val.replace(/-/g, '')
521+
if (argument === "big")
522+
console.warn(`Warning: The argument '${argument}' has been deprecated and has no effect anymore`)
523+
else {
524+
console.error(`Unexpected argument ${val} received`)
525+
process.exit(1)
545526
}
546-
} else {
547-
module.exports = jsonToGo
527+
})
528+
529+
if (filename) {
530+
const fs = require('fs');
531+
const json = fs.readFileSync(filename, 'utf8');
532+
jsonToGoWithErrorHandling(json)
533+
}
534+
535+
if (!filename) {
536+
bufs = []
537+
process.stdin.on('data', function (buf) {
538+
bufs.push(buf)
539+
})
540+
process.stdin.on('end', function () {
541+
const json = Buffer.concat(bufs).toString('utf8')
542+
jsonToGoWithErrorHandling(json)
543+
})
548544
}
549545
}
546+
547+
if (typeof module !== 'undefined') {
548+
module.exports = jsonToGo
549+
}

json-to-go.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
JSON-to-Go
3+
by Matt Holt
4+
5+
https://github.com/mholt/json-to-go
6+
7+
A backwards compatible integration of json-to-go-v2
8+
*/
9+
10+
const jsonToGov2 = require('./json-to-go-v2')
11+
12+
// for backwards compatibility
13+
function jsonToGo(json, typename = "AutoGenerated", flatten = true, example = false, allOmitempty = false) {
14+
return jsonToGov2(
15+
json,
16+
{
17+
typename: typename,
18+
flatten: flatten,
19+
example: example,
20+
allOmitempty: allOmitempty,
21+
})
22+
}
23+
24+
if (typeof module === 'undefined' || !module.parent) {
25+
// being able to run json-to-go backwards compatible
26+
const vm = require('vm');
27+
const fs = require('fs');
28+
const jscode = fs.readFileSync('./json-to-go-v2.js');
29+
const context = {
30+
Buffer,
31+
console,
32+
process,
33+
require,
34+
};
35+
vm.runInNewContext(jscode, context);
36+
}
37+
38+
if (typeof module !== 'undefined') {
39+
module.exports = jsonToGo
40+
}

0 commit comments

Comments
 (0)