Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate example function for TypeScript models #568

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 104 additions & 91 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum Languages {
dart = 'dart',
python = 'python',
rust = 'rust',
kotlin='kotlin'
kotlin = 'kotlin'
}
const possibleLanguageValues = Object.values(Languages).join(', ');

Expand Down Expand Up @@ -61,7 +61,7 @@ export default class Models extends Command {
description: 'TypeScript specific, define the module system to be used.',
required: false,
default: 'ESM',

}),
tsIncludeComments: Flags.boolean({
description: 'TypeScript specific, if enabled add comments while generating models.',
Expand All @@ -80,6 +80,11 @@ export default class Models extends Command {
required: false,
default: false,
}),
tsExampleInstance: Flags.boolean({
description: 'Typescript specific, if enabled add example while generating models.',
required: false,
default: false,
}),
/**
* Go and Java specific package name to use for the generated models
*/
Expand Down Expand Up @@ -109,11 +114,11 @@ export default class Models extends Command {
}),
...validationFlags({ logDiagnostics: false }),
};

/* eslint-disable sonarjs/cognitive-complexity */
async run() {
const { args, flags } = await this.parse(Models);
const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, namespace, csharpAutoImplement, csharpArrayType, packageName, output } = flags;
const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsExampleInstance , namespace, csharpAutoImplement, csharpArrayType, packageName, output } = flags;
const { language, file } = args;
const inputFile = (await load(file)) || (await load());
const { document, status } = await parse(this, inputFile, flags);
Expand All @@ -140,95 +145,103 @@ export default class Models extends Command {
let fileOptions: any = {};
const presets = [];
switch (language) {
case Languages.typescript:
if (tsIncludeComments) {presets.push(TS_DESCRIPTION_PRESET);}
if (tsJsonBinPack) {
presets.push({
preset: TS_COMMON_PRESET,
options: {
marshalling: true
}
},
TS_JSONBINPACK_PRESET);
}
fileGenerator = new TypeScriptFileGenerator({
modelType: tsModelType as 'class' | 'interface',
enumType: tsEnumType as 'enum' | 'union',
presets
});
fileOptions = {
moduleSystem: tsModuleSystem,
exportType: tsExportType
};
break;
case Languages.python:
fileGenerator = new PythonFileGenerator();
break;
case Languages.rust:
fileGenerator = new RustFileGenerator();
break;
case Languages.csharp:
if (namespace === undefined) {
throw new Error('In order to generate models to C#, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
}

fileGenerator = new CSharpFileGenerator({
presets: csharpAutoImplement ? [
{
preset: CSHARP_DEFAULT_PRESET,
case Languages.typescript:
if (tsIncludeComments) { presets.push(TS_DESCRIPTION_PRESET); }
if (tsExampleInstance) {
presets.push({
preset: TS_COMMON_PRESET,
options: {
autoImplementedProperties: true
example : true
}
}
] : [],
collectionType: csharpArrayType as 'Array' | 'List'
});

fileOptions = {
namespace
};
break;
case Languages.golang:
if (packageName === undefined) {
throw new Error('In order to generate models to Go, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new GoFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.java:
if (packageName === undefined) {
throw new Error('In order to generate models to Java, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new JavaFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.javascript:
fileGenerator = new JavaScriptFileGenerator();
break;
case Languages.dart:
if (packageName === undefined) {
throw new Error('In order to generate models to Dart, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new DartFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.kotlin:
if (packageName === undefined) {
throw new Error('In order to generate models to Kotlin, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new KotlinFileGenerator();
fileOptions = {
packageName
};
break;
default:
throw new Error(`Could not determine generator for language ${language}, are you using one of the following values ${possibleLanguageValues}?`);
})
}
if (tsJsonBinPack) {
presets.push({
preset: TS_COMMON_PRESET,
options: {
marshalling: true
}
},
Comment on lines +159 to +164
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see that the same preset are added twice now, just with slightly different options? 🙂

This should not be the case, it can only be added once, and then the options should change slightly based on the parameters 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not getting added twice even if there are both flags in the command of tsExampleInstance and tsJsonBinPack. I think Modelina has the support for this where the same preset is added with multiple values of options then it gets combined to give the output. Just the order of the functions generated is getting changed somewhat rest is the same for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach would be to create an object variable

const options={
    example : tsExampleInstace ,
    marshalling : tsMarshalling
}

presets.push({
    preset: TS_COMMON_PRESET,
    options:options
})

and then use this while pushing the presets which can also resolve #460

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh you right, it's allowed as long as the options don't overlap or default behavior adds some stuff. Because otherwise it will be added twice 😄

TS_JSONBINPACK_PRESET);
}
fileGenerator = new TypeScriptFileGenerator({
modelType: tsModelType as 'class' | 'interface',
enumType: tsEnumType as 'enum' | 'union',
presets
});
fileOptions = {
moduleSystem: tsModuleSystem,
exportType: tsExportType
};
break;
case Languages.python:
fileGenerator = new PythonFileGenerator();
break;
case Languages.rust:
fileGenerator = new RustFileGenerator();
break;
case Languages.csharp:
if (namespace === undefined) {
throw new Error('In order to generate models to C#, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
}

fileGenerator = new CSharpFileGenerator({
presets: csharpAutoImplement ? [
{
preset: CSHARP_DEFAULT_PRESET,
options: {
autoImplementedProperties: true
}
}
] : [],
collectionType: csharpArrayType as 'Array' | 'List'
});

fileOptions = {
namespace
};
break;
case Languages.golang:
if (packageName === undefined) {
throw new Error('In order to generate models to Go, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new GoFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.java:
if (packageName === undefined) {
throw new Error('In order to generate models to Java, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new JavaFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.javascript:
fileGenerator = new JavaScriptFileGenerator();
break;
case Languages.dart:
if (packageName === undefined) {
throw new Error('In order to generate models to Dart, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new DartFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.kotlin:
if (packageName === undefined) {
throw new Error('In order to generate models to Kotlin, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new KotlinFileGenerator();
fileOptions = {
packageName
};
break;
default:
throw new Error(`Could not determine generator for language ${language}, are you using one of the following values ${possibleLanguageValues}?`);
}

if (output) {
Expand Down
31 changes: 20 additions & 11 deletions test/commands/generate/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('models', () => {
expect(ctx.stdout).toMatchSnapshot();
done();
});

test
.stderr()
.stdout()
Expand All @@ -35,8 +35,8 @@ describe('models', () => {
expect(ctx.stdout).toMatchSnapshot();
done();
});
describe('for TypeScript', () => {

describe('for TypeScript', () => {
test
.stderr()
.stdout()
Expand Down Expand Up @@ -68,9 +68,18 @@ describe('models', () => {
expect(ctx.stdout).toMatchSnapshot();
done();
});
test
.stderr()
.stdout()
.command([...generalOptions, 'typescript', './test/specification.yml', '--tsExampleInstance'])
.it('works when tsExampleInstance is set', (ctx, done) => {
expect(ctx.stderr).toEqual('');
expect(ctx.stdout).toMatchSnapShot();
done();
});
});

describe('for JavaScript', () => {
describe('for JavaScript', () => {
test
.stderr()
.stdout()
Expand All @@ -84,7 +93,7 @@ describe('models', () => {
});
});

describe('for Python', () => {
describe('for Python', () => {
test
.stderr()
.stdout()
Expand All @@ -98,7 +107,7 @@ describe('models', () => {
});
});

describe('for Rust', () => {
describe('for Rust', () => {
test
.stderr()
.stdout()
Expand Down Expand Up @@ -147,7 +156,7 @@ describe('models', () => {
test
.stderr()
.stdout()
.command([...generalOptions, 'csharp', './test/specification.yml', `-o=${ path.resolve(outputDir, './csharp')}`, '--namespace=\'asyncapi.models\'', '--csharpArrayType=List'])
.command([...generalOptions, 'csharp', './test/specification.yml', `-o=${ path.resolve(outputDir, './csharp')}`, '--namespace=\'asyncapi.models\'', '--csharpArrayType=List'])
.it('works when array type is provided', (ctx, done) => {
expect(ctx.stderr).toEqual('');
expect(ctx.stdout).toContain(
Expand Down Expand Up @@ -179,8 +188,8 @@ describe('models', () => {
done();
});
});
describe('for Go', () => {

describe('for Go', () => {
test
.stderr()
.stdout()
Expand All @@ -203,7 +212,7 @@ describe('models', () => {
});
});

describe('for Kotlin', () => {
describe('for Kotlin', () => {
test
.stderr()
.stdout()
Expand All @@ -226,7 +235,7 @@ describe('models', () => {
});
});

describe('for Dart', () => {
describe('for Dart', () => {
test
.stderr()
.stdout()
Expand Down