lerenn / asyncapi-codegen

An AsyncAPI Golang Code generator that generates all Go code from the broker to the application/user. Just plug your application to your favorite message broker!
Apache License 2.0
82 stars 22 forks source link

expose channels in generated code as constants #135

Closed antonyc closed 4 months ago

antonyc commented 4 months ago

It would be nice to have to be able to perform compile-time/initialization time checks in the application when implementing brokers that expect some known channels for configuration and would error out on an unknown channel, becase currently broker accepts channel as a simple stream

type BrokerController interface {
    // Publish a message to the broker
    Publish(ctx context.Context, channel string, mw BrokerMessage) error
lerenn commented 4 months ago

Hello @antonyc ! Thanks for your interest in this project :smiley:

So if I understood correctly, if we have, for example, this AsyncAPI document:

channels:
  ping:
    publish:
      operationId: ping
      message:
        $ref : '#/components/messages/Ping'

  pong:
    subscribe:
      operationId: pong
      message:
        $ref: '#/components/messages/Pong'

You would like to have this kind of code generated:

const (
    // PingChannelPath is the channel path for 'ping' operation
    PingChannelPath = "ping"
    // PongChannelPath is the channel path for 'pong' operation
    PongChannelPath = "pong"
)

// ChannelsPaths is an array of all channels paths
var ChannelsPaths = []string{
    PingChannelPath,
    PongChannelPath,
}

That would let you check that necessary channels are generated. Would it be enough for you or did you expected something different/had something different in mind ? :)

antonyc commented 4 months ago

exactly, thanks for super quick answer!

I am just trying to add it our code base, so if the PR gets merged, I probably will have some time to make a PR, but very hard to promise for me.

lerenn commented 4 months ago

No problem ! Should be quick to write, I'll do that today or, more likely, tomorrow :)

lerenn commented 4 months ago

Completed on tag v0.31.0 :)

Feel free to open another issue if you need anything else !