You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Proto3 language guide [states](https://developers.google.com/protocol-buffers/docs/proto3#default):
> 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.
When decoding objects, create an object with all non-optional fields set to the default values, then overwrite them during decoding.
Given that we create arrays for repeated fields there's no need to check for their existence before returning the deserialized object.
The only weird part in all this is message fields. The spec says that any non-optional field is required, but it also says that the default value for a sub-message is to be unset and that the actual value there is language-dependent and links to some language guides which don't include JavaScript - I've used `undefined` here. The upshot of this is that you can have a non-optional field with a default value of `undefined` which essentially makes it optional. Nice. At the moment we throw if the sub-message is not present in the protobuf if the field is required though in a future change we may wish to ignore the spec and initialise the sub-message to an instance of it's type with default values set 🤷.
Refs #43
0 commit comments