-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
214 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
}", | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> { | ||
const models = await generator.generate(jsonSchemaDraft7); | ||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
if (require.main === module) { | ||
generate(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters