forked from asyncapi/modelina
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into cli_update_readme_and_usage
- Loading branch information
Showing
31 changed files
with
939 additions
and
34 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
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,26 @@ | ||
# Go Union Types | ||
|
||
Modelina now supports union types for `Go`. Since `Go` does not have native union types support modelina uses `struct embeddings` to mock union types. Modelina has custom names for models that are not directly embedded in structs. Users can update the default names by passing custom values. | ||
|
||
|
||
```ts | ||
const generator = new GoGenerator({ | ||
unionAnyModelName: 'ModelinaAnyType', | ||
unionArrModelName: 'ModelinaArrType', | ||
unionDictModelName: 'ModelinaDictType' | ||
}); | ||
``` | ||
|
||
## 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,64 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to render union types and should log expected output to console 1`] = ` | ||
Array [ | ||
"// AdditionalProperty represents a AdditionalProperty model. | ||
type AdditionalProperty struct { | ||
AdditionalPropertyOneOf_0 | ||
AdditionalPropertyOneOf_1 | ||
string | ||
float64 | ||
ModelinaArrType []string | ||
ModelinaArrType []Union | ||
AdditionalPropertyOneOf_6 | ||
}", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render union types and should log expected output to console 2`] = ` | ||
Array [ | ||
"// Union represents a Union model. | ||
type Union struct { | ||
string | ||
float64 | ||
ModelinaAnyType interface{} | ||
}", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render union types and should log expected output to console 3`] = ` | ||
Array [ | ||
"// AdditionalPropertyOneOf_6 represents a AdditionalPropertyOneOf_6 model. | ||
type AdditionalPropertyOneOf_6 struct { | ||
string | ||
float64 | ||
bool | ||
}", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render union types and should log expected output to console 4`] = ` | ||
Array [ | ||
"", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render union types and should log expected output to console 5`] = ` | ||
Array [ | ||
"// AdditionalPropertyOneOf_0 represents a AdditionalPropertyOneOf_0 model. | ||
type AdditionalPropertyOneOf_0 struct { | ||
Ref string | ||
AdditionalProperties map[string]interface{} | ||
}", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render union types and should log expected output to console 6`] = ` | ||
Array [ | ||
"// AdditionalPropertyOneOf_1 represents a AdditionalPropertyOneOf_1 model. | ||
type AdditionalPropertyOneOf_1 struct { | ||
Id string | ||
AdditionalProperties map[string]interface{} | ||
}", | ||
] | ||
`; |
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,20 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { | ||
return; | ||
}); | ||
import { generate } from './index'; | ||
|
||
describe('Should be able to render union types', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generate(); | ||
expect(spy.mock.calls.length).toEqual(6); | ||
expect(spy.mock.calls[0]).toMatchSnapshot(); | ||
expect(spy.mock.calls[1]).toMatchSnapshot(); | ||
expect(spy.mock.calls[2]).toMatchSnapshot(); | ||
expect(spy.mock.calls[3]).toMatchSnapshot(); | ||
expect(spy.mock.calls[4]).toMatchSnapshot(); | ||
expect(spy.mock.calls[5]).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,33 @@ | ||
import { GoGenerator } from '../../src'; | ||
|
||
const generator = new GoGenerator({ | ||
unionAnyModelName: 'ModelinaAnyType', | ||
unionArrModelName: 'ModelinaArrType', | ||
unionDictModelName: 'ModelinaDictType' | ||
}); | ||
|
||
const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
additionalProperties: { | ||
oneOf: [ | ||
{ type: 'object', properties: { ref: { type: 'string' } } }, | ||
{ type: 'object', properties: { Id: { type: 'string' } } }, | ||
{ type: 'string' }, | ||
{ type: 'number' }, | ||
{ type: 'array', items: { type: 'string' } }, | ||
{ type: 'array', items: [{ type: 'string' }, { type: 'number' }] }, | ||
{ oneOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }] } | ||
] | ||
} | ||
}; | ||
|
||
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-union-type" }, | ||
"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
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
Oops, something went wrong.