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

Add enum for asyncapi message $id`s #181

Open
czlowiek488 opened this issue May 6, 2021 · 11 comments
Open

Add enum for asyncapi message $id`s #181

czlowiek488 opened this issue May 6, 2021 · 11 comments

Comments

@czlowiek488
Copy link

Reason/Context

I created generator which generate types and Joi validators.
I generate validator with code like below.

validators.enjoiOne("MessageJobCreated")

As you can see I must pass string what is a little bit dirty solution.
This string is $id of message from asyncapi yaml file but there is no way to access it from generated types.

Description

I would like to have access to enum with all messages $id`s as key and value.
I guess it may look similar to this one.

export enum AsyncApiMessages {
   MessageJobCreated = "MessageJobCreated" 
}
@czlowiek488 czlowiek488 added the enhancement New feature or request label May 6, 2021
@jonaslagoni
Copy link
Member

Related to #188

@czlowiek488
Copy link
Author

czlowiek488 commented Jun 28, 2021

@jonaslagoni Im not sure about it. I know that you introduced changes which allow to create enums and use it for values.
But I think here is a little different thing.
Are you sure that I can use enum to define $id of a message?

@jonaslagoni
Copy link
Member

jonaslagoni commented Jun 29, 2021

@czlowiek488 no you right, the changes does not directly allow you to do this natively. However you can manually force it to generate it with the following somewhat pseudocode (not tested):

import {CommonInputModel, CommonModel, TypeScriptGenerator} from '@asyncapi/modelina';
import {parse} from '@asyncapi/parser';

const parsedAsyncapiDocument = await parse(<Your asyncapi document>);
let messageEnumModel = new CommonModel();
messageEnumModel.$id = 'AsyncApiMessages';
for(const [,message] of parsedAsyncapiDocument.allMessages()){
  messageEnumModel.addEnum(message.payload().$id);
}
let inputModel = new CommonInputModel();
inputModel.models = {'AsyncApiMessages': messageEnumModel}
const generator = new TypeScriptGenerator();
const outputModels = await generator.generate(inputModel);

@czlowiek488
Copy link
Author

Ok its nice 👌.
However it would be good feature to have an option which will do this behind the scenes. Current solution force you to learn how this package Works.

@jonaslagoni
Copy link
Member

jonaslagoni commented Jun 29, 2021

Yea I agree with you, there are also different kinds of enums that could be generated using the AsyncAPI file. I have no idea how to best enable such a feature though (as it is only related to AsyncAPI inputs).

@czlowiek488 any suggestion on how you could see the API change to allow for such a feature? 🤔

@czlowiek488
Copy link
Author

@jonaslagoni
Imho the best way to implement such a behaviour is pass those thing as extensions.

I would see it this way.

import { MessageIdsEnum } from "@asyncapi/modelina-plugins";

const generator = new TypeScriptGenerator({ plugins: [MessageIdsEnum] });

It support custom plugins so anyone would be able to modify generator in its own way.
Of course it may not be easiest to implement however it would make modelin more flexible.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Dec 29, 2021
@jonaslagoni
Copy link
Member

Still relevant

@jonaslagoni jonaslagoni removed the stale label Jan 5, 2022
@github-actions
Copy link
Contributor

github-actions bot commented May 6, 2022

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@jonaslagoni
Copy link
Member

As there are no specifics or suggestions on how to add something like plugins for input processors I am gonna close this one down. We have a specific example of creating custom MetaModels from any input you choose. So that can be done as a solution for this one https:/asyncapi/modelina/blob/next/examples/meta-model/index.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants