ipfs / protons

Protocol Buffers for Node.js and the browser without eval
Other
32 stars 23 forks source link

feat: define default types during decode #62

Closed achingbrain closed 2 years ago

achingbrain commented 2 years ago

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.

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

github-actions[bot] commented 2 years ago

:tada: This PR is included in version protons-runtime-v3.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 2 years ago

:tada: This PR is included in version protons-v5.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 3 months ago

:tada: This issue has been resolved in version 3.0.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: