From c268bcf073d72db5a1e8f5e0a9cb71f6c27f8168 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 7 Jun 2022 14:33:42 +0200 Subject: [PATCH 1/5] chore: add example --- docs/languages/Csharp.md | 8 ++++ examples/README.md | 1 + .../csharp-overwrite-enum-naming/README.md | 17 +++++++ .../__snapshots__/index.spec.ts.snap | 41 ++++++++++++++++ .../index.spec.ts | 15 ++++++ .../csharp-overwrite-enum-naming/index.ts | 47 +++++++++++++++++++ .../package-lock.json | 10 ++++ .../csharp-overwrite-enum-naming/package.json | 12 +++++ 8 files changed, 151 insertions(+) create mode 100644 examples/csharp-overwrite-enum-naming/README.md create mode 100644 examples/csharp-overwrite-enum-naming/__snapshots__/index.spec.ts.snap create mode 100644 examples/csharp-overwrite-enum-naming/index.spec.ts create mode 100644 examples/csharp-overwrite-enum-naming/index.ts create mode 100644 examples/csharp-overwrite-enum-naming/package-lock.json create mode 100644 examples/csharp-overwrite-enum-naming/package.json diff --git a/docs/languages/Csharp.md b/docs/languages/Csharp.md index ebb2aaf504..ebf1ca03ad 100644 --- a/docs/languages/Csharp.md +++ b/docs/languages/Csharp.md @@ -10,6 +10,7 @@ There are special use-cases that each language supports; this document pertains - [Generate models with equals and GetHashCode methods](#generate-models-with-equals-and-gethashcode-methods) - [Generate models with auto-implemented properties](#generate-models-with-auto-implemented-properties) - [Change the collection type for arrays](#change-the-collection-type-for-arrays) +- [Generate custom enum value names](#generate-custom-enum-value-names) @@ -39,3 +40,10 @@ Check out this [example for a live demonstration](../../examples/csharp-auto-imp If you consider the Array Class to be insuitable for your situation, then you might look into setting the `collectionType: 'List'` option to your instance of the generator. This will cause all of the collections to be rendered as of type `System.Collections.Generic.IEnumerable`. Check out this [example for a live demonstration](../../examples/csharp-change-collection-type). + + +## Generate custom enum value names + +When using AsyncAPI or JSON Schema, it is not possible to associate enum names with values however with extensions it is. + +Check out this [example for a live demonstration](../../examples/csharp-overwrite-enum-naming/). diff --git a/examples/README.md b/examples/README.md index aafc820741..823078f191 100644 --- a/examples/README.md +++ b/examples/README.md @@ -23,6 +23,7 @@ This directory contains a series of self-contained examples that you can use as - [java-generate-tostring](./java-generate-tostring) - A basic example that shows how to generate models that overwrite the `toString` method - [csharp-generate-equals-and-hashcode](./csharp-generate-equals-and-hashcode) - A basic example on how to generate models that overwrite the `Equal` and `GetHashCode` methods - [csharp-generate-serializer](./csharp-generate-serializer) - A basic example on how to generate models that include function to serialize the data models to JSON +- [csharp-overwrite-enum-naming](./csharp-overwrite-enum-naming) - A basic example on how to generate enum value names. - [generate-javascript-models](./generate-javascript-models) - A basic example to generate JavaScript data models - [javascript-use-esm](./javascript-use-esm) - A basic example that generate the models to use ESM module system. - [javascript-use-cjs](./javascript-use-cjs) - A basic example that generate the models to use CJS module system. diff --git a/examples/csharp-overwrite-enum-naming/README.md b/examples/csharp-overwrite-enum-naming/README.md new file mode 100644 index 0000000000..c3b1a124cd --- /dev/null +++ b/examples/csharp-overwrite-enum-naming/README.md @@ -0,0 +1,17 @@ +# C# Generate custom enum value names + +A basic example of how to custom enum value names. + +## How to run this example + +Run this example using: + +```sh +npm i && npm run start +``` + +If you are on Windows, use the `start:windows` script instead: + +```sh +npm i && npm run start:windows +``` diff --git a/examples/csharp-overwrite-enum-naming/__snapshots__/index.spec.ts.snap b/examples/csharp-overwrite-enum-naming/__snapshots__/index.spec.ts.snap new file mode 100644 index 0000000000..9af7f71137 --- /dev/null +++ b/examples/csharp-overwrite-enum-naming/__snapshots__/index.spec.ts.snap @@ -0,0 +1,41 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Should be able to generate enums with custom value names and should log expected output to console 1`] = ` +Array [ + "public enum OrderStatus +{ + Ordered, + UnderDelivery, + Deliveret, + Cancelled +} + +public static class OrderStatusExtensions +{ + public static dynamic GetValue(this OrderStatus enumValue) + { + switch (enumValue) + { + case OrderStatus.Ordered: return 30; + case OrderStatus.UnderDelivery: return 40; + case OrderStatus.Deliveret: return 50; + case OrderStatus.Cancelled: return 99; + } + return null; + } + + public static OrderStatus? ToOrderStatus(dynamic value) + { + switch (value) + { + case 30: return OrderStatus.Ordered; + case 40: return OrderStatus.UnderDelivery; + case 50: return OrderStatus.Deliveret; + case 99: return OrderStatus.Cancelled; + } + return null; + } +} +", +] +`; diff --git a/examples/csharp-overwrite-enum-naming/index.spec.ts b/examples/csharp-overwrite-enum-naming/index.spec.ts new file mode 100644 index 0000000000..890ec61d94 --- /dev/null +++ b/examples/csharp-overwrite-enum-naming/index.spec.ts @@ -0,0 +1,15 @@ +const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; }); +import {generate} from './index'; + +describe('Should be able to generate enums with custom value names', () => { + afterAll(() => { + jest.restoreAllMocks(); + }); + + test('and should log expected output to console', async () => { + await generate(); + //Generate is called 2x, so even though we expect 1 model, we double it + expect(spy.mock.calls.length).toEqual(2); + expect(spy.mock.calls[1]).toMatchSnapshot(); + }); +}); diff --git a/examples/csharp-overwrite-enum-naming/index.ts b/examples/csharp-overwrite-enum-naming/index.ts new file mode 100644 index 0000000000..923df72572 --- /dev/null +++ b/examples/csharp-overwrite-enum-naming/index.ts @@ -0,0 +1,47 @@ +import { CSharpGenerator } from '../../src'; + +const generator = new CSharpGenerator({ + presets: [ + { + enum: { + item: ({model, item, content}) => { + console.error(model); + const hasCustomName = model.originalInput !== undefined && model.originalInput['x-enumNames'] !== undefined; + if (hasCustomName) { + const customName = model.originalInput['x-enumNames'][item]; + if (customName !== undefined) { + return customName; + } + } + return content; + } + } + } + ] +}); + +const jsonSchemaDraft7 = { + $schema: 'http://json-schema.org/draft-07/schema#', + title: 'OrderStatus', + type: 'number', + enum: [ + 30, + 40, + 50, + 99 + ], + 'x-enumNames': { + 30: 'Ordered', + 40: 'UnderDelivery', + 50: 'Deliveret', + 99: 'Cancelled' + } +}; + +export async function generate() : Promise { + const models = await generator.generate(jsonSchemaDraft7); + for (const model of models) { + console.log(model.result); + } +} +generate(); diff --git a/examples/csharp-overwrite-enum-naming/package-lock.json b/examples/csharp-overwrite-enum-naming/package-lock.json new file mode 100644 index 0000000000..98c488824f --- /dev/null +++ b/examples/csharp-overwrite-enum-naming/package-lock.json @@ -0,0 +1,10 @@ +{ + "name": "csharp-generate-serializer", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "hasInstallScript": true + } + } +} diff --git a/examples/csharp-overwrite-enum-naming/package.json b/examples/csharp-overwrite-enum-naming/package.json new file mode 100644 index 0000000000..0c7c81af5a --- /dev/null +++ b/examples/csharp-overwrite-enum-naming/package.json @@ -0,0 +1,12 @@ +{ + "config" : { + "example_name" : "csharp-overwrite-enum-naming" + }, + "scripts": { + "install": "cd ../.. && npm i", + "start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts", + "start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts", + "test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts", + "test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts" + } +} From f3d3c816c9d223e6927eae74abb9c6b72b054aff Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 7 Jun 2022 14:38:15 +0200 Subject: [PATCH 2/5] removed unused console log --- examples/csharp-overwrite-enum-naming/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/csharp-overwrite-enum-naming/index.ts b/examples/csharp-overwrite-enum-naming/index.ts index 923df72572..da87159adf 100644 --- a/examples/csharp-overwrite-enum-naming/index.ts +++ b/examples/csharp-overwrite-enum-naming/index.ts @@ -5,7 +5,6 @@ const generator = new CSharpGenerator({ { enum: { item: ({model, item, content}) => { - console.error(model); const hasCustomName = model.originalInput !== undefined && model.originalInput['x-enumNames'] !== undefined; if (hasCustomName) { const customName = model.originalInput['x-enumNames'][item]; From 1221c3c8843bb842598ff918036c7404d1474f2a Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 7 Jun 2022 14:39:29 +0200 Subject: [PATCH 3/5] add comment --- examples/csharp-overwrite-enum-naming/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/csharp-overwrite-enum-naming/index.ts b/examples/csharp-overwrite-enum-naming/index.ts index da87159adf..9c1776d6d7 100644 --- a/examples/csharp-overwrite-enum-naming/index.ts +++ b/examples/csharp-overwrite-enum-naming/index.ts @@ -5,8 +5,10 @@ const generator = new CSharpGenerator({ { enum: { item: ({model, item, content}) => { + // Lets see if an enum has any associated names const hasCustomName = model.originalInput !== undefined && model.originalInput['x-enumNames'] !== undefined; if (hasCustomName) { + // Lets see if the specific value has an associated name const customName = model.originalInput['x-enumNames'][item]; if (customName !== undefined) { return customName; From 0883a74836aa7bfe92a669ae73c244101021a962 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Mon, 13 Jun 2022 12:18:44 +0200 Subject: [PATCH 4/5] wip --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dd3bcc9242..e9b05d9ce7 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ This package is still under development and has not reached version 1.0.0 yet. T +- [:loudspeaker: ATTENTION:](#loudspeaker-attention) - [Requirements](#requirements) - [Installation](#installation) - [Features](#features) @@ -34,7 +35,7 @@ This package is still under development and has not reached version 1.0.0 yet. T - [Examples](#examples) - [Development](#development) - [Contributing](#contributing) -- [Contributors ✨](#contributors-%E2%9C%A8) +- [Contributors ✨](#contributors-) @@ -191,3 +192,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! + From 8b7da7066b7f92e4c587ae9aec141a0cd34cd661 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Mon, 13 Jun 2022 12:19:04 +0200 Subject: [PATCH 5/5] Revert "wip" This reverts commit 0883a74836aa7bfe92a669ae73c244101021a962. --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index e9b05d9ce7..dd3bcc9242 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,6 @@ This package is still under development and has not reached version 1.0.0 yet. T -- [:loudspeaker: ATTENTION:](#loudspeaker-attention) - [Requirements](#requirements) - [Installation](#installation) - [Features](#features) @@ -35,7 +34,7 @@ This package is still under development and has not reached version 1.0.0 yet. T - [Examples](#examples) - [Development](#development) - [Contributing](#contributing) -- [Contributors ✨](#contributors-) +- [Contributors ✨](#contributors-%E2%9C%A8) @@ -192,4 +191,3 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! -