-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
176 additions
and
51 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
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,2 +1,2 @@ | ||
# Disable specific duplicate code since it would introduce more complexity to reduce it. | ||
sonar.cpd.exclusions=src/generators/javascript/renderers/ClassRenderer.ts,src/generators/java/renderers/ClassRenderer.ts,src/generators/typescript/renderers/ClassRenderer.ts,src/generators/javascript/JavaScriptRenderer.ts,src/generators/typescript/renderers/EnumRenderer.ts,src/generators/java/renderers/EnumRenderer.ts | ||
sonar.cpd.exclusions=src/generators/javascript/renderers/ClassRenderer.ts,src/generators/java/renderers/ClassRenderer.ts,src/generators/typescript/renderers/ClassRenderer.ts,src/generators/javascript/JavaScriptRenderer.ts,src/generators/java/renderers/ClassRenderer.ts,src/generators/typescript/TypeScriptRenderer.ts,src/generators/typescript/renderers/EnumRenderer.ts,src/generators/java/renderers/EnumRenderer.ts |
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,24 +1,24 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import { AbstractRenderer } from '../AbstractRenderer'; | ||
import { Preset, CommonModel, CommonPreset, PresetArgs } from '../../models'; | ||
import { Preset, CommonModel, CommonPreset, PresetArgs, EnumPreset } from '../../models'; | ||
import { StructRenderer, GO_DEFAULT_STRUCT_PRESET } from './renderers/StructRenderer'; | ||
import { EnumRenderer, GO_DEFAULT_ENUM_PRESET } from './renderers/EnumRenderer'; | ||
|
||
export interface FieldArgs { | ||
fieldName: string; | ||
field: CommonModel; | ||
} | ||
|
||
export interface StructPreset<R extends AbstractRenderer, O extends object = any> extends CommonPreset<R, O> { | ||
ctor?: (args: PresetArgs<R, O>) => Promise<string> | string; | ||
field?: (args: PresetArgs<R, O> & FieldArgs) => Promise<string> | string; | ||
getter?: (args: PresetArgs<R, O> & FieldArgs) => Promise<string> | string; | ||
setter?: (args: PresetArgs<R, O> & FieldArgs) => Promise<string> | string; | ||
} | ||
|
||
export type GoPreset = Preset<{ | ||
struct: StructPreset<StructRenderer>; | ||
enum: EnumPreset<EnumRenderer> | ||
}>; | ||
|
||
export const GO_DEFAULT_PRESET: GoPreset = { | ||
struct: GO_DEFAULT_STRUCT_PRESET, | ||
enum: GO_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
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,38 @@ | ||
import { GoRenderer } from '../GoRenderer'; | ||
import { EnumPreset, CommonModel } from '../../../models'; | ||
import { FormatHelpers } from '../../../helpers'; | ||
|
||
/** | ||
* Renderer for Go's `enum` type | ||
* | ||
* @extends GoRenderer | ||
*/ | ||
export class EnumRenderer extends GoRenderer { | ||
public defaultSelf(): string { | ||
const formattedName = this.model.$id && FormatHelpers.toPascalCase(this.model.$id); | ||
const type = this.enumType(this.model); | ||
const doc = formattedName && this.renderCommentForEnumType(formattedName, type); | ||
|
||
return `${doc} | ||
type ${formattedName} ${type}`; | ||
} | ||
|
||
enumType(model: CommonModel): string { | ||
if (this.model.type === undefined || Array.isArray(this.model.type)) { | ||
return 'interface{}'; | ||
} | ||
|
||
return this.toGoType(this.model.type, model); | ||
} | ||
|
||
renderCommentForEnumType(name: string, type: string): string { | ||
const globalType = type === 'interface{}' ? 'mixed types' : type; | ||
return this.renderComments(`${name} represents an enum of ${globalType}.`); | ||
} | ||
} | ||
|
||
export const GO_DEFAULT_ENUM_PRESET: EnumPreset<EnumRenderer> = { | ||
self({ renderer }) { | ||
return renderer.defaultSelf(); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,29 +1,19 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { TypeScriptGenerator, JavaGenerator, JavaScriptGenerator } from '../../src'; | ||
import { TypeScriptGenerator, JavaGenerator, JavaScriptGenerator, GoGenerator } from '../../src'; | ||
describe('AsyncAPI JSON Schema file', () => { | ||
test('should be generated in TypeScript', async () => { | ||
const inputSchemaString = fs.readFileSync(path.resolve(__dirname, './docs/AsyncAPI_2_0_0.json'), 'utf8'); | ||
const inputSchema = JSON.parse(inputSchemaString); | ||
const generator = new TypeScriptGenerator(); | ||
const generatedContent = await generator.generate(inputSchema); | ||
expect(generatedContent).not.toBeUndefined(); | ||
expect(generatedContent.length).toBeGreaterThan(1); | ||
}); | ||
test('should be generated in Java', async () => { | ||
const inputSchemaString = fs.readFileSync(path.resolve(__dirname, './docs/AsyncAPI_2_0_0.json'), 'utf8'); | ||
const inputSchema = JSON.parse(inputSchemaString); | ||
const generator = new JavaGenerator(); | ||
const generatedContent = await generator.generate(inputSchema); | ||
expect(generatedContent).not.toBeUndefined(); | ||
expect(generatedContent.length).toBeGreaterThan(1); | ||
}); | ||
test('should be generated in JavaScript', async () => { | ||
const inputSchemaString = fs.readFileSync(path.resolve(__dirname, './docs/AsyncAPI_2_0_0.json'), 'utf8'); | ||
const inputSchema = JSON.parse(inputSchemaString); | ||
const generator = new JavaScriptGenerator(); | ||
const generatedContent = await generator.generate(inputSchema); | ||
expect(generatedContent).not.toBeUndefined(); | ||
expect(generatedContent.length).toBeGreaterThan(1); | ||
describe.each([ | ||
['TypeScript', new TypeScriptGenerator()], | ||
['Java', new JavaGenerator()], | ||
['Javascript', new JavaScriptGenerator()], | ||
['Go', new GoGenerator()], | ||
])('code generated in %s', (_, generator) => { | ||
test('should not be empty', async () => { | ||
const inputSchemaString = fs.readFileSync(path.resolve(__dirname, './docs/AsyncAPI_2_0_0.json'), 'utf8'); | ||
const inputSchema = JSON.parse(inputSchemaString); | ||
const generatedContent = await generator.generate(inputSchema); | ||
expect(generatedContent).not.toBeUndefined(); | ||
expect(generatedContent.length).toBeGreaterThan(1); | ||
}); | ||
}); | ||
}); |
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