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

Override snake case for JSON struct tags #129

Closed will-rowe closed 3 months ago

will-rowe commented 6 months ago

Hi.

Thanks for a great package! I was wondering if it is possible to specify a different case for the json struct tags in the generated models? I.e. have camelCase instead?

will-rowe commented 6 months ago

Hi - I ended up forking and adding a helper to change from the default snake case, eg:

func ConvertCase(sentence, caseFormat string) string {
    str := stringy.New(sentence)
    switch caseFormat {
    case "camel":
        camel := str.CamelCase()
        return stringy.New(camel).LcFirst()
    case "kebab":
        val := str.KebabCase()
        return val.ToLower()
    default:
        val := str.SnakeCase()
        return val.ToLower()
    }
}

I updated the schema template and added an extension so you can override the default:

ExtJSONTagCase string `json:"x-json-tag-case"`

Let me know if this is something you want upstream and I can make an MR.

lerenn commented 6 months ago

Hello @will-rowe ! Thanks for the kind words ☺️

Regarding your proposal, it's a great idea !! If you're okay to do a MR to merge your work, I would be really happy to integrate it.

lerenn commented 5 months ago

If you're still good with the MR and if you have time to do so, I would be nice to have your helper ! :)

hound672 commented 3 months ago

I wonder why should use extra tag for it? We can use tag from specification as default and if you need to convert then specify with tag?

lo00l commented 3 months ago

Agree with @hound672. To be honest, I don't understand why it's even necessary to convert property key names to snake_case (or to something else).

Now I have specification with the following schema:

IndividualExposureLimitsSet:
  type: object
  additionalProperties: false
  properties:
    Separate Uuid Thresholds:
      $ref: '#/components/schemas/SeparateUUIDExposureLimitValues'

This specification is provided by other team so I cannot modify it.

When I generate types, I get

type IndividualExposureLimitsSetSchema struct {
    SeparateUuidThresholds *SeparateUUIDExposureLimitValuesSchema `json:"separate _uuid _thresholds"`
}

Well, seems it will not work at all for me :)

What's about adding new generation option convert-keys with possible values, for example, snake, camel, kebab, none (default is snake to make it backward compatible)?

So we could run generation like this: asyncapi-codegen -g types -i asyncapi.yaml -o ./models.gen.go -p test --convert-keys none.

If you think it's ok then I could implement it.

P.S.: I would be happy if we could just remove snakeCase call from schema_definition.tmpl but it would break backward compatibility.

lerenn commented 3 months ago

I really like the generation option ! To be honest, I don't remember why I choose snake case. Maybe because that's why I (wrongly) assume JSON was using.

So I'm all for doing the --convert-keys to avoid breaking existing configuration, but I would be for the none option as default.

It would break some configuration without changing the command line, but:

Thanks for pointing that out ! If you're okay to do a PR, please do !

lerenn commented 3 months ago

Fixed by https://github.com/lerenn/asyncapi-codegen/pull/199