Skip to content

Commit

Permalink
feat: add hashcode and equals flags for csharp code generation (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz Gornicki <[email protected]>
  • Loading branch information
kaushik-rishi and derberg authored May 24, 2023
1 parent 7cc496d commit c7d730a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
43 changes: 32 additions & 11 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator } from '@asyncapi/modelina';
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, CSHARP_COMMON_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import Command from '../../base';
import { load } from '../../models/SpecificationFile';
Expand Down Expand Up @@ -91,7 +91,7 @@ export default class Models extends Command {
}),

/**
* C++ and C# specific namespace to use for the generated models
* C++ and C# and PHP specific namespace to use for the generated models
*/
namespace: Flags.string({
description: 'C#, C++ and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp`,`c++` or `php`.',
Expand All @@ -113,13 +113,23 @@ export default class Models extends Command {
required: false,
default: 'Array'
}),
csharpHashcode: Flags.boolean({
description: 'C# specific, generate the models with the GetHashCode method overwritten',
required: false,
default: false
}),
csharpEqual: Flags.boolean({
description: 'C# specific, generate the models with the Equal method overwritten',
required: false,
default: false
}),
...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, namespace, csharpAutoImplement, csharpArrayType, csharpHashcode, csharpEqual, packageName, output } = flags;
const { language, file } = args;
const inputFile = (await load(file)) || (await load());
const { document, status } = await parse(this, inputFile, flags);
Expand Down Expand Up @@ -178,15 +188,26 @@ export default class Models extends Command {
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
}
if (csharpAutoImplement) {
presets.push({
preset: CSHARP_DEFAULT_PRESET,
options: {
autoImplementedProperties: true
}
});
}
if (csharpHashcode || csharpEqual) {
presets.push({
preset: CSHARP_COMMON_PRESET,
options: {
hashCode: csharpHashcode,
equals: csharpEqual
}
] : [],
});
}

fileGenerator = new CSharpFileGenerator({
presets,
collectionType: csharpArrayType as 'Array' | 'List'
});

Expand Down
48 changes: 36 additions & 12 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 @@ -34,8 +34,8 @@ describe('models', () => {
expect(ctx.stdout).toMatchSnapshot();
done();
});
describe('for TypeScript', () => {

describe('for TypeScript', () => {
test
.stderr()
.stdout()
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('models', () => {
});
});

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

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

describe('for Rust', () => {
describe('for Rust', () => {
test
.stderr()
.stdout()
Expand Down Expand Up @@ -146,7 +146,31 @@ 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\'', '--csharpHashcode'])
.it('works when hash code flag is passed', (ctx, done) => {
expect(ctx.stderr).toEqual('');
expect(ctx.stdout).toContain(
'Successfully generated the following models: '
);
done();
});

test
.stderr()
.stdout()
.command([...generalOptions, 'csharp', './test/specification.yml', `-o=${ path.resolve(outputDir, './csharp')}`, '--namespace=\'asyncapi.models\'', '--csharpEqual'])
.it('works when equal flag is passed', (ctx, done) => {
expect(ctx.stderr).toEqual('');
expect(ctx.stdout).toContain(
'Successfully generated the following models: '
);
done();
});

test
.stderr()
.stdout()
.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 @@ -201,8 +225,8 @@ describe('models', () => {
done();
});
});
describe('for Go', () => {

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

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

describe('for Dart', () => {
describe('for Dart', () => {
test
.stderr()
.stdout()
Expand All @@ -270,7 +294,7 @@ describe('models', () => {
done();
});
});

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

0 comments on commit c7d730a

Please sign in to comment.