-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Don't set defaults for optional fields #179
Comments
Hope this helps. It now presets the default values for required fields only, leaving optional fields with a value of I believe that checking if there is an optional default should be done via reflection, though. Like looking for the default option and testing against |
Thanks for the quick fix, is working for my case at the moment. |
Can you please explain how to check if a field has been set. Currently, I'm observing that if a field has a default value and is not set, then on decoding, the object contains the default value. How can we distinguish this from the case where the field was indeed set with the same value as the default? Thanks! |
See: #200 |
Documentation on https://developers.google.com/protocol-buffers/docs/proto#optional say:
|
I somewhat feel like setting optional fields without a default value to something else than What do you prefer and why? |
If I set the default value, I expect to always get a string/number/whatever not null. The same behavior has an official java implementation. Subjectively, I have a Messages with several hundred Fields, adding "null" causes unnecessary overhead. It causes complications if you want to convert to json output for example. Usually, the application only works with small subset of data. |
Primitive accessors should always return a value even for optional fields. The ProtoBuf.js implemention of default values violates this compared to the C++ and Go protobuf libs. It seems like the problem is trying to make direct field accesses give the same result as calling the accessor function; in C++/Go they do not. The actual fields should always store the explicit value if present during deserialize or stored by an setter. The spec does not say that the default value should be stored -- just that it should be returned by the accessor if there was a null value stored. In addition, ProtoBuf.js violates the last paragraph of the spec that AoJ quoted in above. For primitive fields without a default value, the accessor does not return the type-specific default. To be true to ProtoBuf author's intentions, getters/accessors should be generated as:
|
Out of curiosity, why was this closed? As far as I can tell, this is still an issue. I'm not sure I understand the design decision to not inject default values if not present (and subsequently ignore default values when encoding). Or at least provide the default value in the message prototype. |
Any updates? This is very annoying when used with TypeScript, because the type definition says it can be undefined (being an optional field), but it is never undefined as the default value will be (for strings) empty string. Currently multiple bugs occurred because I was checking |
Well, this is from 2014. Things have changed a lot since then. If you have a specific issue with the current version, please open a new issue. |
Ok, thanks! I think it is strange that this issue was closed even though the problem was never fixed.
On Tue, Dec 26, 2017 at 1:43, Daniel Wirtz<notifications@github.com> wrote:
Well, this is from 2014. Things have changed a lot since then. If you have a specific issue with the current version, please open a new issue.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Protobuf.js shouldn't set defaults for optional fields while encoding a message, rather set defaults during decoding if not present -- and provide has('fieldname') to help users to distingush if it's an explicit set or set by default. I'm not able to use this module to talk to Google's Datastore API.
The text was updated successfully, but these errors were encountered: