Skip to content

Commit

Permalink
fix: update JavaRenderer and remove unnecessary code from presets (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Feb 24, 2021
1 parent 0e21f49 commit 7679550
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 91 deletions.
35 changes: 24 additions & 11 deletions src/generators/java/JavaRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export abstract class JavaRenderer extends AbstractRenderer<JavaOptions> {
if (model.$ref !== undefined) {
return model.$ref;
}
return this.toClassType(this.toJavaType(model.type, model));
const format = model.getFromSchema('format');
return this.toClassType(this.toJavaType(format || model.type, model));
}

toJavaType(type: string | undefined, model: CommonModel): string {
Expand Down Expand Up @@ -83,16 +84,28 @@ export abstract class JavaRenderer extends AbstractRenderer<JavaOptions> {
}
}

renderAnnotation(annotation: any): string;
renderAnnotation(annotations: Array<any>): string;
renderAnnotation(annotation: Array<any> | any): string {
if (Array.isArray(annotation)) {
return annotation.map(ann => this.renderAnnotation(ann)).join(' ');
}
const name = `@${FormatHelpers.upperFirst(annotation.name)}`;
if (annotation.body) {
return `${name}(${annotation.body})`;
renderComments(lines: string | string[]): string {
lines = FormatHelpers.breakLines(lines);
return `/**
${lines.map(line => ` * ${line}`).join('\n')}
*/`;
}

renderAnnotation(annotationName: string, value?: any | Record<string, any>): string {
const name = `@${FormatHelpers.upperFirst(annotationName)}`;
let values = undefined;
if (value !== undefined) {
if (typeof value === 'object') {
values = Object.entries(value || {}).map(([paramName, value]) => {
if (paramName && value !== undefined) {
return `${paramName}=${value}`;
}
return value;
}).filter(v => v !== undefined).join(', ');
} else {
values = `${value}`;
}
}
return name;
return values !== undefined ? `${name}(${values})` : name;
}
}
21 changes: 11 additions & 10 deletions src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class ClassRenderer extends JavaRenderer {
await this.runAdditionalContentPreset(),
];

return `public class ${this.model.$id} {
const formattedName = this.model.$id && FormatHelpers.toPascalCase(this.model.$id);
return `public class ${formattedName} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
Expand All @@ -31,36 +32,36 @@ ${this.indent(this.renderBlock(content, 2))}
const content: string[] = [];

for (const [propertyName, property] of Object.entries(properties)) {
const rendererProperty = await this.runPropertyPreset(propertyName, property, this.model);
const rendererProperty = await this.runPropertyPreset(propertyName, property);
content.push(rendererProperty);
}

return this.renderBlock(content);
}

async runPropertyPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('property', { propertyName, property, parentModel });
async runPropertyPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('property', { propertyName, property });
}

async renderAccessors(): Promise<string> {
const properties = this.model.properties || {};
const content: string[] = [];

for (const [propertyName, property] of Object.entries(properties)) {
const getter = await this.runGetterPreset(propertyName, property, this.model);
const setter = await this.runSetterPreset(propertyName, property, this.model);
const getter = await this.runGetterPreset(propertyName, property);
const setter = await this.runSetterPreset(propertyName, property);
content.push(this.renderBlock([getter, setter]));
}

return this.renderBlock(content, 2);
}

async runGetterPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('getter', { propertyName, property, parentModel });
async runGetterPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('getter', { propertyName, property });
}

async runSetterPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('setter', { propertyName, property, parentModel });
async runSetterPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('setter', { propertyName, property });
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/generators/java/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JavaRenderer } from '../JavaRenderer';

import { CommonModel, EnumPreset } from '../../../models';
import { EnumPreset } from '../../../models';
import { FormatHelpers } from '../../../helpers';

/**
Expand All @@ -15,7 +15,8 @@ export class EnumRenderer extends JavaRenderer {
await this.runAdditionalContentPreset(),
];

return `public enum ${this.model.$id} {
const formattedName = this.model.$id && FormatHelpers.toPascalCase(this.model.$id);
return `public enum ${formattedName} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
Expand All @@ -25,7 +26,7 @@ ${this.indent(this.renderBlock(content, 2))}
const items: string[] = [];

for (const value of enums) {
const renderedItem = await this.runItemPreset(value, this.model);
const renderedItem = await this.runItemPreset(value);
items.push(renderedItem);
}

Expand Down Expand Up @@ -55,8 +56,8 @@ ${this.indent(this.renderBlock(content, 2))}
}
}

async runItemPreset(item: any, parentModel: CommonModel): Promise<string> {
return this.runPreset('item', { item, parentModel });
async runItemPreset(item: any): Promise<string> {
return this.runPreset('item', { item });
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/generators/javascript/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class ClassRenderer extends JavaScriptRenderer {
await this.runAdditionalContentPreset(),
];

return `class ${this.model.$id} {
const formattedName = this.model.$id && FormatHelpers.toPascalCase(this.model.$id);
return `class ${formattedName} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
Expand All @@ -31,20 +32,20 @@ ${this.indent(this.renderBlock(content, 2))}
const content: string[] = [];

for (const [propertyName, property] of Object.entries(properties)) {
const getter = await this.runGetterPreset(propertyName, property, this.model);
const setter = await this.runSetterPreset(propertyName, property, this.model);
const getter = await this.runGetterPreset(propertyName, property);
const setter = await this.runSetterPreset(propertyName, property);
content.push(this.renderBlock([getter, setter]));
}

return this.renderBlock(content, 2);
}

async runGetterPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('getter', { propertyName, property, parentModel });
async runGetterPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('getter', { propertyName, property });
}

async runSetterPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('setter', { propertyName, property, parentModel });
async runSetterPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('setter', { propertyName, property });
}
}

Expand Down
26 changes: 18 additions & 8 deletions src/generators/typescript/TypeScriptRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class TypeScriptRenderer extends AbstractRenderer<TypeScriptOpti
if (type === undefined) {
return 'any';
}
switch (type) {
switch (type) {
case 'string':
return 'string';
case 'integer':
Expand All @@ -54,12 +54,22 @@ export abstract class TypeScriptRenderer extends AbstractRenderer<TypeScriptOpti
}
}

renderTypeSignature(type: CommonModel | CommonModel[], isRequired = true): string {
renderTypeSignature(type: CommonModel | CommonModel[], {
isRequired = true,
orUndefined = false,
}: {
isRequired?: boolean;
orUndefined?: boolean;
} = {}): string {
if (this.options.renderTypes === false) {
return '';
}

const annotation = isRequired ? ':' : '?:';
return `${annotation} ${this.renderType(type)}`;
let t = this.renderType(type);
t = orUndefined ? `${t} | undefined` : t;

return `${annotation} ${t}`;
}

renderComments(lines: string | string[]): string {
Expand All @@ -74,20 +84,20 @@ ${lines.map(line => ` * ${line}`).join('\n')}
const content: string[] = [];

for (const [propertyName, property] of Object.entries(properties)) {
const rendererProperty = await this.runPropertyPreset(propertyName, property, this.model);
const rendererProperty = await this.runPropertyPreset(propertyName, property);
content.push(rendererProperty);
}

return this.renderBlock(content);
}

renderProperty(propertyName: string, property: CommonModel, parentModel: CommonModel): string {
renderProperty(propertyName: string, property: CommonModel): string {
const name = FormatHelpers.toCamelCase(propertyName);
const signature = this.renderTypeSignature(property, parentModel.isRequired(propertyName));
const signature = this.renderTypeSignature(property, { isRequired: this.model.isRequired(propertyName) });
return `${name}${signature};`;
}

async runPropertyPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('property', { propertyName, property, parentModel });
async runPropertyPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('property', { propertyName, property });
}
}
41 changes: 22 additions & 19 deletions src/generators/typescript/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class ClassRenderer extends TypeScriptRenderer {
await this.runAdditionalContentPreset(),
];

return `class ${this.model.$id} {
const formattedName = this.model.$id && FormatHelpers.toPascalCase(this.model.$id);
return `class ${formattedName} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
Expand All @@ -31,20 +32,20 @@ ${this.indent(this.renderBlock(content, 2))}
const content: string[] = [];

for (const [propertyName, property] of Object.entries(properties)) {
const getter = await this.runGetterPreset(propertyName, property, this.model);
const setter = await this.runSetterPreset(propertyName, property, this.model);
const getter = await this.runGetterPreset(propertyName, property);
const setter = await this.runSetterPreset(propertyName, property);
content.push(this.renderBlock([getter, setter]));
}

return this.renderBlock(content, 2);
}

runGetterPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('getter', { propertyName, property, parentModel });
runGetterPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('getter', { propertyName, property });
}

runSetterPreset(propertyName: string, property: CommonModel, parentModel: CommonModel): Promise<string> {
return this.runPreset('setter', { propertyName, property, parentModel });
runSetterPreset(propertyName: string, property: CommonModel): Promise<string> {
return this.runPreset('setter', { propertyName, property });
}
}

Expand All @@ -60,7 +61,7 @@ export const TS_DEFAULT_CLASS_PRESET: ClassPreset<ClassRenderer> = {
});
const ctorProperties: string[] = [];
for (const [propertyName, property] of Object.entries(properties)) {
const rendererProperty = renderer.renderProperty(propertyName, property, model).replace(';', ',');
const rendererProperty = renderer.renderProperty(propertyName, property).replace(';', ',');
ctorProperties.push(rendererProperty);
}

Expand All @@ -70,18 +71,20 @@ ${renderer.indent(renderer.renderBlock(ctorProperties))}
${renderer.indent(renderer.renderBlock(assigments))}
}`;
},
property({ renderer, propertyName, property, parentModel }) {
return `private _${renderer.renderProperty(propertyName, property, parentModel)}`;
property({ renderer, propertyName, property }) {
return `private _${renderer.renderProperty(propertyName, property)}`;
},
getter({ renderer, propertyName, property }) {
propertyName = FormatHelpers.toCamelCase(propertyName);
const signature = renderer.renderTypeSignature(property);
return `get ${propertyName}()${signature} { return this._${propertyName}; }`;
getter({ renderer, model, propertyName, property }) {
const isRequired = model.isRequired(propertyName);
const formattedName = FormatHelpers.toCamelCase(propertyName);
const signature = renderer.renderTypeSignature(property, { orUndefined: !isRequired });
return `get ${formattedName}()${signature} { return this._${formattedName}; }`;
},
setter({ renderer, propertyName, property }) {
propertyName = FormatHelpers.toCamelCase(propertyName);
const signature = renderer.renderTypeSignature(property);
const arg = `${propertyName}${signature}`;
return `set ${propertyName}(${arg}) { this._${propertyName} = ${propertyName}; }`;
setter({ renderer, model, propertyName, property }) {
const isRequired = model.isRequired(propertyName);
const formattedName = FormatHelpers.toCamelCase(propertyName);
const signature = renderer.renderTypeSignature(property, { orUndefined: !isRequired });
const arg = `${formattedName}${signature}`;
return `set ${formattedName}(${arg}) { this._${formattedName} = ${formattedName}; }`;
},
};
11 changes: 6 additions & 5 deletions src/generators/typescript/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypeScriptRenderer } from '../TypeScriptRenderer';

import { CommonModel, EnumPreset } from '../../../models';
import { EnumPreset } from '../../../models';
import { FormatHelpers } from '../../../helpers';

/**
Expand All @@ -15,7 +15,8 @@ export class EnumRenderer extends TypeScriptRenderer {
await this.runAdditionalContentPreset(),
];

return `enum ${this.model.$id} {
const formattedName = this.model.$id && FormatHelpers.toPascalCase(this.model.$id);
return `enum ${formattedName} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
Expand All @@ -25,15 +26,15 @@ ${this.indent(this.renderBlock(content, 2))}
const items: string[] = [];

for (const item of enums) {
const renderedItem = await this.runItemPreset(item, this.model);
const renderedItem = await this.runItemPreset(item);
items.push(renderedItem);
}

return this.renderBlock(items);
}

runItemPreset(item: any, parentModel: CommonModel): Promise<string> {
return this.runPreset('item', { item, parentModel });
runItemPreset(item: any): Promise<string> {
return this.runPreset('item', { item });
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/generators/typescript/renderers/InterfaceRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TypeScriptRenderer } from '../TypeScriptRenderer';

import { InterfacePreset } from '../../../models';
import { FormatHelpers } from '../../../helpers';

/**
* Renderer for TypeScript's `interface` type
Expand All @@ -14,7 +15,8 @@ export class InterfaceRenderer extends TypeScriptRenderer {
await this.runAdditionalContentPreset(),
];

return `interface ${this.model.$id} {
const formattedName = this.model.$id && FormatHelpers.toPascalCase(this.model.$id);
return `interface ${formattedName} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
Expand All @@ -24,7 +26,7 @@ export const TS_DEFAULT_INTERFACE_PRESET: InterfacePreset<InterfaceRenderer> = {
async self({ renderer }) {
return `export ${await renderer.defaultSelf()}`;
},
property({ renderer, propertyName, property, parentModel }) {
return renderer.renderProperty(propertyName, property, parentModel);
property({ renderer, propertyName, property }) {
return renderer.renderProperty(propertyName, property);
},
};
Loading

0 comments on commit 7679550

Please sign in to comment.