Skip to content

Commit

Permalink
reverts generator refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethaasan committed Sep 11, 2023
1 parent b5ad8b1 commit abd41d1
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 391 deletions.
43 changes: 13 additions & 30 deletions src/generators/AbstractGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,6 @@ export const defaultGeneratorOptions: CommonGeneratorOptions = {
}
};

export interface AbstractGeneratorRenderArgs<
Options extends CommonGeneratorOptions,
ConstrainedModel = ConstrainedMetaModel
> {
constrainedModel: ConstrainedModel;
inputModel: InputMetaModel;
options?: DeepPartial<Options>;
}

export interface AbstractGeneratorRenderCompleteModelArgs<
Options extends CommonGeneratorOptions,
RenderCompleteModelOptions
> {
constrainedModel: ConstrainedMetaModel;
inputModel: InputMetaModel;
completeOptions: Partial<RenderCompleteModelOptions>;
options?: DeepPartial<Options>;
}

/**
* Abstract generator which must be implemented by each language
*/
Expand All @@ -75,13 +56,15 @@ export abstract class AbstractGenerator<
) {}

public abstract render(
args: AbstractGeneratorRenderArgs<Options>
model: MetaModel,
inputModel: InputMetaModel,
options?: DeepPartial<Options>
): Promise<RenderOutput>;
public abstract renderCompleteModel(
args: AbstractGeneratorRenderCompleteModelArgs<
Options,
RenderCompleteModelOptions
>
model: MetaModel,
inputModel: InputMetaModel,
completeOptions: Partial<RenderCompleteModelOptions>,
options?: DeepPartial<Options>
): Promise<RenderOutput>;
public abstract constrainToMetaModel(
model: MetaModel,
Expand Down Expand Up @@ -199,12 +182,12 @@ export abstract class AbstractGenerator<
return Promise.all(
this.getConstrainedModels(inputModel).map(
async ({ constrainedModel, dependencyManager }) => {
const renderedOutput = await this.renderCompleteModel({
const renderedOutput = await this.renderCompleteModel(
constrainedModel,
inputModel,
completeOptions,
options: { dependencyManager } as DeepPartial<Options>
});
{ dependencyManager } as DeepPartial<Options>
);
return OutputModel.toOutputModel({
result: renderedOutput.result,
modelName: renderedOutput.renderedName,
Expand All @@ -226,13 +209,13 @@ export abstract class AbstractGenerator<
return Promise.all(
this.getConstrainedModels(inputModel).map(
async ({ constrainedModel, dependencyManager }) => {
const renderedOutput = await this.render({
const renderedOutput = await this.render(
constrainedModel,
inputModel,
options: {
{
dependencyManager
} as DeepPartial<Options>
});
);
return OutputModel.toOutputModel({
result: renderedOutput.result,
modelName: renderedOutput.renderedName,
Expand Down
55 changes: 21 additions & 34 deletions src/generators/cplusplus/CplusplusGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
AbstractGenerator,
AbstractGeneratorRenderArgs,
AbstractGeneratorRenderCompleteModelArgs,
CommonGeneratorOptions,
defaultGeneratorOptions
} from '../AbstractGenerator';
Expand Down Expand Up @@ -123,32 +121,26 @@ export class CplusplusGenerator extends AbstractGenerator<
* @param inputModel
*/
render(
args: AbstractGeneratorRenderArgs<CplusplusOptions>
model: ConstrainedMetaModel,
inputModel: InputMetaModel,
options?: DeepPartial<CplusplusOptions>
): Promise<RenderOutput> {
const optionsToUse = CplusplusGenerator.getOptions({
...this.options,
...args.options
...options
});
if (isReservedCplusplusKeyword(optionsToUse.namespace)) {
throw new Error(
`You cannot use reserved C++ keyword (${optionsToUse.namespace}) as namespace, please use another.`
);
}
if (args.constrainedModel instanceof ConstrainedObjectModel) {
return this.renderClass(
args.constrainedModel,
args.inputModel,
optionsToUse
);
} else if (args.constrainedModel instanceof ConstrainedEnumModel) {
return this.renderEnum(
args.constrainedModel,
args.inputModel,
optionsToUse
);
if (model instanceof ConstrainedObjectModel) {
return this.renderClass(model, inputModel, optionsToUse);
} else if (model instanceof ConstrainedEnumModel) {
return this.renderEnum(model, inputModel, optionsToUse);
}
Logger.warn(
`C++ generator, cannot generate this type of model, ${args.constrainedModel.name}`
`C++ generator, cannot generate this type of model, ${model.name}`
);
return Promise.resolve(
RenderOutput.toRenderOutput({
Expand All @@ -169,35 +161,30 @@ export class CplusplusGenerator extends AbstractGenerator<
* @param options used to render the full output
*/
async renderCompleteModel(
args: AbstractGeneratorRenderCompleteModelArgs<
CplusplusOptions,
CplusplusRenderCompleteModelOptions
>
model: ConstrainedMetaModel,
inputModel: InputMetaModel,
completeModelOptions: Partial<CplusplusRenderCompleteModelOptions>,
options: DeepPartial<CplusplusOptions>
): Promise<RenderOutput> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const completeModelOptionsToUse = mergePartialAndDefault(
CplusplusGenerator.defaultCompleteModelOptions,
args.completeOptions
completeModelOptions
) as CplusplusRenderCompleteModelOptions;
const optionsToUse = CplusplusGenerator.getOptions({
...this.options,
...args.options
...options
});
const dependencyManagerToUse = this.getDependencyManager(optionsToUse);

const outputModel = await this.render({
...args,
options: {
...optionsToUse,
dependencyManager: dependencyManagerToUse
}
const outputModel = await this.render(model, inputModel, {
...optionsToUse,
dependencyManager: dependencyManagerToUse
});

const imports = args.constrainedModel
.getNearestDependencies()
.map((model) => {
return `#include "${model.name}.hpp"`;
});
const imports = model.getNearestDependencies().map((model) => {
return `#include "${model.name}.hpp"`;
});
const formattedOutputResult = FormatHelpers.indent(
outputModel.result,
2,
Expand Down
49 changes: 17 additions & 32 deletions src/generators/csharp/CSharpGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
AbstractGenerator,
AbstractGeneratorRenderArgs,
AbstractGeneratorRenderCompleteModelArgs,
CommonGeneratorOptions,
defaultGeneratorOptions
} from '../AbstractGenerator';
Expand Down Expand Up @@ -148,29 +146,26 @@ export class CSharpGenerator extends AbstractGenerator<
* @param options used to render the full output
*/
async renderCompleteModel(
args: AbstractGeneratorRenderCompleteModelArgs<
CSharpOptions,
CSharpRenderCompleteModelOptions
>
model: ConstrainedMetaModel,
inputModel: InputMetaModel,
completeModelOptions: DeepPartial<CSharpRenderCompleteModelOptions>,
options: DeepPartial<CSharpOptions>
): Promise<RenderOutput> {
const completeModelOptionsToUse = mergePartialAndDefault(
CSharpGenerator.defaultCompleteModelOptions,
args.completeOptions
completeModelOptions
) as CSharpRenderCompleteModelOptions;
const optionsToUse = CSharpGenerator.getCSharpOptions({
...this.options,
...args.options
...options
});
if (isReservedCSharpKeyword(completeModelOptionsToUse.namespace)) {
throw new Error(
`You cannot use reserved CSharp keyword (${completeModelOptionsToUse.namespace}) as namespace, please use another.`
);
}

const outputModel = await this.render({
...args,
options: optionsToUse
});
const outputModel = await this.render(model, inputModel);

const outputDependencies =
outputModel.dependencies.length === 0
Expand All @@ -194,34 +189,24 @@ ${FormatHelpers.indent(
}

render(
args: AbstractGeneratorRenderArgs<CSharpOptions>
model: ConstrainedMetaModel,
inputModel: InputMetaModel,
options?: DeepPartial<CSharpOptions>
): Promise<RenderOutput> {
const optionsToUse = CSharpGenerator.getCSharpOptions({
...this.options,
...args.options
...options
});
if (args.constrainedModel instanceof ConstrainedObjectModel) {
if (model instanceof ConstrainedObjectModel) {
if (this.options.modelType === 'record') {
return this.renderRecord(
args.constrainedModel,
args.inputModel,
optionsToUse
);
return this.renderRecord(model, inputModel, optionsToUse);
}
return this.renderClass(
args.constrainedModel,
args.inputModel,
optionsToUse
);
} else if (args.constrainedModel instanceof ConstrainedEnumModel) {
return this.renderEnum(
args.constrainedModel,
args.inputModel,
optionsToUse
);
return this.renderClass(model, inputModel, optionsToUse);
} else if (model instanceof ConstrainedEnumModel) {
return this.renderEnum(model, inputModel, optionsToUse);
}
Logger.warn(
`C# generator, cannot generate this type of model, ${args.constrainedModel.name}`
`C# generator, cannot generate this type of model, ${model.name}`
);
return Promise.resolve(
RenderOutput.toRenderOutput({
Expand Down
42 changes: 17 additions & 25 deletions src/generators/dart/DartGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
AbstractGenerator,
AbstractGeneratorRenderArgs,
AbstractGeneratorRenderCompleteModelArgs,
CommonGeneratorOptions,
defaultGeneratorOptions
} from '../AbstractGenerator';
Expand Down Expand Up @@ -123,27 +121,21 @@ export class DartGenerator extends AbstractGenerator<
* @param inputModel
*/
render(
args: AbstractGeneratorRenderArgs<DartOptions>
model: ConstrainedMetaModel,
inputModel: InputMetaModel,
options?: DeepPartial<DartOptions>
): Promise<RenderOutput> {
const optionsToUse = DartGenerator.getDartOptions({
...this.options,
...args.options
...options
});
if (args.constrainedModel instanceof ConstrainedObjectModel) {
return this.renderClass(
args.constrainedModel,
args.inputModel,
optionsToUse
);
} else if (args.constrainedModel instanceof ConstrainedEnumModel) {
return this.renderEnum(
args.constrainedModel,
args.inputModel,
optionsToUse
);
if (model instanceof ConstrainedObjectModel) {
return this.renderClass(model, inputModel, optionsToUse);
} else if (model instanceof ConstrainedEnumModel) {
return this.renderEnum(model, inputModel, optionsToUse);
}
Logger.warn(
`Dart generator, cannot generate this type of model, ${args.constrainedModel.name}`
`Dart generator, cannot generate this type of model, ${model.name}`
);
return Promise.resolve(
RenderOutput.toRenderOutput({
Expand All @@ -164,18 +156,18 @@ export class DartGenerator extends AbstractGenerator<
* @param options used to render the full output
*/
async renderCompleteModel(
args: AbstractGeneratorRenderCompleteModelArgs<
DartOptions,
DartRenderCompleteModelOptions
>
model: ConstrainedMetaModel,
inputModel: InputMetaModel,
completeModelOptions: Partial<DartRenderCompleteModelOptions>,
options: DeepPartial<DartOptions>
): Promise<RenderOutput> {
const completeModelOptionsToUse = mergePartialAndDefault(
DartGenerator.defaultCompleteModelOptions,
args.completeOptions
completeModelOptions
) as DartRenderCompleteModelOptions;
const optionsToUse = DartGenerator.getDartOptions({
...this.options,
...args.options
...options
});
const dependencyManagerToUse = this.getDependencyManager(optionsToUse);
if (isReservedDartKeyword(completeModelOptionsToUse.packageName)) {
Expand All @@ -184,9 +176,9 @@ export class DartGenerator extends AbstractGenerator<
);
}

const outputModel = await this.render({ ...args, options: optionsToUse });
const outputModel = await this.render(model, inputModel, optionsToUse);
const modelDependencies = dependencyManagerToUse.renderAllModelDependencies(
args.constrainedModel,
model,
completeModelOptionsToUse.packageName
);
const outputContent = `${modelDependencies}
Expand Down
Loading

0 comments on commit abd41d1

Please sign in to comment.