Skip to content

Commit

Permalink
Fix omit empty (#2505)
Browse files Browse the repository at this point in the history
* Add omit-empty to usable options

* run Prettier
  • Loading branch information
Adam724 authored Feb 15, 2024
1 parent bb5f033 commit 916cd94
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/quicktype-core/src/language/Golang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export const goOptions = {
packageName: new StringOption("package", "Generated package name", "NAME", "main"),
multiFileOutput: new BooleanOption("multi-file-output", "Renders each top-level object in its own Go file", false),
fieldTags: new StringOption("field-tags", "list of tags which should be generated for fields", "TAGS", "json"),
omitEmpty: new BooleanOption("omit-empty", "If set, all non-required objects will be tagged with ,omitempty", false)
omitEmpty: new BooleanOption(
"omit-empty",
'If set, all non-required objects will be tagged with ",omitempty"',
false
)
};

export class GoTargetLanguage extends TargetLanguage {
Expand All @@ -38,9 +42,9 @@ export class GoTargetLanguage extends TargetLanguage {
protected getOptions(): Option<any>[] {
return [
goOptions.justTypes,
goOptions.justTypesAndPackage,
goOptions.packageName,
goOptions.multiFileOutput,
goOptions.justTypesAndPackage,
goOptions.fieldTags,
goOptions.omitEmpty
];
Expand Down Expand Up @@ -95,8 +99,9 @@ function isValueType(t: Type): boolean {
return primitiveValueTypeKinds.indexOf(kind) >= 0 || kind === "class" || kind === "enum" || kind === "date-time";
}

function canOmitEmpty(cp: ClassProperty): boolean {
function canOmitEmpty(cp: ClassProperty, omitEmptyOption: boolean): boolean {
if (!cp.isOptional) return false;
if (omitEmptyOption) return true;
const t = cp.type;
return ["union", "null", "any"].indexOf(t.kind) < 0;
}
Expand Down Expand Up @@ -285,7 +290,7 @@ export class GoRenderer extends ConvenienceRenderer {
const docStrings =
description !== undefined && description.length > 0 ? description.map(d => "// " + d) : [];
const goType = this.propertyGoType(p);
const omitEmpty = canOmitEmpty(p) || this._options.omitEmpty ? ",omitempty" : [];
const omitEmpty = canOmitEmpty(p, this._options.omitEmpty) ? ",omitempty" : [];

docStrings.forEach(doc => columns.push([doc]));
const tags = this._options.fieldTags
Expand Down

0 comments on commit 916cd94

Please sign in to comment.