-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added C# class and enum generator (#316)
- Loading branch information
1 parent
e57110b
commit 1dfbe58
Showing
21 changed files
with
973 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
FROM openjdk:16.0.1-jdk-slim-buster | ||
|
||
# Install updates | ||
RUN apt-get update -yq \ | ||
&& apt-get install -yq curl \ | ||
&& curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \ | ||
&& apt-get install -yq nodejs \ | ||
&& curl -fsSL https://golang.org/dl/go1.16.5.linux-amd64.tar.gz | tar -C /usr/local -xz | ||
&& apt-get install -yq curl | ||
|
||
# Install nodejs | ||
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \ | ||
&& apt-get install -yq nodejs | ||
|
||
# Install golang | ||
RUN curl -fsSL https://golang.org/dl/go1.16.5.linux-amd64.tar.gz | tar -C /usr/local -xz | ||
ENV PATH="${PATH}:/usr/local/go/bin" | ||
|
||
# Install dotnet SDK | ||
RUN apt install apt-transport-https dirmngr gnupg ca-certificates -yq \ | ||
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \ | ||
&& echo "deb https://download.mono-project.com/repo/debian stable-buster main" | tee /etc/apt/sources.list.d/mono-official-stable.list \ | ||
&& apt update -yq \ | ||
&& apt install mono-devel -yq | ||
|
||
# Setup library | ||
COPY package-lock.json . | ||
RUN npm install | ||
COPY . . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
AbstractGenerator, | ||
CommonGeneratorOptions, | ||
defaultGeneratorOptions, | ||
} from '../AbstractGenerator'; | ||
import { CommonModel, CommonInputModel, RenderOutput } from '../../models'; | ||
import { TypeHelpers, ModelKind, CommonNamingConvention, CommonNamingConventionImplementation } from '../../helpers'; | ||
import { CSharpPreset, CSHARP_DEFAULT_PRESET } from './CSharpPreset'; | ||
import { EnumRenderer } from './renderers/EnumRenderer'; | ||
import { ClassRenderer } from './renderers/ClassRenderer'; | ||
|
||
export interface CSharpOptions extends CommonGeneratorOptions<CSharpPreset> { | ||
namingConvention?: CommonNamingConvention; | ||
} | ||
|
||
/** | ||
* Generator for CSharp | ||
*/ | ||
export class CSharpGenerator extends AbstractGenerator<CSharpOptions> { | ||
static defaultOptions: CSharpOptions = { | ||
...defaultGeneratorOptions, | ||
defaultPreset: CSHARP_DEFAULT_PRESET, | ||
namingConvention: CommonNamingConventionImplementation | ||
}; | ||
|
||
constructor( | ||
options: CSharpOptions = CSharpGenerator.defaultOptions, | ||
) { | ||
super('CSharp', CSharpGenerator.defaultOptions, options); | ||
} | ||
|
||
render(model: CommonModel, inputModel: CommonInputModel): Promise<RenderOutput> { | ||
const kind = TypeHelpers.extractKind(model); | ||
switch (kind) { | ||
case ModelKind.OBJECT: | ||
return this.renderClass(model, inputModel); | ||
case ModelKind.ENUM: | ||
return this.renderEnum(model, inputModel); | ||
} | ||
|
||
return Promise.resolve(RenderOutput.toRenderOutput({ result: '', dependencies: [] })); | ||
} | ||
|
||
async renderEnum(model: CommonModel, inputModel: CommonInputModel): Promise<RenderOutput> { | ||
const presets = this.getPresets('enum'); | ||
const renderer = new EnumRenderer(this.options, this, presets, model, inputModel); | ||
const result = await renderer.runSelfPreset(); | ||
return RenderOutput.toRenderOutput({ result, dependencies: renderer.dependencies }); | ||
} | ||
|
||
async renderClass(model: CommonModel, inputModel: CommonInputModel): Promise<RenderOutput> { | ||
const presets = this.getPresets('class'); | ||
const renderer = new ClassRenderer(this.options, this, presets, model, inputModel); | ||
const result = await renderer.runSelfPreset(); | ||
return RenderOutput.toRenderOutput({ result, dependencies: renderer.dependencies }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import { Preset, EnumPreset, ClassPreset } from '../../models'; | ||
import { ClassRenderer, CSHARP_DEFAULT_CLASS_PRESET } from './renderers/ClassRenderer'; | ||
import { CSHARP_DEFAULT_ENUM_PRESET, EnumRenderer } from './renderers/EnumRenderer'; | ||
|
||
export type CSharpPreset = Preset<{ | ||
class: ClassPreset<ClassRenderer>; | ||
enum: EnumPreset<EnumRenderer> | ||
}>; | ||
|
||
export const CSHARP_DEFAULT_PRESET: CSharpPreset = { | ||
class: CSHARP_DEFAULT_CLASS_PRESET, | ||
enum: CSHARP_DEFAULT_ENUM_PRESET, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { AbstractRenderer } from '../AbstractRenderer'; | ||
import { CSharpGenerator, CSharpOptions } from './CSharpGenerator'; | ||
import { CommonModel, CommonInputModel, Preset, PropertyType } from '../../models'; | ||
import { FormatHelpers } from '../../helpers/FormatHelpers'; | ||
import { isReservedCSharpKeyword } from './Constants'; | ||
|
||
/** | ||
* Common renderer for CSharp types | ||
* | ||
* @extends AbstractRenderer | ||
*/ | ||
export abstract class CSharpRenderer extends AbstractRenderer<CSharpOptions> { | ||
constructor( | ||
options: CSharpOptions, | ||
generator: CSharpGenerator, | ||
presets: Array<[Preset, unknown]>, | ||
model: CommonModel, | ||
inputModel: CommonInputModel, | ||
) { | ||
super(options, generator, presets, model, inputModel); | ||
} | ||
|
||
/** | ||
* Renders the name of a type based on provided generator option naming convention type function. | ||
* | ||
* This is used to render names of models and then later used if that class is referenced from other models. | ||
* | ||
* @param name | ||
* @param model | ||
*/ | ||
nameType(name: string | undefined, model?: CommonModel): string { | ||
return this.options?.namingConvention?.type | ||
? this.options.namingConvention.type(name, { model: model || this.model, inputModel: this.inputModel, isReservedKeyword: isReservedCSharpKeyword(`${name}`) }) | ||
: name || ''; | ||
} | ||
|
||
/** | ||
* Renders the name of a property based on provided generator option naming convention property function. | ||
* | ||
* @param propertyName | ||
* @param property | ||
*/ | ||
nameProperty(propertyName: string | undefined, property?: CommonModel): string { | ||
return this.options?.namingConvention?.property | ||
? this.options.namingConvention.property(propertyName, { model: this.model, inputModel: this.inputModel, property, isReservedKeyword: isReservedCSharpKeyword(`${propertyName}`) }) | ||
: propertyName || ''; | ||
} | ||
|
||
runPropertyPreset(propertyName: string, property: CommonModel, type: PropertyType = PropertyType.property): Promise<string> { | ||
return this.runPreset('property', { propertyName, property, type }); | ||
} | ||
|
||
renderType(model: CommonModel): string { | ||
if (model.$ref !== undefined) { | ||
return this.nameType(model.$ref); | ||
} | ||
|
||
if (Array.isArray(model.type)) { | ||
return model.type.length > 1 ? 'dynamic' : `${this.toCSharpType(model.type[0], model)}`; | ||
} | ||
|
||
return this.toCSharpType(model.type, model); | ||
} | ||
|
||
renderComments(lines: string | string[]): string { | ||
lines = FormatHelpers.breakLines(lines); | ||
return lines.map(line => `// ${line}`).join('\n'); | ||
} | ||
|
||
toCSharpType(type: string | undefined, model: CommonModel): string { | ||
if (type === undefined) { | ||
return 'dynamic'; | ||
} | ||
|
||
switch (type) { | ||
case 'string': | ||
return 'string'; | ||
case 'integer': | ||
return 'int'; | ||
case 'number': | ||
return 'float'; | ||
case 'boolean': | ||
return 'bool'; | ||
case 'object': | ||
return 'object'; | ||
case 'array': { | ||
if (Array.isArray(model.items)) { | ||
return model.items.length > 1? 'dynamic[]' : `${this.renderType(model.items[0])}[]`; | ||
} | ||
const arrayType = model.items ? this.renderType(model.items) : 'dynamic'; | ||
return `${arrayType}[]`; | ||
} | ||
default: return type; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
export const RESERVED_CSHARP_KEYWORDS = [ | ||
'abstract', | ||
'as', | ||
'base', | ||
'bool', | ||
'break', | ||
'byte', | ||
'case', | ||
'catch', | ||
'char', | ||
'checked', | ||
'class', | ||
'const', | ||
'continue', | ||
'decimal', | ||
'default', | ||
'delegate', | ||
'do', | ||
'double', | ||
'else', | ||
'enum', | ||
'event', | ||
'explicit', | ||
'extern', | ||
'false', | ||
'finally', | ||
'fixed', | ||
'float', | ||
'for', | ||
'foreach', | ||
'goto', | ||
'if', | ||
'implicit', | ||
'in', | ||
'int', | ||
'interface', | ||
'internal', | ||
'is', | ||
'lock', | ||
'long', | ||
'namespace', | ||
'new', | ||
'null', | ||
'object', | ||
'operator', | ||
'out', | ||
'override', | ||
'params', | ||
'private', | ||
'protected', | ||
'public', | ||
'readonly', | ||
'ref', | ||
'return', | ||
'sbyte', | ||
'sealed', | ||
'short', | ||
'sizeof', | ||
'stackalloc', | ||
'static', | ||
'string', | ||
'struct', | ||
'switch', | ||
'this', | ||
'throw', | ||
'true', | ||
'try', | ||
'typeof', | ||
'uint', | ||
'ulong', | ||
'unchecked', | ||
'unsafe', | ||
'ushort', | ||
'using', | ||
'virtual', | ||
'void', | ||
'volatile', | ||
'while' | ||
]; | ||
|
||
export function isReservedCSharpKeyword(word: string): boolean { | ||
return RESERVED_CSHARP_KEYWORDS.includes(word); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './CSharpGenerator'; | ||
export { CSHARP_DEFAULT_PRESET } from './CSharpPreset'; | ||
export type { CSharpPreset } from './CSharpPreset'; |
Oops, something went wrong.