asyncapi / modelina

A library for generating typed models based on inputs such as AsyncAPI, OpenAPI, and JSON Schema documents with high customization
https://modelina.org
Apache License 2.0
296 stars 173 forks source link

Add enum for asyncapi message $id`s #181

Open czlowiek488 opened 3 years ago

czlowiek488 commented 3 years ago

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" 
}
jonaslagoni commented 3 years ago

Related to https://github.com/asyncapi/modelina/issues/188

czlowiek488 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping: 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 :heart:

github-actions[bot] commented 2 years ago

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

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 :heart:

jonaslagoni commented 2 years ago

Still relevant

github-actions[bot] commented 2 years ago

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

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 :heart:

jonaslagoni commented 2 years ago

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://github.com/asyncapi/modelina/blob/next/examples/meta-model/index.ts