ipfs / protons

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

fix!: singular fields should be optional to write #83

Closed achingbrain closed 1 year ago

achingbrain commented 1 year ago

As per proto3's language guide:

Message fields can be one of the following:

  • singular: a well-formed message can have zero or one of this field (but not more than one). And this is the default field rule for proto3 syntax.

This means that all proto3 fields are effectively optional when writing, so it's relatively easy to implement by accepting a Partial version of the message interface for encoding.

When reading messages singular fields are initialized to their default values when reading and optional values are not so the plain non-Partial interface can be returned.

This also has the nice side effect of not requring the user to pass empty lists/maps for repeated and map fields which never really felt right. These values are initted to their empty forms when reading messages from the wire.

Fixes #42

BREAKING CHANGE: codecs now accept a Partial object so ts definitions will need to be regenerated from .proto files to ensure the now-optional values for singular fields are handled correctly

achingbrain commented 1 year ago

cc @fryorcraken

fryorcraken commented 1 year ago

Nice!

Do you have a test that confirms that if a singular field has a default value, then it is not encoded over the wire? As I understand protobufjs always encodes the fields over the wire the test may not be easy to do.

achingbrain commented 1 year ago

Yes, I've just added one here

github-actions[bot] commented 1 year ago

:tada: This PR is included in version protons-v7.0.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: