E52A Update enum generation to generate a map to validate enums. by AndreasAbdi · Pull Request #2181 · oapi-codegen/oapi-codegen · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@AndreasAbdi
Copy link

Customers often need to validate whether a value is a legit enum value.
We implement that validation by creating helper functions.

With the helper function, customers no longer have to manually write their own version of the helper.

i.e.

const (
	ClientTypeWithNamesExtensionActive  ClientTypeWithNamesExtension = "ACT"
	ClientTypeWithNamesExtensionExpired ClientTypeWithNamesExtension = "EXP"
)

var ClientTypeWithNamesExtensionValues = map[ClientTypeWithNamesExtension]struct{}{
	ClientTypeWithNamesExtensionActive:  {},
	ClientTypeWithNamesExtensionExpired: {},
}

func (s ClientTypeWithNamesExtension) IsValid() bool {
	_, ok := ClientTypeWithNamesExtensionValues[s]
	return ok
}

and then the customer uses it like:

func legit() {
	k := ClientTypeWithNamesExtension("")
	isValid := k.IsValid()
	fmt.Print(isValid)
}

Customers often need to validate whether a value is a legit enum value.
We implement that validation by creating helper functions.

With the helper function, customers no longer have to manually
write their own version of the helper.
@AndreasAbdi AndreasAbdi requested a review from a team as a code owner January 9, 2026 07:43
@AndreasAbdi AndreasAbdi changed the title Update enum generation to generate a map. Update enum generation to generate a map to validate enums. Jan 9, 2026
Copy link
Member
@jamietanna jamietanna left a comment

Choose a reason for hiding this comment

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

I'd say we probably shouldn't export the map as then it's mutable outside of the package?

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.

2 participants

0