-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add file path as input (#1601)
- Loading branch information
1 parent
3558b26
commit 268bfd4
Showing
25 changed files
with
367 additions
and
20 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generate models using file URI as input | ||
|
||
A basic example of how to generate models using file URI as input for AsyncAPI | ||
|
||
## 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,59 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to generate models using file URI as input to output folder and should log expected output to console 1`] = ` | ||
Array [ | ||
"class AnonymousSchema_1 { | ||
private _displayName?: string; | ||
private _email?: string; | ||
private _additionalProperties?: Map<string, any>; | ||
constructor(input: { | ||
displayName?: string, | ||
email?: string, | ||
additionalProperties?: Map<string, any>, | ||
}) { | ||
this._displayName = input.displayName; | ||
this._email = input.email; | ||
this._additionalProperties = input.additionalProperties; | ||
} | ||
get displayName(): string | undefined { return this._displayName; } | ||
set displayName(displayName: string | undefined) { this._displayName = displayName; } | ||
get email(): string | undefined { return this._email; } | ||
set email(email: string | undefined) { this._email = email; } | ||
get additionalProperties(): Map<string, any> | undefined { return this._additionalProperties; } | ||
set additionalProperties(additionalProperties: Map<string, any> | undefined) { this._additionalProperties = additionalProperties; } | ||
}", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render models using file URI as input and should log expected output to console 1`] = ` | ||
Array [ | ||
"class AnonymousSchema_1 { | ||
private _displayName?: string; | ||
private _email?: string; | ||
private _additionalProperties?: Map<string, any>; | ||
constructor(input: { | ||
displayName?: string, | ||
email?: string, | ||
additionalProperties?: Map<string, any>, | ||
}) { | ||
this._displayName = input.displayName; | ||
this._email = input.email; | ||
this._additionalProperties = input.additionalProperties; | ||
} | ||
get displayName(): string | undefined { return this._displayName; } | ||
set displayName(displayName: string | undefined) { this._displayName = displayName; } | ||
get email(): string | undefined { return this._email; } | ||
set email(email: string | undefined) { this._email = email; } | ||
get additionalProperties(): Map<string, any> | undefined { return this._additionalProperties; } | ||
set additionalProperties(additionalProperties: Map<string, any> | undefined) { this._additionalProperties = additionalProperties; } | ||
}", | ||
] | ||
`; |
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 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { | ||
return; | ||
}); | ||
import { generate, generateToFiles } from './index'; | ||
|
||
describe('Should be able to render models using file URI as input', () => { | ||
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(); | ||
}); | ||
}); | ||
|
||
describe('Should be able to generate models using file URI as input to output folder', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generateToFiles(); | ||
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,26 @@ | ||
import path from 'path'; | ||
import { TypeScriptFileGenerator, TypeScriptGenerator } from '../../src'; | ||
|
||
const generator = new TypeScriptGenerator(); | ||
const fileGenerator = new TypeScriptFileGenerator(); | ||
|
||
export async function generate(): Promise<void> { | ||
const fileUri = `file://${path.resolve(__dirname, './testasyncapi.yml')}`; | ||
const models = await generator.generate(fileUri); | ||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
|
||
export async function generateToFiles(): Promise<void> { | ||
const outputFolder = './examples/file-uri-input/output'; | ||
const fileUri = `file://${path.resolve(__dirname, './testasyncapi.yml')}`; | ||
const models = await fileGenerator.generateToFiles(fileUri, outputFolder); | ||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
if (require.main === module) { | ||
generate(); | ||
generateToFiles(); | ||
} |
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" : "file-uri-input" }, | ||
"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,23 @@ | ||
asyncapi: '2.6.0' | ||
info: | ||
title: Account Service | ||
version: 1.0.0 | ||
description: This service is in charge of processing user signups | ||
channels: | ||
user/signedup: | ||
subscribe: | ||
message: | ||
$ref: '#/components/messages/UserSignedUp' | ||
components: | ||
messages: | ||
UserSignedUp: | ||
payload: | ||
type: object | ||
properties: | ||
displayName: | ||
type: string | ||
description: Name of the user | ||
email: | ||
type: string | ||
format: email | ||
description: Email of the user |
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
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
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
Oops, something went wrong.