Skip to content

Commit

Permalink
Adding Scala code gen examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Ciocanu committed Dec 23, 2023
1 parent 9b91c3f commit ad94e42
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 2 deletions.
17 changes: 17 additions & 0 deletions examples/generate-scala-enums/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Scala Enums

A basic example of how to use Modelina and output a Scala enumeration.

## 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
```
15 changes: 15 additions & 0 deletions examples/generate-scala-enums/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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();
});
});
20 changes: 20 additions & 0 deletions examples/generate-scala-enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ScalaGenerator } from '../../src/generators/scala';

const generator = new ScalaGenerator();
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();
}
10 changes: 10 additions & 0 deletions examples/generate-scala-enums/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/generate-scala-enums/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"config": {
"example_name": "generate-scala-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"
}
}
17 changes: 17 additions & 0 deletions examples/generate-scala-models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Scala Data Models

A basic example of how to use Modelina and output a Scala 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
```
15 changes: 15 additions & 0 deletions examples/generate-scala-models/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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();
});
});
41 changes: 41 additions & 0 deletions examples/generate-scala-models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ScalaGenerator } from '../../src/generators/scala';

const generator = new ScalaGenerator();
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();
}
10 changes: 10 additions & 0 deletions examples/generate-scala-models/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/generate-scala-models/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"config": {
"example_name": "generate-scala-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"
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@
"generate:assets": "npm run build:prod && npm run docs && npm run generate:readme:toc && npm run format",
"bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION",
"prepublishOnly": "npm run build:prod && npm run generate:readme:toc && npm run format",
"format": "prettier --config .prettierrc \"./**/*.ts\" --write",
"format-now": "prettier --config .prettierrc test/generators/scala/ScalaGenerator.spec.ts --write"
"format": "prettier --config .prettierrc \"./**/*.ts\" --write"
},
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit ad94e42

Please sign in to comment.