enumer
enumer copied to clipboard
Add optional "Validate() error" method
Changes
- Add a new
validateflag - When the
validateflag is true, generate aValidate() errormethod that verifies the enum is set to a proper value - Some small quality of life improvements to the golden test runner
- Run gofmt on some of the files (this just happened automatically because of my editor)
Why
We currently have a IsA<Type>() bool method, but the name is different for every enum type. That basically makes it impossible to integrate it with Go interfaces or generics.
I return an error instead of a bool, because returning an error lets us provide more information on why the value is invalid. And again, it would also compose better with any generic validation code (you could, for example, recursively validate fields in a struct, and combine all the errors using errors.Join).
Note
I didn't see any more well known method signatures than Validate() error, but if there are any, we could switch and use that instead.