Skip to content

Commit

Permalink
feat: add Typescript Input (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ron-debajyoti authored Mar 3, 2022
1 parent c8bcbd6 commit 737ade9
Show file tree
Hide file tree
Showing 26 changed files with 605 additions and 136 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
<em>Modelina is the official AsyncAPI SDK to generate data models (i.e. <a href="#outputs">Java/TypeScript classes, Go Structs, etc</a>) from <a href="#inputs">AsyncAPI documents, among other supported inputs</a>.</em>
</p>


[![blackbox pipeline status](https://img.shields.io/github/workflow/status/asyncapi/modelina/Blackbox%20testing%20(Stay%20Awhile%20and%20Listen)?label=blackbox%20testing)](https://github.com/asyncapi/modelina/actions/workflows/blackbox-testing.yml?query=branch%3Amaster++)
[![blackbox pipeline status](<https://img.shields.io/github/workflow/status/asyncapi/modelina/Blackbox%20testing%20(Stay%20Awhile%20and%20Listen)?label=blackbox%20testing>)](https://github.com/asyncapi/modelina/actions/workflows/blackbox-testing.yml?query=branch%3Amaster++)
[![Coverage Status](https://coveralls.io/repos/github/asyncapi/modelina/badge.svg?branch=master)](https://coveralls.io/github/asyncapi/modelina?branch=master)
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
![Maintenance score](https://img.shields.io/npms-io/maintenance-score/@asyncapi/modelina)
[![Npm latest version](https://img.shields.io/npm/v/@asyncapi/modelina)](https://www.npmjs.com/package/@asyncapi/modelina)
[![License](https://img.shields.io/github/license/asyncapi/modelina)](https://github.com/asyncapi/modelina/blob/master/LICENSE)
[![License](https://img.shields.io/github/license/asyncapi/modelina)](https://github.com/asyncapi/modelina/blob/master/LICENSE)
[![last commit](https://img.shields.io/github/last-commit/asyncapi/modelina)](https://github.com/asyncapi/modelina/commits/master)
[![Discussions](https://img.shields.io/github/discussions/asyncapi/modelina)](https://github.com/asyncapi/modelina/discussions)
[![Playground](https://img.shields.io/website?label=playground&url=https%3A%2F%2Fwww.asyncapi.com%2Ftools%2Fmodelina)](https://www.asyncapi.com/tools/modelina) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-22-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

---
Expand Down Expand Up @@ -52,6 +52,7 @@ This package is still under development and has not reached version 1.0.0 yet. T
Feel free to submit an issue if you require this project in other use-cases.

## Installation

Run this command to install Modelina in your project:

```bash
Expand All @@ -61,11 +62,13 @@ npm install @asyncapi/modelina
Once you've successfully installed Modelina in your project, it's time to select your generator. Check out the [examples](#examples) for the specific code.

## Features

The following table provides a short summary of available features for supported output languages.

To see the complete feature list for each language, please click the individual links for each language.

<a id="inputs"></a>

<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
Expand All @@ -85,6 +88,10 @@ To see the complete feature list for each language, please click the individual
<td>OpenAPI</td>
<td>We support the following OpenAPI versions: <em><a href="./docs/usage.md#generate-models-from-swagger-20-documents">Swagger 2.0</a> and <a href="./docs/usage.md#generate-models-from-openapi-documents">OpenAPI 3.0</a></em>, which generates models for all the defined request and response payloads.</td>
</tr>
<tr>
<td><a href="./docs/usage.md#generate-model-from-typescript-type-files">TypeScript file</a></td>
<td>We currently support TypeScript type file as input for model generation</td>
</tr>
</table>

<a id="outputs"></a>
Expand Down
8 changes: 8 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ The OpenAPI input processor expects that the property `openapi` is defined in or

The response and request payloads, since it is a JSON Schema variant, is [interpreted as a such](./interpretation_of_JSON_Schema.md).

## Generate models from TypeScript type files

Currently, we support generating models from a TypeScript type file.

- [Generate Java model from a TypeScript file](../examples/java-from-typescript-type/)

The TypeScript input processor expects that the typescript file and base directory where it's present, is passed as input, in order to process the types accurately.

## Generate Go models

Go is one of the many output languages we support. Check out this [basic example for a live demonstration](../examples/generate-go-models) and the following [Go documentation for more advanced use-cases](./languages/Go.md).
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ This directory contains a series of self-contained examples that you can use as
- [generate-csharp-models](./generate-csharp-models) - A basic example to generate C# data models
- [java-change-collection-type](./java-change-collection-type) - An example to render collections as List in Java.
- [java-generate-hashcode](./java-generate-hashcode) - A basic example that shows how to generate models that overwrite the `hashCode` method
- [java-from-typescript-type](./java-from-typescript-type/) - A basic example that shows how to generate a Java model from a TypeScript type input file.
- [java-generate-marshalling](./java-generate-marshalling) - A basic example of how to use the un/marshalling functionality of the java class.
- [java-from-typescript-type-with-options](./java-from-typescript-type-with-options/) - A basic example that shows how to generate a Java model from a TypeScript type input file along with user provided options.
17 changes: 17 additions & 0 deletions examples/java-from-typescript-type-with-options/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TODO: Generate Java model from a TypeScript Types file using processor options

This is a basic example of generating Java models using a Typescript type file as input and processor options. This can be extended to generating Go, C# and other supported language models.

## 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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to generate Java models from a TypeScript type file
along with specific options and should log expected output to console 1`] = `
Array [
"public class OuterData {
private Object inner;
private String name;
private Map<String, Object> additionalProperties;
public Object getInner() { return this.inner; }
public void setInner(Object inner) { this.inner = inner; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
]
`;
16 changes: 16 additions & 0 deletions examples/java-from-typescript-type-with-options/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; });
import {generate} from './index';

describe(`Should be able to generate Java models from a TypeScript type file
along with specific options`, () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
//GenerateWithOptions is called 4x, so even though we expect 1 model, we double it
expect(spy.mock.calls.length).toEqual(4);
expect(spy.mock.calls[1]).toMatchSnapshot();
});
});

31 changes: 31 additions & 0 deletions examples/java-from-typescript-type-with-options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { JavaGenerator } from '../../src';
import * as path from 'path';
import * as fs from 'fs';

const generator = new JavaGenerator(
{
processorOptions: {
typescript: {
compilerOptions: {
strictNullChecks: true
}
}
}
}
);

const file = path.resolve(__dirname, './typescriptFile.ts');
const fileContents = fs.readFileSync(path.resolve(__dirname, './typescriptFile.ts'),'utf-8');

export async function generate() : Promise<void> {
const models = await generator.generate({
fileContents,
baseFile: file
});

for (const model of models) {
console.log(model.result);
}
}

generate();
10 changes: 10 additions & 0 deletions examples/java-from-typescript-type-with-options/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/java-from-typescript-type-with-options/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "java-from-typescript-type-with-options" },
"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/java-from-typescript-type-with-options/typescriptFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface InnerData {
age: number;
name: string;
free: boolean;
}

export interface OuterData {
inner: InnerData;
name: string;
}
17 changes: 17 additions & 0 deletions examples/java-from-typescript-type/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generate Java from a TypeScript Types file

This is a basic example of generating Java models using a Typescript type file as input. This can be extended to generating Go, C# and other supported language models.

## 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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to generate Java model from a TypeScript type file and should log expected output to console 1`] = `
Array [
"public class InnerData {
private Double age;
private String name;
private Boolean free;
private Map<String, Object> additionalProperties;
public Double getAge() { return this.age; }
public void setAge(Double age) { this.age = age; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public Boolean getFree() { return this.free; }
public void setFree(Boolean free) { this.free = free; }
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
]
`;
14 changes: 14 additions & 0 deletions examples/java-from-typescript-type/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; });
import {generate} from './index';

describe('Should be able to generate Java model from a TypeScript type file', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
//Generate is called 4x, so even though we expect 1 model, we double it
expect(spy.mock.calls.length).toEqual(4);
expect(spy.mock.calls[1]).toMatchSnapshot();
});
});
21 changes: 21 additions & 0 deletions examples/java-from-typescript-type/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { JavaGenerator } from '../../src';
import * as path from 'path';
import * as fs from 'fs';

const generator = new JavaGenerator();

const file = path.resolve(__dirname, './typescriptFile.ts');
const fileContents = fs.readFileSync(path.resolve(__dirname, './typescriptFile.ts'),'utf-8');

export async function generate() : Promise<void> {
const models = await generator.generate({
fileContents,
baseFile: file
});

for (const model of models) {
console.log(model.result);
}
}

generate();
10 changes: 10 additions & 0 deletions examples/java-from-typescript-type/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/java-from-typescript-type/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "java-from-typescript-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"
}
}
9 changes: 9 additions & 0 deletions examples/java-from-typescript-type/typescriptFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type Shape = {
size: number;
}

export interface InnerData {
age: number;
name: string;
free: boolean;
}
Loading

0 comments on commit 737ade9

Please sign in to comment.