-
Notifications
You must be signed in to change notification settings - Fork 819
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
Option to always encode proto3 fields, even if they have their default value #878
Comments
I think the best solution would be to use optional fields which were added to proto3 a few years ago. This generates a It is also possible to set nanopb-specific Encoding zero values should be acceptable for the format. But if there is no separate |
I always want every field to be encoded, exactly because during decoding, I assume default values for any field that is not present (through I do not want to have to check a |
Hmm, ok. Seems like a bit niche situation, but that behavior would make sense for your use case. Have you considered using |
https://github.com/nanopb/nanopb/blob/master/pb_encode.c#L270-L278 This piece of code may be relevant, the situation in #558 was somewhat similar except using proto2 syntax and specifying |
Maybe it could just be added as a flag to |
Seems quite difficult to pass that flag down to where the decision is made. |
Unfortunate, would a PR be accepted? Otherwise we'll just have to keep the hack in our private fork, not a big deal either. |
I'm not sure that there is a way to implement this that I would be comfortable merging. It seems it would need quite large changes to the rest of the code for a fairly niche use-case. But if you already have a working hack, it could be interesting to see it. You can e.g. attach a patch file here. |
Well right now I'm just commenting out the call to |
Hmm yeah. I guess that would have minimum impact even though it is not as useful as a per-field option. This is probably worth reconsidering. |
Would it be possible to add a field/message/file option that forces fields to always be encoded, even if they have their default value and would thus normally be skipped in proto3?
My use case is the following: I have a device configuration on an embedded device that is serialized to EEPROM using nanopb. When upgrading to newer firmware with new config options, these new options should be set to specific values (as specified in the firmware, not in protobuf), while all old options should be used as-is. This mostly works fine by first filling the config struct with the "default" values, then deserializing into it using
pb_decode_ex(..., PB_DECODE_NOINIT);
.However, since fields with a zero-ish value are not serialized at all, they always look like "new fields" on the next deserialization and are set to their "default" value.
From a spec perspective, is it legal to always encode fields, even if they have their default value? If so, would a PR adding such an option to nanopb be accepted?
The text was updated successfully, but these errors were encountered: