-
Notifications
You must be signed in to change notification settings - Fork 111
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
Make marshaling unknown values error #50
Comments
I see the issue, but it would be a huge change in behavior right now for Enumer that would be backward incompatible. I would say that the root issue is that Go allows you to store However, there are cool solutions to your problem. For me, the best one is to define an undefined action with value 0. type Action int
const (
ActionUndefined Action = iota
ActionFoo
ActionBar
) That's it. Now the zero value is the action "undefined" and you can check that in your code. Take into account that a variable of type var actionValid Action = ActionFoo
var actionInvalid Action = 1234
actionValid.IsAAction() // returns true
actionInvalid.IsAAction() // returns false I hope this helps. |
Stringer has no error return, so it shouldn't be refusing. It'd be trivial to change MarshalJSON to handle the undefined values, and only delegate to Stringer for the known good values. You're trying to make me make the invalid value become a valid marshalable value; that is the opposite of my reason for introducing it. |
In an ideal world, an enum "type" would only ever allow valid (passes |
I'd personally like to see @tv42 's request implemented. It is backwards incompatible, but who is actually intentionally depending on |
In any case, I do really appreciate the project as-is - it's already leaps and bounds better than raw ints!! |
Hi. I'm using
to make the default zero value be an undefined value. I just noticed that some JSON marshaled as
I would much prefer that marshaling enums that are not a valid value produce an error. It would expose my programming errors sooner. What do you think?
The text was updated successfully, but these errors were encountered: