Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Dec 25, 2023
1 parent 1e55f7a commit cc76c51
Show file tree
Hide file tree
Showing 31 changed files with 777 additions and 8 deletions.
36 changes: 36 additions & 0 deletions docs/languages/Scala.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Scala

There are special use-cases that each language supports; this document pertains to **Scala models**.

<!-- toc is generated with GitHub Actions do not remove toc markers -->

<!-- toc -->

- [Description Present](#description-present)
- [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 -->

## Description Present

By default, descriptions are not rendered for the model; you can change that by applying `SCALA_DESCRIPTION_PRESET`.

Check out this [example for a live demonstration](../../examples/php-generate-documentation-preset).

## Generate serializer and deserializer functionality

The most widely used usecase for Modelina is to generate models that include serilization and deserialization functionality to convert the models into payload data. This payload data can of course be many different 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)!
10 changes: 10 additions & 0 deletions examples/custom-logging/package-lock.json

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

9 changes: 9 additions & 0 deletions examples/custom-logging/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"config" : { "example_name" : "custom-logging" }, "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"
}
}
10 changes: 10 additions & 0 deletions examples/dart-generate-json-annotation/package-lock.json

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

9 changes: 9 additions & 0 deletions examples/dart-generate-json-annotation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"config" : { "example_name" : "dart-generate-json-annotation" }, "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-php-models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# PHP Generate Models Example

A basic example of how to use Modelina and output PHP 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
```
14 changes: 14 additions & 0 deletions examples/generate-php-models/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to render PHP and should log expected output to console 1`] = `
Array [
"final class Root
{
private ?string $email;
public function getEmail(): ?string { return $this->email; }
public function setEmail(?string $email): void { $this->email = $email; }
}
",
]
`;
15 changes: 15 additions & 0 deletions examples/generate-php-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 PHP', () => {
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();
});
});
24 changes: 24 additions & 0 deletions examples/generate-php-models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { OutputModel, PhpGenerator } from '../../src';

const generator: PhpGenerator = new PhpGenerator();
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
format: 'email'
}
}
};

export async function generate(): Promise<void> {
const models: OutputModel[] = 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-php-models/package-lock.json

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

10 changes: 10 additions & 0 deletions examples/generate-php-models/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "php-generate-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"
}
}
14 changes: 14 additions & 0 deletions examples/generate-scala-enums/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to render Kotlin Enums and should log expected output to console 1`] = `
Array [
"object Protocol extends Enumeration {
type Protocol = Value
val Http: Protocol.Value = Value(\\"HTTP\\")
val Number_1: Protocol.Value = Value(1)
val Https: Protocol.Value = Value(\\"HTTPS\\")
val ReservedTrue: Protocol.Value = Value(\\"true\\")
}",
]
`;
11 changes: 11 additions & 0 deletions examples/generate-scala-models/__snapshots__/index.spec.ts.snap
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 [
"case class Root(
email: Option[String],
cache: Option[Int],
website: Option[Website],
)",
]
`;
3 changes: 3 additions & 0 deletions examples/integrate-with-maven/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Integrate Modelina into Maven

This example show how you can integrate Modelina into Maven with a custom build script. [Please have a look at the Maven project README file for further information](./maven-project/README.md).
25 changes: 25 additions & 0 deletions examples/integrate-with-maven/maven-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse m2e generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

bin
.settings

# Ignore Modelina generation node modules
scripts/modelina/java_runtime_node
scripts/modelina/node_modules
10 changes: 10 additions & 0 deletions examples/integrate-with-maven/maven-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Integrate Modelina into Maven

This Java Maven project shows an example how to integrate Modelina and AsyncAPI into your build process.

Here is how it works:
- The script `./scripts/modelina/generate.ts` is what generates all the models, and what Maven uses to generate the models. This can also be executed manually through `npm run generate`.
- The input, in this case, is an AsyncAPI document located in the root of the project `./asyncapi.json`. The input can be anything, just alter the generator script.
- The Maven project file `./pom.xml` then utilizes the [frontend-maven-plugin](https://github.com/eirslett/frontend-maven-plugin) to execute the generate script on build so you will always have the up to date models from your AsyncAPI document.

> NOTICE: The only thing you manually have to change for this to work in your project is the dependency entry for `"@asyncapi/modelina": "file:../../../../../",` in the `./scripts/modelina/package.json` file to use the latest Modelina version (we only use a local one for testing purposes).
26 changes: 26 additions & 0 deletions examples/integrate-with-maven/maven-project/asyncapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"asyncapi": "2.2.0",
"info": {
"title": "example",
"version": "0.1.0"
},
"channels": {
"/test": {
"subscribe": {
"message": {
"payload": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"email": {
"type": "string",
"format": "email"
}
}
}
}
}
}
}
}
113 changes: 113 additions & 0 deletions examples/integrate-with-maven/maven-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>

<name>maven-project</name>
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
<configuration>
<workingDirectory>./scripts/modelina</workingDirectory>
<installDirectory>java_runtime_node</installDirectory>
</configuration>
<executions>
<!-- It will install nodejs and npm -->
<execution>
<id>install node and npm</id>
<phase>generate-sources</phase>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v18.15.0</nodeVersion>
</configuration>
</execution>
<execution>
<id>Generate sources</id>
<phase>generate-sources</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run generate</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit cc76c51

Please sign in to comment.