Skip to content

Handling of default values do not conform with proto3 language guide #43

Closed
@D4nte

Description

@D4nte

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:

  1. 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
  2. Enable some way to parameterize this behavior (guidance would be welcome here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    need/analysisNeeds further analysis before proceedingneed/triageNeeds initial labeling and prioritizationreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions