Skip to content

Commit

Permalink
feat: added file path as input
Browse files Browse the repository at this point in the history
  • Loading branch information
nilkanth987 committed Nov 13, 2023
1 parent 69dd4e4 commit e781736
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
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
37 changes: 32 additions & 5 deletions src/generators/AbstractGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import { InputProcessor } from '../processors';
import { IndentationTypes } from '../helpers';
import { DeepPartial, isPresetWithOptions } from '../utils';
import { AbstractDependencyManager } from './AbstractDependencyManager';
import Parser, { fromFile } from '@asyncapi/parser';

export interface CommonGeneratorOptions<
P extends Preset = Preset,
DependencyManager extends
AbstractDependencyManager = AbstractDependencyManager
AbstractDependencyManager = AbstractDependencyManager

Check failure on line 22 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Insert `··`
> {
indentation?: {
type: IndentationTypes;
Expand All @@ -27,6 +28,7 @@ export interface CommonGeneratorOptions<
defaultPreset?: P;
presets?: Presets<P>;
processorOptions?: ProcessorOptions;
inputProcessor?: InputProcessorOptions;
/**
* This dependency manager type serves two functions.
* 1. It can be used to provide a factory for generate functions
Expand Down Expand Up @@ -63,6 +65,16 @@ export interface AbstractGeneratorRenderCompleteModelArgs<
options?: DeepPartial<Options>;
}

export interface InputProcessorOptions {
type?: InputType

Check failure on line 69 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Insert `;`
}

export enum InputType {
FILE = 'file',
DOCUMENT = 'document',
OBJECT = 'object'
}

/**
* Abstract generator which must be implemented by each language
*/
Expand All @@ -73,7 +85,7 @@ export abstract class AbstractGenerator<
constructor(
public readonly languageName: string,
public readonly options: Options
) {}
) { }

Check failure on line 88 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Delete `·`

public abstract render(
args: AbstractGeneratorRenderArgs<Options>
Expand Down Expand Up @@ -165,7 +177,7 @@ export abstract class AbstractGenerator<
for (const unionConstrainedModel of unionConstrainedModelsWithDepManager) {
if (
unionConstrainedModel.constrainedModel instanceof
ConstrainedUnionModel &&
ConstrainedUnionModel &&

Check failure on line 180 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Replace `··········` with `············`
unionConstrainedModel.constrainedModel.union.some(
(m) =>
m.name === constrainedModel.name &&
Expand Down Expand Up @@ -259,8 +271,7 @@ export abstract class AbstractGenerator<
protected async processInput(
input: any | InputMetaModel
): Promise<InputMetaModel> {
const rawInputModel =
input instanceof InputMetaModel ? input : await this.process(input);
const rawInputModel = await this.getRawInputModel(input)

Check failure on line 274 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Insert `;`

Check failure on line 274 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Missing semicolon

//Split out the models based on the language specific requirements of which models is rendered separately
const splitOutModels: { [key: string]: MetaModel } = {};
Expand All @@ -274,6 +285,22 @@ export abstract class AbstractGenerator<
return rawInputModel;
}

protected async getRawInputModel(
input: any | InputMetaModel
): Promise<InputMetaModel> {
const inputProcessorOptions = this.options.inputProcessor

Check failure on line 291 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Insert `;`

Check failure on line 291 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Missing semicolon
switch (inputProcessorOptions?.type) {
case InputType.FILE:
const parser = new Parser();

Check failure on line 294 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Unexpected lexical declaration in case block
const { document, diagnostics } = await fromFile(parser, input).parse();

Check failure on line 295 in src/generators/AbstractGenerator.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Unexpected lexical declaration in case block
// TODO : Want to log the diagnostics
if (!document) throw new Error('Invalid file input')
return this.process(document as any)
default:
return input instanceof InputMetaModel ? input : this.process(input);
}
}

/**
* Get all presets (default and custom ones from options) for a given preset type (class, enum, etc).
*/
Expand Down
21 changes: 21 additions & 0 deletions test/generators/AbstractGenerator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';
import { InputType } from '../../src';
import {
InputMetaModel,
AnyModel,
Expand Down Expand Up @@ -50,6 +52,25 @@ describe('AbstractGenerator', () => {
expect(outputModels[0].modelName).toEqual('TestName');
});

test('generate() should process file input', async () => {
const newGenerator = new TestGenerator({
inputProcessor: { type: InputType.FILE }
});
const outputModels = await newGenerator.generate(path.resolve('test/generators/testasyncapi.yml'));
expect(outputModels[0].result).toEqual('anonymous_schema_1');
expect(outputModels[0].modelName).toEqual('TestName');
});

test('generateCompleteModels() should process InputMetaModel instance', async () => {
const newGenerator = new TestGenerator({
inputProcessor: { type: InputType.FILE }
});
const outputModels = await newGenerator.generate(path.resolve('test/generators/testasyncapi.yml'));

expect(outputModels[0].result).toEqual('anonymous_schema_1');
expect(outputModels[0].modelName).toEqual('TestName');
});

test('should `process` function return InputMetaModel', async () => {
const doc: any = { type: 'string', $id: 'test' };
const commonInputModel = await generator.process(doc);
Expand Down
23 changes: 23 additions & 0 deletions test/generators/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

0 comments on commit e781736

Please sign in to comment.