From f90a1b6224073f7c60747de33a1e37f2e85a10fe Mon Sep 17 00:00:00 2001 From: souvik Date: Mon, 20 May 2024 13:44:07 +0530 Subject: [PATCH 1/4] feat: add go omitempty json tag --- docs/languages/Go.md | 8 ++ examples/go-omitepty-tag/README.md | 17 ++++ .../__snapshots__/index.spec.ts.snap | 97 +++++++++++++++++++ examples/go-omitepty-tag/index.spec.ts | 17 ++++ examples/go-omitepty-tag/index.ts | 37 +++++++ examples/go-omitepty-tag/package-lock.json | 10 ++ examples/go-omitepty-tag/package.json | 10 ++ modelina-cli/package-lock.json | 5 +- src/generators/go/presets/CommonPreset.ts | 11 ++- .../go/presets/CommonPreset.spec.ts | 5 +- .../__snapshots__/CommonPreset.spec.ts.snap | 6 +- 11 files changed, 214 insertions(+), 9 deletions(-) create mode 100644 examples/go-omitepty-tag/README.md create mode 100644 examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap create mode 100644 examples/go-omitepty-tag/index.spec.ts create mode 100644 examples/go-omitepty-tag/index.ts create mode 100644 examples/go-omitepty-tag/package-lock.json create mode 100644 examples/go-omitepty-tag/package.json diff --git a/docs/languages/Go.md b/docs/languages/Go.md index 305b56ea6e..daef4e9d3f 100644 --- a/docs/languages/Go.md +++ b/docs/languages/Go.md @@ -21,6 +21,7 @@ As you normally only need one library to do this, we developers can never get en Here are all the supported presets and the libraries they use for converting to and from JSON: - [JSON Tags](#json-tags) + - [omitempty tag](#omitempty-tag) #### JSON Tags @@ -28,6 +29,13 @@ To generate go models that work correctly with JSON marshal functions we need to check out this [example for a live demonstration](../../examples/go-json-tags/) +##### omitempty tag + +To add `omitempty` parameter to JSON `struct-tags`, use the preset `GO_COMMON_PRESET` and provide the option `addOmitEmpty: true` along with `addJsonTag: true`. + +check out this [example for a live demonstration](../../examples/go-omitepty-tag/) + + ### To and from XML Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)! diff --git a/examples/go-omitepty-tag/README.md b/examples/go-omitepty-tag/README.md new file mode 100644 index 0000000000..6b053ad6aa --- /dev/null +++ b/examples/go-omitepty-tag/README.md @@ -0,0 +1,17 @@ +# go omitempty JSON struct tag + +`GO_COMMON_PRESET` to render `omitempty` parameter in JSON `struct-tags` in go struct. + +## How to run this example + +Run this example using: + +```sh +npm i && npm run start +``` + +If you are on Windows, use the `start:windows` script instead: + +```sh +npm i && npm run start:windows +``` diff --git a/examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap b/examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap new file mode 100644 index 0000000000..09bcce6b21 --- /dev/null +++ b/examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap @@ -0,0 +1,97 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Should be able to render TEMPLATE and should log expected output to console 1`] = ` +Array [ + "// Root represents a Root model. +type Root struct { + Cities *Cities \`json:\\"cities, omitempty\\"\` + Options *Options \`json:\\"options, omitempty\\"\` +}", +] +`; + +exports[`Should be able to render TEMPLATE and should log expected output to console 2`] = ` +Array [ + "// Cities represents an enum of Cities. +type Cities uint + +const ( + CitiesLondon Cities = iota + CitiesRome + CitiesBrussels +) + +// Value returns the value of the enum. +func (op Cities) Value() any { + if op >= Cities(len(CitiesValues)) { + return nil + } + return CitiesValues[op] +} + +var CitiesValues = []any{\\"London\\",\\"Rome\\",\\"Brussels\\"} +var ValuesToCities = map[any]Cities{ + CitiesValues[CitiesLondon]: CitiesLondon, + CitiesValues[CitiesRome]: CitiesRome, + CitiesValues[CitiesBrussels]: CitiesBrussels, +} + + +func (op *Cities) UnmarshalJSON(raw []byte) error { + var v any + if err := json.Unmarshal(raw, &v); err != nil { + return err + } + *op = ValuesToCities[v] + return nil +} + +func (op Cities) MarshalJSON() ([]byte, error) { + return json.Marshal(op.Value()) +}", +] +`; + +exports[`Should be able to render TEMPLATE and should log expected output to console 3`] = ` +Array [ + "// Options represents an enum of Options. +type Options uint + +const ( + OptionsNumber_123 Options = iota + OptionsNumber_213 + OptionsTrue + OptionsRun +) + +// Value returns the value of the enum. +func (op Options) Value() any { + if op >= Options(len(OptionsValues)) { + return nil + } + return OptionsValues[op] +} + +var OptionsValues = []any{123,213,true,\\"Run\\"} +var ValuesToOptions = map[any]Options{ + OptionsValues[OptionsNumber_123]: OptionsNumber_123, + OptionsValues[OptionsNumber_213]: OptionsNumber_213, + OptionsValues[OptionsTrue]: OptionsTrue, + OptionsValues[OptionsRun]: OptionsRun, +} + + +func (op *Options) UnmarshalJSON(raw []byte) error { + var v any + if err := json.Unmarshal(raw, &v); err != nil { + return err + } + *op = ValuesToOptions[v] + return nil +} + +func (op Options) MarshalJSON() ([]byte, error) { + return json.Marshal(op.Value()) +}", +] +`; diff --git a/examples/go-omitepty-tag/index.spec.ts b/examples/go-omitepty-tag/index.spec.ts new file mode 100644 index 0000000000..a282244bfb --- /dev/null +++ b/examples/go-omitepty-tag/index.spec.ts @@ -0,0 +1,17 @@ +const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { + return; +}); +import { generate } from './index'; + +describe('Should be able to render TEMPLATE', () => { + afterAll(() => { + jest.restoreAllMocks(); + }); + test('and should log expected output to console', async () => { + await generate(); + expect(spy.mock.calls.length).toEqual(3); + expect(spy.mock.calls[0]).toMatchSnapshot(); + expect(spy.mock.calls[1]).toMatchSnapshot(); + expect(spy.mock.calls[2]).toMatchSnapshot(); + }); +}); diff --git a/examples/go-omitepty-tag/index.ts b/examples/go-omitepty-tag/index.ts new file mode 100644 index 0000000000..caedf25fe2 --- /dev/null +++ b/examples/go-omitepty-tag/index.ts @@ -0,0 +1,37 @@ +import { + GoGenerator, + GO_COMMON_PRESET, + GoCommonPresetOptions +} from '../../src'; + +const options: GoCommonPresetOptions = { addJsonTag: true, addOmitEmpty: true }; +const generator = new GoGenerator({ + presets: [{ preset: GO_COMMON_PRESET, options }] +}); +const jsonSchemaDraft7 = { + $schema: 'http://json-schema.org/draft-07/schema#', + type: 'object', + additionalProperties: false, + properties: { + cities: { + $id: 'cities', + type: 'string', + enum: ['London', 'Rome', 'Brussels'] + }, + options: { + $id: 'options', + type: ['integer', 'boolean', 'string'], + enum: [123, 213, true, 'Run'] + } + } +}; + +export async function generate(): Promise { + const models = await generator.generate(jsonSchemaDraft7); + for (const model of models) { + console.log(model.result); + } +} +if (require.main === module) { + generate(); +} diff --git a/examples/go-omitepty-tag/package-lock.json b/examples/go-omitepty-tag/package-lock.json new file mode 100644 index 0000000000..56ab4e873b --- /dev/null +++ b/examples/go-omitepty-tag/package-lock.json @@ -0,0 +1,10 @@ +{ + "name": "typescript-interface", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "hasInstallScript": true + } + } +} diff --git a/examples/go-omitepty-tag/package.json b/examples/go-omitepty-tag/package.json new file mode 100644 index 0000000000..7c9aa607b8 --- /dev/null +++ b/examples/go-omitepty-tag/package.json @@ -0,0 +1,10 @@ +{ + "config" : { "example_name" : "go omitempty json tag" }, + "scripts": { + "install": "cd ../.. && npm i", + "start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts", + "start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts", + "test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts", + "test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts" + } +} diff --git a/modelina-cli/package-lock.json b/modelina-cli/package-lock.json index 67a8370d69..1aa32ad4fc 100644 --- a/modelina-cli/package-lock.json +++ b/modelina-cli/package-lock.json @@ -185,9 +185,10 @@ } }, "node_modules/@asyncapi/modelina": { - "version": "4.0.0-next.38", + "version": "4.0.0-next.39", "resolved": "file:scripts/modelina-package/asyncapi-modelina.tgz", - "integrity": "sha512-Hv6M5/1ViR6EGGllwIqyblNlsLeW5qEQOHWc7F1+zk4DMmLT6cxiDPZKYyRbqWWhbjbKXY4A0bK0/pIDs8mdvg==", + "integrity": "sha512-Hkmdufj2ZJigzRvb2MQmfTxQpRj4Wp0A5n8o3vz9n9nhuMfehrzilYed8uIPaFJ9ML3wMckk0KrAYcpula7JFw==", + "license": "Apache-2.0", "dependencies": { "@apidevtools/json-schema-ref-parser": "^11.1.0", "@apidevtools/swagger-parser": "^10.1.0", diff --git a/src/generators/go/presets/CommonPreset.ts b/src/generators/go/presets/CommonPreset.ts index 8f1ae31643..de15b75b43 100644 --- a/src/generators/go/presets/CommonPreset.ts +++ b/src/generators/go/presets/CommonPreset.ts @@ -8,12 +8,15 @@ import { export interface GoCommonPresetOptions { addJsonTag: boolean; + addOmitEmpty?: boolean; } function renderJSONTag({ - field + field, + omitempty }: { field: ConstrainedObjectPropertyModel; + omitempty?: boolean; }): string { if ( field.property instanceof ConstrainedDictionaryModel && @@ -21,7 +24,9 @@ function renderJSONTag({ ) { return `json:"-"`; } - return `json:"${field.unconstrainedPropertyName}"`; + return `json:"${field.unconstrainedPropertyName}${ + omitempty ? ', omitempty' : '' + }"`; } function renderMarshallingFunctions({ @@ -52,7 +57,7 @@ export const GO_COMMON_PRESET: GoPreset = { field: ({ content, field, options }) => { const blocks: string[] = []; if (options.addJsonTag) { - blocks.push(renderJSONTag({ field })); + blocks.push(renderJSONTag({ field, omitempty: options.addOmitEmpty })); } return `${content} \`${blocks.join(' ')}\``; } diff --git a/test/generators/go/presets/CommonPreset.spec.ts b/test/generators/go/presets/CommonPreset.spec.ts index 9647bdfec6..2fcb892307 100644 --- a/test/generators/go/presets/CommonPreset.spec.ts +++ b/test/generators/go/presets/CommonPreset.spec.ts @@ -7,7 +7,10 @@ import { describe('GO_COMMON_PRESET', () => { let generator: GoGenerator; beforeEach(() => { - const options: GoCommonPresetOptions = { addJsonTag: true }; + const options: GoCommonPresetOptions = { + addJsonTag: true, + addOmitEmpty: true + }; generator = new GoGenerator({ presets: [{ preset: GO_COMMON_PRESET, options }] }); diff --git a/test/generators/go/presets/__snapshots__/CommonPreset.spec.ts.snap b/test/generators/go/presets/__snapshots__/CommonPreset.spec.ts.snap index 2b8d2f831c..6a1819d052 100644 --- a/test/generators/go/presets/__snapshots__/CommonPreset.spec.ts.snap +++ b/test/generators/go/presets/__snapshots__/CommonPreset.spec.ts.snap @@ -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\\"\` }" `; From 2dadd2c5869bab9b90fd26a3df64f74ada7ac9e0 Mon Sep 17 00:00:00 2001 From: souvik Date: Tue, 21 May 2024 13:22:49 +0530 Subject: [PATCH 2/4] feat: remove omitempty option --- docs/languages/Go.md | 7 -- examples/go-omitepty-tag/README.md | 17 ---- .../__snapshots__/index.spec.ts.snap | 97 ------------------- examples/go-omitepty-tag/index.spec.ts | 17 ---- examples/go-omitepty-tag/index.ts | 37 ------- examples/go-omitepty-tag/package-lock.json | 10 -- examples/go-omitepty-tag/package.json | 10 -- src/generators/go/presets/CommonPreset.ts | 9 +- .../go/presets/CommonPreset.spec.ts | 1 - 9 files changed, 2 insertions(+), 203 deletions(-) delete mode 100644 examples/go-omitepty-tag/README.md delete mode 100644 examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap delete mode 100644 examples/go-omitepty-tag/index.spec.ts delete mode 100644 examples/go-omitepty-tag/index.ts delete mode 100644 examples/go-omitepty-tag/package-lock.json delete mode 100644 examples/go-omitepty-tag/package.json diff --git a/docs/languages/Go.md b/docs/languages/Go.md index daef4e9d3f..d2bd2b0f6c 100644 --- a/docs/languages/Go.md +++ b/docs/languages/Go.md @@ -21,7 +21,6 @@ As you normally only need one library to do this, we developers can never get en Here are all the supported presets and the libraries they use for converting to and from JSON: - [JSON Tags](#json-tags) - - [omitempty tag](#omitempty-tag) #### JSON Tags @@ -29,12 +28,6 @@ To generate go models that work correctly with JSON marshal functions we need to check out this [example for a live demonstration](../../examples/go-json-tags/) -##### omitempty tag - -To add `omitempty` parameter to JSON `struct-tags`, use the preset `GO_COMMON_PRESET` and provide the option `addOmitEmpty: true` along with `addJsonTag: true`. - -check out this [example for a live demonstration](../../examples/go-omitepty-tag/) - ### To and from XML Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)! diff --git a/examples/go-omitepty-tag/README.md b/examples/go-omitepty-tag/README.md deleted file mode 100644 index 6b053ad6aa..0000000000 --- a/examples/go-omitepty-tag/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# go omitempty JSON struct tag - -`GO_COMMON_PRESET` to render `omitempty` parameter in JSON `struct-tags` in go struct. - -## How to run this example - -Run this example using: - -```sh -npm i && npm run start -``` - -If you are on Windows, use the `start:windows` script instead: - -```sh -npm i && npm run start:windows -``` diff --git a/examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap b/examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap deleted file mode 100644 index 09bcce6b21..0000000000 --- a/examples/go-omitepty-tag/__snapshots__/index.spec.ts.snap +++ /dev/null @@ -1,97 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Should be able to render TEMPLATE and should log expected output to console 1`] = ` -Array [ - "// Root represents a Root model. -type Root struct { - Cities *Cities \`json:\\"cities, omitempty\\"\` - Options *Options \`json:\\"options, omitempty\\"\` -}", -] -`; - -exports[`Should be able to render TEMPLATE and should log expected output to console 2`] = ` -Array [ - "// Cities represents an enum of Cities. -type Cities uint - -const ( - CitiesLondon Cities = iota - CitiesRome - CitiesBrussels -) - -// Value returns the value of the enum. -func (op Cities) Value() any { - if op >= Cities(len(CitiesValues)) { - return nil - } - return CitiesValues[op] -} - -var CitiesValues = []any{\\"London\\",\\"Rome\\",\\"Brussels\\"} -var ValuesToCities = map[any]Cities{ - CitiesValues[CitiesLondon]: CitiesLondon, - CitiesValues[CitiesRome]: CitiesRome, - CitiesValues[CitiesBrussels]: CitiesBrussels, -} - - -func (op *Cities) UnmarshalJSON(raw []byte) error { - var v any - if err := json.Unmarshal(raw, &v); err != nil { - return err - } - *op = ValuesToCities[v] - return nil -} - -func (op Cities) MarshalJSON() ([]byte, error) { - return json.Marshal(op.Value()) -}", -] -`; - -exports[`Should be able to render TEMPLATE and should log expected output to console 3`] = ` -Array [ - "// Options represents an enum of Options. -type Options uint - -const ( - OptionsNumber_123 Options = iota - OptionsNumber_213 - OptionsTrue - OptionsRun -) - -// Value returns the value of the enum. -func (op Options) Value() any { - if op >= Options(len(OptionsValues)) { - return nil - } - return OptionsValues[op] -} - -var OptionsValues = []any{123,213,true,\\"Run\\"} -var ValuesToOptions = map[any]Options{ - OptionsValues[OptionsNumber_123]: OptionsNumber_123, - OptionsValues[OptionsNumber_213]: OptionsNumber_213, - OptionsValues[OptionsTrue]: OptionsTrue, - OptionsValues[OptionsRun]: OptionsRun, -} - - -func (op *Options) UnmarshalJSON(raw []byte) error { - var v any - if err := json.Unmarshal(raw, &v); err != nil { - return err - } - *op = ValuesToOptions[v] - return nil -} - -func (op Options) MarshalJSON() ([]byte, error) { - return json.Marshal(op.Value()) -}", -] -`; diff --git a/examples/go-omitepty-tag/index.spec.ts b/examples/go-omitepty-tag/index.spec.ts deleted file mode 100644 index a282244bfb..0000000000 --- a/examples/go-omitepty-tag/index.spec.ts +++ /dev/null @@ -1,17 +0,0 @@ -const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { - return; -}); -import { generate } from './index'; - -describe('Should be able to render TEMPLATE', () => { - afterAll(() => { - jest.restoreAllMocks(); - }); - test('and should log expected output to console', async () => { - await generate(); - expect(spy.mock.calls.length).toEqual(3); - expect(spy.mock.calls[0]).toMatchSnapshot(); - expect(spy.mock.calls[1]).toMatchSnapshot(); - expect(spy.mock.calls[2]).toMatchSnapshot(); - }); -}); diff --git a/examples/go-omitepty-tag/index.ts b/examples/go-omitepty-tag/index.ts deleted file mode 100644 index caedf25fe2..0000000000 --- a/examples/go-omitepty-tag/index.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { - GoGenerator, - GO_COMMON_PRESET, - GoCommonPresetOptions -} from '../../src'; - -const options: GoCommonPresetOptions = { addJsonTag: true, addOmitEmpty: true }; -const generator = new GoGenerator({ - presets: [{ preset: GO_COMMON_PRESET, options }] -}); -const jsonSchemaDraft7 = { - $schema: 'http://json-schema.org/draft-07/schema#', - type: 'object', - additionalProperties: false, - properties: { - cities: { - $id: 'cities', - type: 'string', - enum: ['London', 'Rome', 'Brussels'] - }, - options: { - $id: 'options', - type: ['integer', 'boolean', 'string'], - enum: [123, 213, true, 'Run'] - } - } -}; - -export async function generate(): Promise { - const models = await generator.generate(jsonSchemaDraft7); - for (const model of models) { - console.log(model.result); - } -} -if (require.main === module) { - generate(); -} diff --git a/examples/go-omitepty-tag/package-lock.json b/examples/go-omitepty-tag/package-lock.json deleted file mode 100644 index 56ab4e873b..0000000000 --- a/examples/go-omitepty-tag/package-lock.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "typescript-interface", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "hasInstallScript": true - } - } -} diff --git a/examples/go-omitepty-tag/package.json b/examples/go-omitepty-tag/package.json deleted file mode 100644 index 7c9aa607b8..0000000000 --- a/examples/go-omitepty-tag/package.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "config" : { "example_name" : "go omitempty json tag" }, - "scripts": { - "install": "cd ../.. && npm i", - "start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts", - "start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts", - "test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts", - "test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts" - } -} diff --git a/src/generators/go/presets/CommonPreset.ts b/src/generators/go/presets/CommonPreset.ts index de15b75b43..6f50470fa6 100644 --- a/src/generators/go/presets/CommonPreset.ts +++ b/src/generators/go/presets/CommonPreset.ts @@ -8,15 +8,12 @@ import { export interface GoCommonPresetOptions { addJsonTag: boolean; - addOmitEmpty?: boolean; } function renderJSONTag({ field, - omitempty }: { field: ConstrainedObjectPropertyModel; - omitempty?: boolean; }): string { if ( field.property instanceof ConstrainedDictionaryModel && @@ -24,9 +21,7 @@ function renderJSONTag({ ) { return `json:"-"`; } - return `json:"${field.unconstrainedPropertyName}${ - omitempty ? ', omitempty' : '' - }"`; + return `json:"${field.unconstrainedPropertyName}, omitempty"`; } function renderMarshallingFunctions({ @@ -57,7 +52,7 @@ export const GO_COMMON_PRESET: GoPreset = { field: ({ content, field, options }) => { const blocks: string[] = []; if (options.addJsonTag) { - blocks.push(renderJSONTag({ field, omitempty: options.addOmitEmpty })); + blocks.push(renderJSONTag({ field })); } return `${content} \`${blocks.join(' ')}\``; } diff --git a/test/generators/go/presets/CommonPreset.spec.ts b/test/generators/go/presets/CommonPreset.spec.ts index 2fcb892307..f10a2842f6 100644 --- a/test/generators/go/presets/CommonPreset.spec.ts +++ b/test/generators/go/presets/CommonPreset.spec.ts @@ -9,7 +9,6 @@ describe('GO_COMMON_PRESET', () => { beforeEach(() => { const options: GoCommonPresetOptions = { addJsonTag: true, - addOmitEmpty: true }; generator = new GoGenerator({ presets: [{ preset: GO_COMMON_PRESET, options }] From b9a513d67b39b0f315afa6f2af1cf44fd9ec9d2e Mon Sep 17 00:00:00 2001 From: souvik Date: Tue, 21 May 2024 13:25:12 +0530 Subject: [PATCH 3/4] chore: lint fixes --- docs/languages/Go.md | 1 - src/generators/go/presets/CommonPreset.ts | 2 +- test/generators/go/presets/CommonPreset.spec.ts | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/languages/Go.md b/docs/languages/Go.md index d2bd2b0f6c..305b56ea6e 100644 --- a/docs/languages/Go.md +++ b/docs/languages/Go.md @@ -28,7 +28,6 @@ To generate go models that work correctly with JSON marshal functions we need to check out this [example for a live demonstration](../../examples/go-json-tags/) - ### To and from XML Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)! diff --git a/src/generators/go/presets/CommonPreset.ts b/src/generators/go/presets/CommonPreset.ts index 6f50470fa6..248a7b5f6c 100644 --- a/src/generators/go/presets/CommonPreset.ts +++ b/src/generators/go/presets/CommonPreset.ts @@ -11,7 +11,7 @@ export interface GoCommonPresetOptions { } function renderJSONTag({ - field, + field }: { field: ConstrainedObjectPropertyModel; }): string { diff --git a/test/generators/go/presets/CommonPreset.spec.ts b/test/generators/go/presets/CommonPreset.spec.ts index f10a2842f6..5fe4213b9c 100644 --- a/test/generators/go/presets/CommonPreset.spec.ts +++ b/test/generators/go/presets/CommonPreset.spec.ts @@ -8,7 +8,7 @@ describe('GO_COMMON_PRESET', () => { let generator: GoGenerator; beforeEach(() => { const options: GoCommonPresetOptions = { - addJsonTag: true, + addJsonTag: true }; generator = new GoGenerator({ presets: [{ preset: GO_COMMON_PRESET, options }] From 7d863fe2582d7cdb35da304dafed7ec1e6db462d Mon Sep 17 00:00:00 2001 From: souvik Date: Tue, 21 May 2024 13:34:52 +0530 Subject: [PATCH 4/4] feat: update example tests --- examples/go-json-tags/__snapshots__/index.spec.ts.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/go-json-tags/__snapshots__/index.spec.ts.snap b/examples/go-json-tags/__snapshots__/index.spec.ts.snap index d0a0aaf1e9..3f1ddc2de8 100644 --- a/examples/go-json-tags/__snapshots__/index.spec.ts.snap +++ b/examples/go-json-tags/__snapshots__/index.spec.ts.snap @@ -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\\"\` }", ] `;