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
Optional fields don't use default values when unset #82
Comments
I just found issue #21 as well. I'm going to see exactly what |
Here is a test I added that demonstrates this: https://github.com/basho/riak_pb/blob/develop/test/encoding_test.erl#L87-L102 Running that test with I will see if I can add the |
Default values is a bit of a weak spot with if you compile with records (ie not with the But if one compiles with the When generating code for decoding, But there's another strategy for how the decoding code looks like, that passes around each field as a variable instead of the record, that handling initiates any Which of these two strategies to use depends on some judgement done in the analysis phase. Regardless of strategy, when decoding is generated for maps, default values are never included, making it inconsistent in one more aspect. One goal, generally for I think that it is wrong to insert default values when the record is defined. (definitely wrong in case of optional fields anyway) And I think that the Another goal should be that there is some room for sensible extensibility in the direction of what is requested in the #21. I think the #71 is unrelated since this is still on At the moment, there are some other things, it'll take a week at the earliest before I could take a closer look. |
After spending time reading the proto2 and proto3 specs, I can see why both For Other changes to be in-line with the proto2 spec aren't as important to me at this point.
But, that is what the spec requires. If no value is sent on the wire, either the field's |
Ah, yes, it was a long time for me too, but perhaps it boils down to designing something that:
The Is it really possible to do all of 1, 2 and 3? 1 and 3 can be achieved for instance by adding a bitmap to the record, the bitmap describing which optional fields are actually present. But it would require using api functions for setting fields so that the bitmap gets updated. One could not merely set the record field using the normal record field setting syntax. so it would fail on 2 (natural mapping to Erlang). I get the impression that If we could come up with something that feels natural to Erlang, I'd be willing to give it a go, but until now, I haven't been able to come up with anything good. Ideas welcome of course! For Being somewhat wiser from earlier experiences, I think the best for option designing would be to (a) control this by some new option for instance |
I think I need to make myself more clear: If we can find some way that feels natural to Erlang, to do both 1 and 3, then I'd welcome it, and be willing to spend time. Until then, I'd rather prioritize 3 (ie being able to see omitted fields) but I could accept an option, or perhaps two options, for shifting priority to defaults or type defaults, I can see this being useful. However, if message type fields can have defaults, then substantial re-writes would be necessary, such that I think options for shifting priority would be much more work that it is worth. |
Thanks for the detailed thoughts. The spec isn't clear, but I'm pretty sure that for message fields the default value is language-specific, so for languages that support |
Yes, I agree to this after having read some protobuf source code and also tried it experimentally:
So is the below a correct recap of our discussion so far?
|
@tomas-abrahamsson - that is my understanding as well. In Your other bullet points are spot on too. I spent some time trying to get a handle on everything that happens in Have a great weekend. |
Thanks! The same (retroactively :) ) I just pushed a branch (with a very long name) if you want to try it out: |
Thank you @tomas-abrahamsson I'll be trying it out today. |
Just pushed 3.26.0, containing the options |
(I'll remove the for-test-and-review temporary branches |
Refactored to reduce code duplication and increase clarity.
Message definition from here:
Input data:
Expected record with values after decoding:
Actual decoded data:
It seems like, according to the spec an
optional
field that is not supplied adefault
value or a value during encoding should be using type-specific defaults (0
for numeric fields, for instance). Instead, it seems likeundefined
is being used.This may be related to #71
Thoughts? I'm going to start digging into the
gpb
code now to see what I can find. Thanks!The text was updated successfully, but these errors were encountered: