Skip to content

Commit

Permalink
fix linting and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jan 17, 2023
1 parent 5ab42aa commit 9e70c11
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 63 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/modelina",
"version": "1.0.0-next.39",
"version": "1.0.0-next.40",
"description": "Library for generating data models based on inputs such as AsyncAPI, OpenAPI, or JSON Schema documents",
"license": "Apache-2.0",
"homepage": "https://github.com/asyncapi/modelina",
Expand Down
6 changes: 2 additions & 4 deletions src/generators/csharp/CSharpConstrainer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ConstrainedEnumValueModel, ConstrainedObjectPropertyModel } from '../../models';
import { TypeMapping } from '../../helpers';
import { defaultEnumKeyConstraints, defaultEnumValueConstraints } from './constrainer/EnumConstrainer';
import { defaultModelNameConstraints } from './constrainer/ModelNameConstrainer';
import { defaultPropertyKeyConstraints } from './constrainer/PropertyKeyConstrainer';
import { CSharpOptions, CSharpTypeMapping } from './CSharpGenerator';
import { CSharpDependencyManager } from './CSharpDependencyManager';
import { CSharpTypeMapping } from './CSharpGenerator';

function getFullTypeDefinition(typeName: string, partOfProperty: ConstrainedObjectPropertyModel | undefined) {
return partOfProperty?.required ?? true
Expand All @@ -30,7 +28,7 @@ const fromEnumValueToType = (enumValueModel: ConstrainedEnumValueModel): string
}
};

export const CSharpDefaultTypeMapping: TypeMapping<CSharpOptions, CSharpDependencyManager> = {
export const CSharpDefaultTypeMapping: CSharpTypeMapping = {
Object({ constrainedModel, partOfProperty }): string {
return getFullTypeDefinition(constrainedModel.name, partOfProperty);
},
Expand Down
8 changes: 3 additions & 5 deletions src/generators/csharp/CSharpGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import { CSharpDefaultConstraints, CSharpDefaultTypeMapping } from './CSharpCons
import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials';
import { CSharpDependencyManager } from './CSharpDependencyManager';

export type CSharpTypeMapping = TypeMapping<CSharpOptions, CSharpDependencyManager>
export interface CSharpOptions extends CommonGeneratorOptions<CSharpPreset> {
collectionType: 'List' | 'Array';
typeMapping: CSharpTypeMapping;
typeMapping: TypeMapping<CSharpOptions, CSharpDependencyManager>;
constraints: Constraints;
autoImplementedProperties: boolean;
}
export type CSharpTypeMapping = TypeMapping<CSharpOptions, CSharpDependencyManager>;

export interface CSharpRenderCompleteModelOptions {
namespace: string
Expand All @@ -41,7 +41,6 @@ export class CSharpGenerator extends AbstractGenerator<CSharpOptions, CSharpRend
dependencyManager: () => { return {} as CSharpDependencyManager;}
};


static defaultCompleteModelOptions: CSharpRenderCompleteModelOptions = {
namespace: 'Asyncapi.Models'
};
Expand All @@ -53,11 +52,10 @@ export class CSharpGenerator extends AbstractGenerator<CSharpOptions, CSharpRend
super('CSharp', realizedOptions);
}


/**
* Returns the CSharp options by merging custom options with default ones.
*/
static getCSharpOptions(options?: DeepPartial<CSharpOptions>): CSharpOptions {
static getCSharpOptions(options?: DeepPartial<CSharpOptions>): CSharpOptions {
const optionsToUse = mergePartialAndDefault(this.defaultOptions, options) as CSharpOptions;
//Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager)
if (options?.dependencyManager === undefined) {
Expand Down
12 changes: 12 additions & 0 deletions src/generators/dart/DartDependencyManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FormatHelpers } from '../../helpers';
import { ConstrainedMetaModel } from '../../models';
import { AbstractDependencyManager } from '../AbstractDependencyManager';
import { DartOptions } from './DartGenerator';

Expand All @@ -8,4 +10,14 @@ export class DartDependencyManager extends AbstractDependencyManager {
) {
super(dependencies);
}

renderImport(model: ConstrainedMetaModel, packageName: string): string {
return `import 'package:${packageName}/${FormatHelpers.snakeCase(model.name)}.dart';`;
}

renderAllModelDependencies(model: ConstrainedMetaModel, packageName: string): string {
return model.getNearestDependencies().map((dependencyModel) => {
return this.renderImport(dependencyModel, packageName);
}).join('\n');
}
}
14 changes: 5 additions & 9 deletions src/generators/dart/DartGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import {ClassRenderer} from './renderers/ClassRenderer';
import {EnumRenderer} from './renderers/EnumRenderer';
import {isReservedDartKeyword} from './Constants';
import {Logger} from '../../';
import {FormatHelpers} from '../../helpers/FormatHelpers';
import { DartDefaultConstraints, DartDefaultTypeMapping } from './DartConstrainer';
import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials';
import { DartDependencyManager } from './DartDependencyManager';
export type DartTypeMapping = TypeMapping<DartOptions, DartDependencyManager>;
export interface DartOptions extends CommonGeneratorOptions<DartPreset> {
collectionType?: 'List';
typeMapping: DartTypeMapping;
typeMapping: TypeMapping<DartOptions, DartDependencyManager>;
constraints: Constraints;
}
export type DartTypeMapping = TypeMapping<DartOptions, DartDependencyManager>;

export interface DartRenderCompleteModelOptions {
packageName: string;
Expand All @@ -45,11 +44,10 @@ export class DartGenerator extends AbstractGenerator<DartOptions, DartRenderComp
super('Dart', realizedOptions);
}


/**
* Returns the Dart options by merging custom options with default ones.
*/
static getDartOptions(options?: DeepPartial<DartOptions>): DartOptions {
static getDartOptions(options?: DeepPartial<DartOptions>): DartOptions {
const optionsToUse = mergePartialAndDefault(DartGenerator.defaultOptions, options) as DartOptions;
//Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager)
if (options?.dependencyManager === undefined) {
Expand Down Expand Up @@ -127,10 +125,8 @@ export class DartGenerator extends AbstractGenerator<DartOptions, DartRenderComp
}

const outputModel = await this.render(model, inputModel, optionsToUse);
const modelDependencies = model.getNearestDependencies().map((dependencyModelName) => {
return `import 'package:${completeModelOptionsToUse.packageName}/${FormatHelpers.snakeCase(dependencyModelName.name)}.dart';`;
});
const outputContent = `${modelDependencies.join('\n')}
const modelDependencies = dependencyManagerToUse.renderAllModelDependencies(model, completeModelOptionsToUse.packageName);
const outputContent = `${modelDependencies}
${outputModel.dependencies.join('\n')}
${outputModel.result}`;
return RenderOutput.toRenderOutput({
Expand Down
7 changes: 3 additions & 4 deletions src/generators/go/GoGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { Logger } from '../../utils/LoggingInterface';
import { GoDefaultConstraints, GoDefaultTypeMapping } from './GoConstrainer';
import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials';
import { GoDependencyManager } from './GoDependencyManager';
export type GoTypeMapping = TypeMapping<GoOptions, GoDependencyManager>;

export interface GoOptions extends CommonGeneratorOptions<GoPreset> {
typeMapping: GoTypeMapping;
typeMapping: TypeMapping<GoOptions, GoDependencyManager>;
constraints: Constraints
}
export type GoTypeMapping = TypeMapping<GoOptions, GoDependencyManager>;

export interface GoRenderCompleteModelOptions {
packageName: string
Expand All @@ -38,7 +38,6 @@ export class GoGenerator extends AbstractGenerator<GoOptions, GoRenderCompleteMo
packageName: 'AsyncapiModels'
};


constructor(
options?: DeepPartial<GoOptions>,
) {
Expand All @@ -49,7 +48,7 @@ export class GoGenerator extends AbstractGenerator<GoOptions, GoRenderCompleteMo
/**
* Returns the Go options by merging custom options with default ones.
*/
static getGoOptions(options?: DeepPartial<GoOptions>): GoOptions {
static getGoOptions(options?: DeepPartial<GoOptions>): GoOptions {
const optionsToUse = mergePartialAndDefault(GoGenerator.defaultOptions, options) as GoOptions;
//Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager)
if (options?.dependencyManager === undefined) {
Expand Down
11 changes: 11 additions & 0 deletions src/generators/java/JavaDependencyManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConstrainedMetaModel } from '../../models';
import { AbstractDependencyManager } from '../AbstractDependencyManager';
import { JavaOptions } from './JavaGenerator';

Expand All @@ -8,4 +9,14 @@ export class JavaDependencyManager extends AbstractDependencyManager {
) {
super(dependencies);
}

renderImport(model: ConstrainedMetaModel, packageName: string): string {
return `import ${packageName}.${model.name};`;
}

renderAllModelDependencies(model: ConstrainedMetaModel, packageName: string): string {
return model.getNearestDependencies().map((dependencyModel) => {
return this.renderImport(dependencyModel, packageName);
}).join('\n');
}
}
13 changes: 5 additions & 8 deletions src/generators/java/JavaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import { JavaDefaultConstraints, JavaDefaultTypeMapping } from './JavaConstraine
import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials';
import { JavaDependencyManager } from './JavaDependencyManager';

export type JavaTypeMapping = TypeMapping<JavaOptions, JavaDependencyManager>;

export interface JavaOptions extends CommonGeneratorOptions<JavaPreset> {
collectionType: 'List' | 'Array';
typeMapping: JavaTypeMapping;
typeMapping: TypeMapping<JavaOptions, JavaDependencyManager>;
constraints: Constraints;
}
export type JavaTypeMapping = TypeMapping<JavaOptions, JavaDependencyManager>;
export interface JavaRenderCompleteModelOptions {
packageName: string
}
Expand Down Expand Up @@ -125,12 +124,10 @@ export class JavaGenerator extends AbstractGenerator<JavaOptions, JavaRenderComp
throw new Error(`You cannot use reserved Java keyword (${completeModelOptionsToUse.packageName}) as package name, please use another.`);
}

const outputModel = await this.render(model, inputModel);
const modelDependencies = model.getNearestDependencies().map((dependencyModel) => {
return `import ${completeModelOptionsToUse.packageName}.${dependencyModel.name};`;
});
const outputModel = await this.render(model, inputModel, optionsToUse);
const modelDependencies = dependencyManagerToUse.renderAllModelDependencies(model, completeModelOptionsToUse.packageName);
const outputContent = `package ${completeModelOptionsToUse.packageName};
${modelDependencies.join('\n')}
${modelDependencies}
${outputModel.dependencies.join('\n')}
${outputModel.result}`;
return RenderOutput.toRenderOutput({result: outputContent, renderedName: outputModel.renderedName, dependencies: outputModel.dependencies});
Expand Down
9 changes: 4 additions & 5 deletions src/generators/javascript/JavaScriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import {
defaultGeneratorOptions
} from '../AbstractGenerator';
import { ConstrainedMetaModel, ConstrainedObjectModel, InputMetaModel, MetaModel, RenderOutput } from '../../models';
import { TypeMapping, Constraints, split, constrainMetaModel, renderJavaScriptDependency, SplitOptions } from '../../helpers';
import { TypeMapping, Constraints, split, constrainMetaModel, SplitOptions } from '../../helpers';
import { JavaScriptPreset, JS_DEFAULT_PRESET } from './JavaScriptPreset';
import { ClassRenderer } from './renderers/ClassRenderer';
import { Logger } from '../../';
import { JavaScriptDefaultConstraints, JavaScriptDefaultTypeMapping } from './JavaScriptConstrainer';
import { DeepPartial, mergePartialAndDefault } from '../../utils';
import { JavaScriptDependencyManager } from './JavaScriptDependencyManager';
export type JavaScriptTypeMapping = TypeMapping<JavaScriptOptions, JavaScriptDependencyManager>;
export interface JavaScriptOptions extends CommonGeneratorOptions<JavaScriptPreset> {
typeMapping: JavaScriptTypeMapping;
typeMapping: TypeMapping<JavaScriptOptions, JavaScriptDependencyManager>;
constraints: Constraints;
moduleSystem: 'ESM' | 'CJS';
}
export type JavaScriptTypeMapping = TypeMapping<JavaScriptOptions, JavaScriptDependencyManager>;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface JavaScriptRenderCompleteModelOptions { }

Expand All @@ -41,11 +41,10 @@ export class JavaScriptGenerator extends AbstractGenerator<JavaScriptOptions, Ja
super('JavaScript', realizedOptions);
}


/**
* Returns the JavaScript options by merging custom options with default ones.
*/
static getJavaScriptOptions(options?: DeepPartial<JavaScriptOptions>): JavaScriptOptions {
static getJavaScriptOptions(options?: DeepPartial<JavaScriptOptions>): JavaScriptOptions {
const optionsToUse = mergePartialAndDefault(JavaScriptGenerator.defaultOptions, options) as JavaScriptOptions;
//Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager)
if (options?.dependencyManager === undefined) {
Expand Down
3 changes: 1 addition & 2 deletions src/generators/python/PythonDependencyManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConstrainedMetaModel } from 'models';
import { ConstrainedMetaModel } from '../../models';
import { AbstractDependencyManager } from '../AbstractDependencyManager';
import { PythonOptions } from './PythonGenerator';

Expand All @@ -10,7 +10,6 @@ export class PythonDependencyManager extends AbstractDependencyManager {
super(dependencies);
}


/**
* Simple helper function to render a dependency based on the module system that the user defines.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/generators/python/PythonGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import { constrainMetaModel, Constraints } from '../../helpers/ConstrainHelpers'
import { PythonDefaultConstraints, PythonDefaultTypeMapping } from './PythonConstrainer';
import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials';
import { PythonDependencyManager } from './PythonDependencyManager';
export type PythonTypeMapping = TypeMapping<PythonOptions, PythonDependencyManager>;

export interface PythonOptions extends CommonGeneratorOptions<PythonPreset> {
typeMapping: PythonTypeMapping;
typeMapping: TypeMapping<PythonOptions, PythonDependencyManager>;
constraints: Constraints;
importsStyle: 'explicit' | 'implicit';
}
export type PythonTypeMapping = TypeMapping<PythonOptions, PythonDependencyManager>;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface PythonRenderCompleteModelOptions { }

Expand All @@ -47,7 +47,7 @@ export class PythonGenerator extends AbstractGenerator<PythonOptions, PythonRend
/**
* Returns the Python options by merging custom options with default ones.
*/
static getPythonOptions(options?: DeepPartial<PythonOptions>): PythonOptions {
static getPythonOptions(options?: DeepPartial<PythonOptions>): PythonOptions {
const optionsToUse = mergePartialAndDefault(PythonGenerator.defaultOptions, options) as PythonOptions;
//Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager)
if (options?.dependencyManager === undefined) {
Expand Down Expand Up @@ -125,7 +125,7 @@ export class PythonGenerator extends AbstractGenerator<PythonOptions, PythonRend
const optionsToUse = PythonGenerator.getPythonOptions({...this.options, ...options});
const dependencyManagerToUse = this.getDependencyManager(optionsToUse);
const outputModel = await this.render(model, inputModel, {dependencyManager: dependencyManagerToUse});
const modelDependencies = model.getNearestDependencies().map((model) => {return dependencyManagerToUse.renderDependency(model)});
const modelDependencies = model.getNearestDependencies().map((model) => {return dependencyManagerToUse.renderDependency(model);});
const outputContent = `${modelDependencies.join('\n')}
${outputModel.dependencies.join('\n')}
${outputModel.result}`;
Expand Down
5 changes: 2 additions & 3 deletions src/generators/rust/RustConstrainer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { defaultEnumKeyConstraints, defaultEnumValueConstraints } from './constrainer/EnumConstrainer';
import { defaultModelNameConstraints } from './constrainer/ModelNameConstrainer';
import { defaultPropertyKeyConstraints } from './constrainer/PropertyKeyConstrainer';
import { RustOptions, RustTypeMapping } from './RustGenerator';
import { FormatHelpers, Constraints, TypeMapping } from '../../helpers';
import { RustTypeMapping } from './RustGenerator';
import { FormatHelpers, Constraints } from '../../helpers';
import { ConstrainedAnyModel, ConstrainedArrayModel, ConstrainedDictionaryModel, ConstrainedEnumModel, ConstrainedFloatModel, ConstrainedMetaModel, ConstrainedObjectModel, ConstrainedReferenceModel, ConstrainedStringModel, ConstrainedTupleModel, ConstrainedUnionModel } from '../../models';
import { RustDependencyManager } from './RustDependencyManager';

export function deriveHash(model: ConstrainedMetaModel): boolean {
// float primitives and std::collection::HashMap do not implement Hash trait
Expand Down
1 change: 0 additions & 1 deletion src/generators/rust/RustDependencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export class RustDependencyManager extends AbstractDependencyManager {
) {
super(dependencies);
}

}
8 changes: 4 additions & 4 deletions src/generators/rust/RustGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
CommonGeneratorOptions,
defaultGeneratorOptions
} from '../AbstractGenerator';
import { OutputModel, InputMetaModel, RenderOutput, ConstrainedObjectModel, ConstrainedEnumModel, ConstrainedMetaModel, MetaModel, ConstrainedTupleModel, ConstrainedUnionModel, UnionModel } from '../../models';
import { OutputModel, InputMetaModel, RenderOutput, ConstrainedObjectModel, ConstrainedEnumModel, ConstrainedMetaModel, MetaModel, ConstrainedTupleModel, ConstrainedUnionModel } from '../../models';
import { constrainMetaModel, Constraints, IndentationTypes, split, SplitOptions, TypeMapping } from '../../helpers';
import { RustPreset, RUST_DEFAULT_PRESET } from './RustPreset';
import { StructRenderer } from './renderers/StructRenderer';
Expand All @@ -15,11 +15,11 @@ import { UnionRenderer } from './renderers/UnionRenderer';
import { PackageRenderer } from './renderers/PackageRenderer';
import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials';
import { RustDependencyManager } from './RustDependencyManager';
export type RustTypeMapping = TypeMapping<RustOptions, RustDependencyManager>;
export interface RustOptions extends CommonGeneratorOptions<RustPreset> {
typeMapping: RustTypeMapping;
typeMapping: TypeMapping<RustOptions, RustDependencyManager>;
constraints: Constraints
}
export type RustTypeMapping = TypeMapping<RustOptions, RustDependencyManager>;

export enum RustPackageFeatures {
json,
Expand Down Expand Up @@ -165,7 +165,7 @@ export class RustGenerator extends AbstractGenerator<RustOptions, RustRenderComp
inputModel: InputMetaModel,
completeModelOptions: Partial<RustRenderCompleteModelOptions>,
options: DeepPartial<RustOptions>): Promise<RenderOutput> {
const _ = RustGenerator.getRustCompleteOptions(completeModelOptions);
//const _ = RustGenerator.getRustCompleteOptions(completeModelOptions);
const optionsToUse = RustGenerator.getRustOptions({...this.options, ...options});
Logger.debug('Generating complete models with options: ', optionsToUse);
const outputModel = await this.render(model, inputModel);
Expand Down
Loading

0 comments on commit 9e70c11

Please sign in to comment.