-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Kotlin model generator (#1074)
Co-authored-by: Jonas Lagoni <jonas-lt@live.dk>
- Loading branch information
1 parent
9e3a823
commit fcdab58
Showing
75 changed files
with
2,457 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ node_modules | |
coverage | ||
lib | ||
*.DS_Store | ||
.vscode | ||
.idea | ||
output |
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
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,52 @@ | ||
# Kotlin | ||
|
||
There are special use-cases that each language supports; this document pertains to **Kotlin models**. | ||
|
||
Since `data classes` are used for every model that has got properties, there is no need for additional settings or | ||
features to generate `toString()`, `equals()`, `hashCode()`, getters or setters. | ||
|
||
Classes without properties are depicted by usual `classes`, they get no `toString()`, `equals()` or `hashCode()` | ||
implementation. The default one should suffice here. | ||
|
||
<!-- toc is generated with GitHub Actions do not remove toc markers --> | ||
|
||
<!-- toc --> | ||
|
||
- [Include KDoc for properties](#include-kdoc-for-properties) | ||
- [Change the collection type for arrays](#change-the-collection-type-for-arrays) | ||
- [Include Javax validation constraint annotations for properties](#include-javax-validation-constraint-annotations-for-properties) | ||
- [Generate serializer and deserializer functionality](#generate-serializer-and-deserializer-functionality) | ||
* [To and from JSON](#to-and-from-json) | ||
* [To and from XML](#to-and-from-xml) | ||
* [To and from binary](#to-and-from-binary) | ||
<!-- tocstop --> | ||
|
||
## Include KDoc for properties | ||
To generate models containing `KDoc` from description and examples, use the `KOTLIN_DESCRIPTION_PRESET` option. | ||
|
||
Check out this [example for a live demonstration](../../examples/kotlin-generate-kdoc). | ||
|
||
## Change the collection type for arrays | ||
|
||
Sometimes, we might want to render a different collection type, and instead of the default `Array` use as `List` type. To do so, provide the option `collectionType: 'List'`. | ||
|
||
Check out this [example for a live demonstration](../../examples/kotlin-change-collection-type). | ||
|
||
## Include Javax validation constraint annotations for properties | ||
|
||
In some cases, when you generate the models from JSON Schema, you may want to include `javax.validation.constraint` annotations. | ||
|
||
Check out this [example for a live demonstration](../../examples/kotlin-generate-javax-constraint-annotation). | ||
|
||
## Generate serializer and deserializer functionality | ||
|
||
The most widely used usecase for Modelina is to generate models that include serialization and deserialization functionality to convert the models into payload data. This payload data can of course be many kinds, JSON, XML, raw binary, you name it. | ||
|
||
As you normally only need one library to do this, we developers can never get enough with creating new stuff, therefore there might be one specific library you need or want to integrate with. Therefore, there is not one specific preset that offers everything. Below is a list of all the supported serialization presets. | ||
|
||
### To and from JSON | ||
Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)! | ||
### 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)! | ||
### To and from binary | ||
Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)! |
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,17 @@ | ||
# Kotlin Enums | ||
|
||
A basic example of how to use Modelina and output a Kotlin 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 | ||
``` |
12 changes: 12 additions & 0 deletions
12
examples/generate-kotlin-enums/__snapshots__/index.spec.ts.snap
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,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to render Kotlin Enums and should log expected output to console 1`] = ` | ||
Array [ | ||
"enum class Protocol(val value: Any) { | ||
HTTP(\\"HTTP\\"), | ||
NUMBER_1(1), | ||
HTTPS(\\"HTTPS\\"), | ||
TRUE(true); | ||
}", | ||
] | ||
`; |
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,13 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; }); | ||
import {generate} from './index'; | ||
|
||
describe('Should be able to render Kotlin Enums', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generate(); | ||
expect(spy.mock.calls.length).toEqual(1); | ||
expect(spy.mock.calls[0]).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,20 @@ | ||
import { KotlinGenerator} from '../../src'; | ||
|
||
const generator = new KotlinGenerator(); | ||
const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
additionalProperties: false, | ||
$id: 'protocol', | ||
type: ['string', 'int', 'boolean'], | ||
enum: ['HTTP', 1, 'HTTPS', true], | ||
}; | ||
|
||
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,12 @@ | ||
{ | ||
"config": { | ||
"example_name": "generate-kotlin-enums" | ||
}, | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Kotlin Data Models | ||
|
||
A basic example of how to use Modelina and output a Kotlin data model. | ||
|
||
## 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 | ||
``` |
11 changes: 11 additions & 0 deletions
11
examples/generate-kotlin-models/__snapshots__/index.spec.ts.snap
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,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to render Kotlin Models and should log expected output to console 1`] = ` | ||
Array [ | ||
"data class Root( | ||
val email: String, | ||
val cache: Int, | ||
val website: Website, | ||
)", | ||
] | ||
`; |
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,13 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; }); | ||
import {generate} from './index'; | ||
|
||
describe('Should be able to render Kotlin Models', () => { | ||
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(); | ||
}); | ||
}); |
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 { KotlinGenerator } from '../../src'; | ||
|
||
const generator = new KotlinGenerator(); | ||
const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
email: { | ||
type: 'string', | ||
format: 'email' | ||
}, | ||
cache: { | ||
type: 'integer' | ||
}, | ||
website: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
domain: { | ||
type: 'string', | ||
format: 'url' | ||
}, | ||
protocol: { | ||
type: 'string', | ||
enum: ['HTTP', 'HTTPS'], | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
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,12 @@ | ||
{ | ||
"config": { | ||
"example_name": "generate-kotlin-models" | ||
}, | ||
"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
Oops, something went wrong.