fix: proto3 optional scalars should default to null in reflection API#1693
Merged
alexander-fenster merged 1 commit intoprotobufjs:masterfrom Jul 8, 2022
Merged
Conversation
protobufjs#1584 made proto3 optional scalars default to null when using static/static-module, but the old behaviour remained when using reflection (eg json-module).
dae
added a commit
to ankitects/anki
that referenced
this pull request
Feb 27, 2022
Protobuf 3.15 introduced support for marking scalar fields like uint32 as optional, and all of our tooling appears to support it now. This allows us to use simple optional/null checks in our Rust/ TypeScript code, without having to resort to an inner message. I had to apply a minor patch to protobufjs to get this working with the json-module output; this has also been submitted upstream: protobufjs/protobuf.js#1693 I've modified CardStatsResponse as an example of the new syntax. One thing to note: while the Rust and TypeScript bindings use optional/ null fields, as that is the norm in those languages, Google's Python bindings are not very Pythonic. Referencing an optional field that is missing will yield the default value, and a separate HasField() call is required, eg: ``` >>> from anki.stats_pb2 import CardStatsResponse as R ... msg = R.FromString(b"") ... print(msg.first_review) ... print(msg.HasField("first_review")) 0 False ```
alexander-fenster
approved these changes
Jul 8, 2022
This was referenced Jul 8, 2022
Merged
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#1584 made proto3 optional
scalars default to null when using static/static-module, but the old
behaviour remained when using reflection (eg json-module). This PR
attempts to make the latter case behave the same way (would love to use
static code generation, but the resulting file sizes are too large).
This passes the existing tests, but I do not know the codebase well, so I
am not sure if this is the best approach or not.