diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 79664d35..90406c5c 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -3,12 +3,6 @@ "name": "Favourites", "ops": [] }, - { - "name": "Wangkechun", - "ops": [ - "JSON To Go Struct" - ] - }, { "name": "Data format", "ops": [ @@ -420,6 +414,7 @@ "JavaScript Minify", "JSON Beautify", "JSON Minify", + "JSON to Go Struct", "XML Beautify", "XML Minify", "SQL Beautify", diff --git a/src/core/operations/JSONToGoStruct.mjs b/src/core/operations/JSONToGoStruct.mjs index 4f2e4ddd..fce2b665 100644 --- a/src/core/operations/JSONToGoStruct.mjs +++ b/src/core/operations/JSONToGoStruct.mjs @@ -5,12 +5,12 @@ */ import Operation from "../Operation.mjs"; -import { jsonToGo } from "../lib/JSONToGoStruct.mjs"; +import { jsonToGo } from "../vendor/JSONToGoStruct.mjs"; import JSON5 from "json5"; import OperationError from "../errors/OperationError.mjs"; /** - * JSON To Go Struct operation + * JSON to Go Struct operation */ class JSONToGoStruct extends Operation { /** @@ -19,7 +19,7 @@ class JSONToGoStruct extends Operation { constructor() { super(); - this.name = "JSON To Go Struct"; + this.name = "JSON to Go Struct"; this.module = "Default"; this.description = "converts JSON into a Go type definition."; this.infoURL = "https://mholt.github.io/json-to-go/"; @@ -34,7 +34,7 @@ class JSONToGoStruct extends Operation { { name: "Flatten", type: "boolean", - value: false, + value: true, }, { name: "All Omit Empty", @@ -59,7 +59,7 @@ class JSONToGoStruct extends Operation { throw new OperationError("Unable to parse input as JSON.\n" + err); } const result = jsonToGo(code, typename, flatten, false, allOmitempty); - return result["go"]; + return result.go; } } diff --git a/src/core/lib/JSONToGoStruct.mjs b/src/core/vendor/JSONToGoStruct.mjs similarity index 97% rename from src/core/lib/JSONToGoStruct.mjs rename to src/core/vendor/JSONToGoStruct.mjs index 2a3de4a2..ff38907b 100644 --- a/src/core/lib/JSONToGoStruct.mjs +++ b/src/core/vendor/JSONToGoStruct.mjs @@ -171,6 +171,9 @@ export function jsonToGo( ++innerTabs; const keys = Object.keys(scope); for (let i in keys) { + if (!Object.prototype.hasOwnProperty.call(keys, i)) { + continue; + } const keyname = getOriginalName(keys[i]); indenter(innerTabs); const typename = uniqueTypeName(format(keyname), seenTypeNames); @@ -195,6 +198,9 @@ export function jsonToGo( ++tabs; const keys = Object.keys(scope); for (let i in keys) { + if (!Object.prototype.hasOwnProperty.call(keys, i)) { + continue; + } const keyname = getOriginalName(keys[i]); indent(tabs); const typename = uniqueTypeName(format(keyname), seenTypeNames); @@ -450,6 +456,9 @@ export function jsonToGo( function formatScopeKeys(keys) { for (let i in keys) { + if (!Object.prototype.hasOwnProperty.call(keys, i)) { + continue; + } keys[i] = format(keys[i]); } return keys; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 570fbb6f..25a4f125 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -59,6 +59,7 @@ import "./tests/Jump.mjs"; import "./tests/JSONBeautify.mjs"; import "./tests/JSONMinify.mjs"; import "./tests/JSONtoCSV.mjs"; +import "./tests/JSONToGoStruct.mjs"; import "./tests/JWTDecode.mjs"; import "./tests/JWTSign.mjs"; import "./tests/JWTVerify.mjs"; diff --git a/tests/operations/tests/JSONToGoStruct.mjs b/tests/operations/tests/JSONToGoStruct.mjs new file mode 100644 index 00000000..8b8f2f08 --- /dev/null +++ b/tests/operations/tests/JSONToGoStruct.mjs @@ -0,0 +1,62 @@ +/** + * JSON to Go Struct tests. + * + * @author wangkechun [hi@hi-hi.cn] + * + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "JSON to Go Struct: simple", + input: JSON.stringify({ a: "1", b: "2", c: "3" }), + expectedOutput: `type AutoGenerated struct { + A string \`json:"a"\` + B string \`json:"b"\` + C string \`json:"c"\` +}`.replaceAll(" ", "\t"), + recipeConfig: [ + { + op: "JSON to Go Struct", + args: ["AutoGenerated", true, false], + }, + ], + }, + { + name: "JSON to Go Struct: flatten", + input: JSON.stringify({ a: "1", b: "2", c: { d: "e" } }), + expectedOutput: `type AutoGenerated struct { + A string \`json:"a"\` + B string \`json:"b"\` + C C \`json:"c"\` +} +type C struct { + D string \`json:"d"\` +}`.replaceAll(" ", "\t"), + recipeConfig: [ + { + op: "JSON to Go Struct", + args: ["AutoGenerated", true, false], + }, + ], + }, + { + name: "JSON to Go Struct: nest", + input: JSON.stringify({ a: "1", b: "2", c: { d: "e" } }), + expectedOutput: `type AutoGenerated struct { + A string \`json:"a"\` + B string \`json:"b"\` + C struct { + D string \`json:"d"\` + } \`json:"c"\` +}`.replaceAll(" ", "\t"), + recipeConfig: [ + { + op: "JSON to Go Struct", + args: ["AutoGenerated", false, false], + }, + ], + }, +]);