Skip to content

Commit

Permalink
feat: add file path as input (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilkanth987 authored Nov 22, 2023
1 parent 3558b26 commit 268bfd4
Show file tree
Hide file tree
Showing 25 changed files with 367 additions and 20 deletions.
17 changes: 17 additions & 0 deletions examples/file-uri-input/README.md
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
```
59 changes: 59 additions & 0 deletions examples/file-uri-input/__snapshots__/index.spec.ts.snap
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; }
}",
]
`;
26 changes: 26 additions & 0 deletions examples/file-uri-input/index.spec.ts
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();
});
});
26 changes: 26 additions & 0 deletions examples/file-uri-input/index.ts
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();
}
10 changes: 10 additions & 0 deletions examples/file-uri-input/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/file-uri-input/package.json
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"
}
}
23 changes: 23 additions & 0 deletions examples/file-uri-input/testasyncapi.yml
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
2 changes: 1 addition & 1 deletion src/generators/AbstractFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type FileGenerator = (content: string, toFile: string) => Promise<void>;
*/
export interface AbstractFileGenerator<RenderCompleteModelOptions> {
generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: RenderCompleteModelOptions
): Promise<OutputModel[]>;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/cplusplus/CplusplusFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CplusplusFileGenerator
* @param options
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options?: CplusplusRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/csharp/CSharpFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class CSharpFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: DeepPartial<CSharpRenderCompleteModelOptions>,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/dart/DartFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class DartFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: DartRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/go/GoFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class GoFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: GoRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/java/JavaFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class JavaFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: JavaRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/javascript/JavaScriptFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class JavaScriptFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options?: JavaScriptRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/kotlin/KotlinFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class KotlinFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: KotlinRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/php/PhpFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class PhpFileGenerator
* @param ensureFilesWritten
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options?: PhpRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/python/PythonFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class PythonFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: PythonRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
4 changes: 2 additions & 2 deletions src/generators/rust/RustFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class RustFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: RustRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down Expand Up @@ -50,7 +50,7 @@ export class RustFileGenerator
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToPackage(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options: RustRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/rust/RustGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export class RustGenerator extends AbstractGenerator<
}

async generateCompleteSupport(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
completeModelOptions: Partial<RustRenderCompleteModelOptions>,
options?: DeepPartial<RustOptions>
): Promise<OutputModel[]> {
Expand Down
2 changes: 1 addition & 1 deletion src/generators/template/TemplateFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TemplateFileGenerator
* @param options
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options?: TemplateRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/typescript/TypeScriptFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class TypeScriptFileGenerator extends TypeScriptGenerator {
* @param ensureFilesWritten verify that the files is completely written before returning, this can happen if the file system is swamped with write requests.
*/
public async generateToFiles(
input: Record<string, unknown> | InputMetaModel,
input: any | InputMetaModel,
outputDirectory: string,
options?: TypeScriptRenderCompleteModelOptions,
ensureFilesWritten = false
Expand Down
Loading

0 comments on commit 268bfd4

Please sign in to comment.