mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-08 07:21:02 -04:00
test pass
This commit is contained in:
parent
5c4f8e5d87
commit
7f2355b782
5 changed files with 78 additions and 11 deletions
|
@ -3,12 +3,6 @@
|
||||||
"name": "Favourites",
|
"name": "Favourites",
|
||||||
"ops": []
|
"ops": []
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "Wangkechun",
|
|
||||||
"ops": [
|
|
||||||
"JSON To Go Struct"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "Data format",
|
"name": "Data format",
|
||||||
"ops": [
|
"ops": [
|
||||||
|
@ -420,6 +414,7 @@
|
||||||
"JavaScript Minify",
|
"JavaScript Minify",
|
||||||
"JSON Beautify",
|
"JSON Beautify",
|
||||||
"JSON Minify",
|
"JSON Minify",
|
||||||
|
"JSON to Go Struct",
|
||||||
"XML Beautify",
|
"XML Beautify",
|
||||||
"XML Minify",
|
"XML Minify",
|
||||||
"SQL Beautify",
|
"SQL Beautify",
|
||||||
|
|
|
@ -5,12 +5,12 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import { jsonToGo } from "../lib/JSONToGoStruct.mjs";
|
import { jsonToGo } from "../vendor/JSONToGoStruct.mjs";
|
||||||
import JSON5 from "json5";
|
import JSON5 from "json5";
|
||||||
import OperationError from "../errors/OperationError.mjs";
|
import OperationError from "../errors/OperationError.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON To Go Struct operation
|
* JSON to Go Struct operation
|
||||||
*/
|
*/
|
||||||
class JSONToGoStruct extends Operation {
|
class JSONToGoStruct extends Operation {
|
||||||
/**
|
/**
|
||||||
|
@ -19,7 +19,7 @@ class JSONToGoStruct extends Operation {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.name = "JSON To Go Struct";
|
this.name = "JSON to Go Struct";
|
||||||
this.module = "Default";
|
this.module = "Default";
|
||||||
this.description = "converts JSON into a Go type definition.";
|
this.description = "converts JSON into a Go type definition.";
|
||||||
this.infoURL = "https://mholt.github.io/json-to-go/";
|
this.infoURL = "https://mholt.github.io/json-to-go/";
|
||||||
|
@ -34,7 +34,7 @@ class JSONToGoStruct extends Operation {
|
||||||
{
|
{
|
||||||
name: "Flatten",
|
name: "Flatten",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: false,
|
value: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "All Omit Empty",
|
name: "All Omit Empty",
|
||||||
|
@ -59,7 +59,7 @@ class JSONToGoStruct extends Operation {
|
||||||
throw new OperationError("Unable to parse input as JSON.\n" + err);
|
throw new OperationError("Unable to parse input as JSON.\n" + err);
|
||||||
}
|
}
|
||||||
const result = jsonToGo(code, typename, flatten, false, allOmitempty);
|
const result = jsonToGo(code, typename, flatten, false, allOmitempty);
|
||||||
return result["go"];
|
return result.go;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -171,6 +171,9 @@ export function jsonToGo(
|
||||||
++innerTabs;
|
++innerTabs;
|
||||||
const keys = Object.keys(scope);
|
const keys = Object.keys(scope);
|
||||||
for (let i in keys) {
|
for (let i in keys) {
|
||||||
|
if (!Object.prototype.hasOwnProperty.call(keys, i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const keyname = getOriginalName(keys[i]);
|
const keyname = getOriginalName(keys[i]);
|
||||||
indenter(innerTabs);
|
indenter(innerTabs);
|
||||||
const typename = uniqueTypeName(format(keyname), seenTypeNames);
|
const typename = uniqueTypeName(format(keyname), seenTypeNames);
|
||||||
|
@ -195,6 +198,9 @@ export function jsonToGo(
|
||||||
++tabs;
|
++tabs;
|
||||||
const keys = Object.keys(scope);
|
const keys = Object.keys(scope);
|
||||||
for (let i in keys) {
|
for (let i in keys) {
|
||||||
|
if (!Object.prototype.hasOwnProperty.call(keys, i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const keyname = getOriginalName(keys[i]);
|
const keyname = getOriginalName(keys[i]);
|
||||||
indent(tabs);
|
indent(tabs);
|
||||||
const typename = uniqueTypeName(format(keyname), seenTypeNames);
|
const typename = uniqueTypeName(format(keyname), seenTypeNames);
|
||||||
|
@ -450,6 +456,9 @@ export function jsonToGo(
|
||||||
|
|
||||||
function formatScopeKeys(keys) {
|
function formatScopeKeys(keys) {
|
||||||
for (let i in keys) {
|
for (let i in keys) {
|
||||||
|
if (!Object.prototype.hasOwnProperty.call(keys, i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
keys[i] = format(keys[i]);
|
keys[i] = format(keys[i]);
|
||||||
}
|
}
|
||||||
return keys;
|
return keys;
|
|
@ -59,6 +59,7 @@ import "./tests/Jump.mjs";
|
||||||
import "./tests/JSONBeautify.mjs";
|
import "./tests/JSONBeautify.mjs";
|
||||||
import "./tests/JSONMinify.mjs";
|
import "./tests/JSONMinify.mjs";
|
||||||
import "./tests/JSONtoCSV.mjs";
|
import "./tests/JSONtoCSV.mjs";
|
||||||
|
import "./tests/JSONToGoStruct.mjs";
|
||||||
import "./tests/JWTDecode.mjs";
|
import "./tests/JWTDecode.mjs";
|
||||||
import "./tests/JWTSign.mjs";
|
import "./tests/JWTSign.mjs";
|
||||||
import "./tests/JWTVerify.mjs";
|
import "./tests/JWTVerify.mjs";
|
||||||
|
|
62
tests/operations/tests/JSONToGoStruct.mjs
Normal file
62
tests/operations/tests/JSONToGoStruct.mjs
Normal 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],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
Loading…
Add table
Add a link
Reference in a new issue