-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: define default types during decode (#62)
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
- Loading branch information
1 parent
a7d567d
commit 6453809
Showing
12 changed files
with
213 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ syntax = "proto3"; | |
|
||
message Basic { | ||
optional string foo = 1; | ||
required int32 num = 2; | ||
int32 num = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.