test pass

This commit is contained in:
wangkechun 2024-01-29 17:28:50 +08:00
parent 5c4f8e5d87
commit 7f2355b782
5 changed files with 78 additions and 11 deletions

View File

@ -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",

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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";

View File

@ -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],
},
],
},
]);