Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat JSON to Go Struct #1698

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"Avro to JSON",
"CBOR Encode",
"CBOR Decode",
"JSON to Go Struct",
"Rison Encode",
"Rison Decode"
]
Expand Down
66 changes: 66 additions & 0 deletions src/core/operations/JSONToGoStruct.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @author wangkechun [[email protected]]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import { jsonToGo } from "../vendor/JSONToGoStruct.mjs";
import JSON5 from "json5";
import OperationError from "../errors/OperationError.mjs";

/**
* JSON to Go Struct operation
*/
class JSONToGoStruct extends Operation {
/**
* JSONToGoStruct constructor
*/
constructor() {
super();

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/";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Type Name",
type: "string",
value: "AutoGenerated",
},
{
name: "Flatten",
type: "boolean",
value: true,
},
{
name: "All Omit Empty",
type: "boolean",
value: false,
},
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [typename, flatten, allOmitempty] = args;
if (!input) return "";
let code;
try {
code = JSON.stringify(JSON5.parse(input));
} catch (err) {
throw new OperationError("Unable to parse input as JSON.\n" + err);
}
const result = jsonToGo(code, typename, flatten, false, allOmitempty);
return result.go;
}
}

export default JSONToGoStruct;
Loading
Loading