Skip to content

Commit

Permalink
fix: add go omitempty json tag (#2004)
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns authored May 21, 2024
1 parent a994e02 commit 4a296b7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/go-json-tags/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ exports[`Should be able to render json-tags in struct and should log expected ou
Array [
"// Root represents a Root model.
type Root struct {
Cities *Cities \`json:\\"cities\\"\`
Options *Options \`json:\\"options\\"\`
Cities *Cities \`json:\\"cities, omitempty\\"\`
Options *Options \`json:\\"options, omitempty\\"\`
}",
]
`;
Expand Down
5 changes: 3 additions & 2 deletions modelina-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/generators/go/presets/CommonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function renderJSONTag({
) {
return `json:"-"`;
}
return `json:"${field.unconstrainedPropertyName}"`;
return `json:"${field.unconstrainedPropertyName}, omitempty"`;
}

function renderMarshallingFunctions({
Expand Down
4 changes: 3 additions & 1 deletion test/generators/go/presets/CommonPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
describe('GO_COMMON_PRESET', () => {
let generator: GoGenerator;
beforeEach(() => {
const options: GoCommonPresetOptions = { addJsonTag: true };
const options: GoCommonPresetOptions = {
addJsonTag: true
};
generator = new GoGenerator({
presets: [{ preset: GO_COMMON_PRESET, options }]
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
exports[`GO_COMMON_PRESET should render json tags for structs 1`] = `
"// Root represents a Root model.
type Root struct {
StringProp string \`json:\\"stringProp\\"\`
NumberProp float64 \`json:\\"numberProp\\"\`
BooleanProp bool \`json:\\"booleanProp\\"\`
StringProp string \`json:\\"stringProp, omitempty\\"\`
NumberProp float64 \`json:\\"numberProp, omitempty\\"\`
BooleanProp bool \`json:\\"booleanProp, omitempty\\"\`
}"
`;
Expand Down

0 comments on commit 4a296b7

Please sign in to comment.