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 option to prefix enum values with their typename #662

Merged
merged 1 commit into from Jul 25, 2022

Conversation

corani
Copy link
Contributor

@corani corani commented Jul 7, 2022

Add a compatibility option to always prefix enum values with their typename.

Previously the following spec:

status:
  type: string
  enum: [ success, failed ]

would be generated as:

type Status string

const (
    Success Status = "success"
    Failed  Status = "failed"
)

or in case of conflicts:

type Status string

const (
    StatusSuccess Status = "success"
    StatusFailed  Status = "failed"
)

This means if you start out without conflicts but introduce conflicts in a later version the generated code unexpectedly changes from under you and you potentially have to fix your code in many places.

When the new option compatibility.always-prefix-enum-values is set to true, the latter code is always generated.

Closes: #625

@corani corani marked this pull request as ready for review July 7, 2022 04:01
@corani corani changed the title Add option to prefix enum values with their typename (#657) Add option to prefix enum values with their typename (#625) Jul 7, 2022
@corani corani changed the title Add option to prefix enum values with their typename (#625) Add option to prefix enum values with their typename Jul 7, 2022
// This is set to true when this enum conflicts with another in terms of
// TypeNames or when explicitly requested via the
// `compatibility.always-prefix-enum-values` option.
PrefixTypename bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably makes sense to be:

Suggested change
PrefixTypename bool
PrefixTypeName bool

If it's using TypeName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done!

@deepmap-marcinr deepmap-marcinr merged commit d9622d1 into deepmap:master Jul 25, 2022
@corani corani deleted the daniel/prefix-enum branch July 26, 2022 01:14
adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
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

Successfully merging this pull request may close these issues.

Enum value collision
3 participants