-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add json serialisation support for golang (#1848)
- Loading branch information
Showing
15 changed files
with
358 additions
and
7 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 Struct Tags In Models | ||
|
||
`GO_COMMON_PRESET` to render JSON `struct-tags` in go `structs` along with adding custom `Marshaler` functions for enum. | ||
|
||
## 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 json-tags in struct and should log expected output to console 1`] = ` | ||
Array [ | ||
"// Root represents a Root model. | ||
type Root struct { | ||
Cities *Cities \`json:\\"cities\\"\` | ||
Options *Options \`json:\\"options\\"\` | ||
}", | ||
] | ||
`; | ||
exports[`Should be able to render json-tags in struct 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 json-tags in struct 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 json-tags in struct', () => { | ||
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 }; | ||
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-json-tags" }, | ||
"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" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { GoPreset } from '../GoPreset'; | ||
import { GoRenderer } from '../GoRenderer'; | ||
import { | ||
ConstrainedDictionaryModel, | ||
ConstrainedObjectPropertyModel, | ||
ConstrainedEnumModel | ||
} from '../../../models'; | ||
|
||
export interface GoCommonPresetOptions { | ||
addJsonTag: boolean; | ||
} | ||
|
||
function renderJSONTag({ | ||
field | ||
}: { | ||
field: ConstrainedObjectPropertyModel; | ||
}): string { | ||
if ( | ||
field.property instanceof ConstrainedDictionaryModel && | ||
field.property.serializationType === 'unwrap' | ||
) { | ||
return `json:"-"`; | ||
} | ||
return `json:"${field.unconstrainedPropertyName}"`; | ||
} | ||
|
||
function renderMarshallingFunctions({ | ||
model, | ||
renderer | ||
}: { | ||
model: ConstrainedEnumModel; | ||
renderer: GoRenderer<any>; | ||
}): string { | ||
renderer.dependencyManager.addDependency('encoding/json'); | ||
return ` | ||
func (op *${model.name}) UnmarshalJSON(raw []byte) error { | ||
var v any | ||
if err := json.Unmarshal(raw, &v); err != nil { | ||
return err | ||
} | ||
*op = ValuesTo${model.name}[v] | ||
return nil | ||
} | ||
func (op ${model.name}) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(op.Value()) | ||
}`; | ||
} | ||
|
||
export const GO_COMMON_PRESET: GoPreset<GoCommonPresetOptions> = { | ||
struct: { | ||
field: ({ content, field, options }) => { | ||
const blocks: string[] = []; | ||
if (options.addJsonTag) { | ||
blocks.push(renderJSONTag({ field })); | ||
} | ||
return `${content} \`${blocks.join(' ')}\``; | ||
} | ||
}, | ||
enum: { | ||
additionalContent({ content, model, renderer, options }) { | ||
const blocks: string[] = []; | ||
if (options.addJsonTag) { | ||
blocks.push(renderMarshallingFunctions({ model, renderer })); | ||
} | ||
|
||
return `${content}\n ${blocks.join('\n')}`; | ||
} | ||
} | ||
}; |
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 @@ | ||
export * from './CommonPreset'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
GoGenerator, | ||
GO_COMMON_PRESET, | ||
GoCommonPresetOptions | ||
} from '../../../../src/generators'; | ||
|
||
describe('GO_COMMON_PRESET', () => { | ||
let generator: GoGenerator; | ||
beforeEach(() => { | ||
const options: GoCommonPresetOptions = { addJsonTag: true }; | ||
generator = new GoGenerator({ | ||
presets: [{ preset: GO_COMMON_PRESET, options }] | ||
}); | ||
}); | ||
|
||
test('should render json tags for structs', async () => { | ||
const doc = { | ||
type: 'object', | ||
properties: { | ||
stringProp: { type: 'string' }, | ||
numberProp: { type: 'number' }, | ||
booleanProp: { type: 'boolean' } | ||
}, | ||
additionalProperties: false | ||
}; | ||
|
||
const models = await generator.generate(doc); | ||
expect(models).toHaveLength(1); | ||
expect(models[0].result).toMatchSnapshot(); | ||
}); | ||
|
||
test('should render the marshal functions for enum', async () => { | ||
const doc = { | ||
type: 'string', | ||
enum: ['2.6.0'] | ||
}; | ||
const models = await generator.generate(doc); | ||
expect(models).toHaveLength(1); | ||
expect(models[0].result).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.