-
Notifications
You must be signed in to change notification settings - Fork 0
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
Include an Unspecified Value in Enums #171
Comments
I think it depends on the use case, but I generally feel we should have some "unknown" default value for enums. I agree that our decoder code can treat unknown as some other default. For example, we might decide that unrecognized invite decisions are equivalent to "REJECT". |
Just to be clear, because "unknown" is possibly unspecific, there are two different cases:
It's the latter that's the focus of this issue. |
It seems like this slipped through the cracks, and I think it's valuable to include this in MVP. We would not have any enums that are actually unspecified in the initial release, because it only happens when a new enum field is added and you read a previous version of the proto, however it seems like consistency is good, rather than having some enums whose first value is "unspecified", and other enums don't have that. I'm going to update the issue description to be specific. |
Trying to understand this issue, I made a couple of tests using Test 1 (Loading a new doc with added enum from old build)
Takeaway -> forwards compatibility regarding enum themselves -> you don't get anything Test 2 (Loading a new doc with added enum field from old build)
Takeaway -> forwards compatibility regarding enum fields -> you get "UNRECOGNIZED" Test 3 (Loading a old doc from new build with new enum field on last position)
Takeway -> adding enum fields at the last position -> correctly decoded Test 4 (Loading an old doc from new build with new enum field on arbitrary position)
Takeway -> adding enum fields at an arbitrary last position -> incorrectly decoded (offseted) I'm still struggling to reproduce the case you are talking about @gmaclennan, mainly:
It seems that it doesn't default to decoding it with value 0. It decodes the document with the original enum index, which can be correct if we only add values at the end of an enum and incorrect if we insert a field in an arbitrary position |
tl;dr: I don't think we need to do this (ever). Our existing behavior seems correct. I don't think we should ever address this issue. Let's run through the worrisome cases and see how they aren't a problem. (A lot of this is redundant with Tomás's tests.) Case 1: adding a new enum valueLet's say we add a new enum DeviceType {
mobile = 0;
tablet = 1;
desktop = 2;
+ server = 3;
}
✅ This is all good! Unrecognized enum values are parsed as unrecognized. Case 2: adding a new optional enum fieldLet's say we add a new optional enum field, message DeviceInfo_1 {
// ...
+ enum Color {
+ red = 0;
+ blue = 1;
+ }
+ optional Color color = 7;
}
✅ This is all good! There's nothing special about enums here; unknown fields are ignored. Case 3: adding a new non-optional enum fieldLet's say we add a new non-optional enum field, message DeviceInfo_1 {
// ...
+ enum Status {
+ active = 0;
+ inactive = 1;
+ }
+ Status status = 8;
}
✅ This is all good! This is the expected behavior for default values in protobuf. Unless there's a requirement I'm unaware of, I don't think this is necessary. Does that seem right? |
Thanks for the detailed research into this, this is really helpful. It's into the nitty-gritty, but valuable! I initially thought this was unnecessary, but then re-considered.
TL;DR, I do think it is valuable to adopt this. It is not necessary for enums that already exist in our "v1", however I think it makese sense to do for all enums for the sake of consistency. |
Thoughts on always calling this value |
Ok, so I understand the case for this now. |
ah yes, I see. What you have is fine then. |
For each enum in our protobuf definitions, add
UNSPECIFIED
as the0
enum, and increment the values for other enum values by1
.e.g.
We either need to map this to a default enum during decode, or we need to include this in the JSON schema / decoded type, and then add handling to the frontend.
The text was updated successfully, but these errors were encountered: