Closed
Description
Proto3 language guide states:
When a message is parsed, if the encoded message does not contain a particular singular element, the corresponding field in the parsed object is set to the default value for that field.
Looking at protons
's code and behaviour, it is clear that:
- Fields are always encoded in the message, even if their value is the default one (expected: do not encode the field).
- When decoding a message and a field is not present, it is set as
undefined
(expected: set to the default value of the field).
Default values are type-specific:
- For strings, the default value is the empty string.
- For bytes, the default value is empty bytes.
- For bools, the default value is false.
- For numeric types, the default value is zero.
- For enums, the default value is the first defined enum value, which must be 0.
- For message fields, the field is not set. Its exact value is language-dependent. See the generated code guide for details.
I see that the tests are done against pbjs, this package states:
Unlike other JavaScript implementations, this library doesn't write out default values.
Is the choice to not follow the proto3 language guide regarding default value on purpose in protons
?
Keen to open a PR, I can see two ways forward:
- Follow proto3 language guide: do not encode a field when it's set to a default value, set a default value when a field is absent as decoding
- Enable some way to parameterize this behavior (guidance would be welcome here).