Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ export class GoRenderer extends ConvenienceRenderer {
const columns: Sourcelike[][] = [];
this.forEachEnumCase(e, "none", (name, jsonName) => {
columns.push([
[name, " "],
this._options.enumTypeNameSuffix
? [name, enumName, " "]
: [name, " "],
[enumName, ' = "', stringEscape(jsonName), '"'],
]);
});
Expand Down
5 changes: 5 additions & 0 deletions packages/quicktype-core/src/language/Golang/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export const goOptions = {
'If set, all non-required objects will be tagged with ",omitempty"',
false,
),
enumTypeNameSuffix: new BooleanOption(
"enum-type-name-suffix",
"Appends the type name to enum contracts",
false,
),
};

const golangLanguageConfig = {
Expand Down
1 change: 1 addition & 0 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ export const GoLanguage: Language = {
// fields preserve null instead of being omitted.
["omit-empty.json", { "omit-empty": "true" }],
["nullable-optional-one-of.schema", { "omit-empty": "true" }],
["enum.schema", { "enum-type-name-suffix": "true" }],
],
sourceFiles: ["src/language/Golang/index.ts"],
};
Expand Down
40 changes: 40 additions & 0 deletions test/unit/golang-enum-type-name-suffix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, test } from "vitest";

import {
InputData,
JSONSchemaInput,
quicktype,
} from "../../packages/quicktype-core/src/index.js";

async function renderGo(enumTypeNameSuffix: boolean): Promise<string> {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({
name: "TreePart",
schema: JSON.stringify({
type: "string",
enum: ["BARK", "LEAF", "ROOT"],
}),
});

const inputData = new InputData();
inputData.addInput(schemaInput);

const result = await quicktype({
inputData,
lang: "go",
rendererOptions: {
"enum-type-name-suffix": enumTypeNameSuffix,
},
});
return result.lines.join("\n");
}

describe("Go enum-type-name-suffix", () => {
test("appends the enum type name to each constant when enabled", async () => {
const output = await renderGo(true);

expect(output).toMatch(/BarkTreePart\s+TreePart = "BARK"/);
expect(output).toMatch(/LeafTreePart\s+TreePart = "LEAF"/);
expect(output).toMatch(/RootTreePart\s+TreePart = "ROOT"/);
});
});
Loading