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

fix: add go omitempty json tag #2004

Merged
merged 4 commits into from
May 21, 2024
Merged
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
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
Loading