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.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON serialization emitDefaults option #592
base: master
Are you sure you want to change the base?
JSON serialization emitDefaults option #592
Changes from all commits
772ee3e
5c50a95
db5637f
7121dc7
d714dbc
c253b82
d3bd5ae
86af2f2
b622504
3cd3d15
ed9527e
89ba6cf
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: shouldn't default value for maps be
null
? I think maps are considered the same as messages in the spec, at least when talking about default values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question. I know that the
emitDefaults
option within the Go library serializes maps that are not set to an empty map value:{}
. The spec is not very clear on this point.Let me know if we are thinking this should be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be related to #309. I say not a blocker for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jmartin127 , this is not much easier to follow now.
I still have one suggestion though. In the outer
if
, in the first few cases you check the field type, which makes sense. But after thebool
case you start checking the value type rather than field type. Ideally we should be able to check just the field type, and the value type should be as expected for the field type (i.e. value isint
if field type isint32
). I have a suggestion in my top-level comment on how to make this easier.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this part was a bit wonky. The reason I switched from value types to field types was because there are just so many numeric types to handle, so it became more readable to check the value types. I think if we follow you top-level comment suggestion, then this is no longer an issue. I'll wait for your and @sigurdm to reply on the best path forward there (if we go with the switch, from your other PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discovered an interesting case regarding JSON serialization and proto2. proto2 spec doesn't specify JSON encoding, only proto3 does. However
toProto3Json
can be called on both proto2 and proto3 messages. So if I have a proto2 message likeI can convert this to proto3 JSON using
toProto3Json
, but in this PR we assume proto3 defaults -- i.e. for numeric fields the default to be 0. This doesn't hold when the message syntax is proto2. As a result output of this code is somewhat surprising:It's possible to fix this issue and simplify the code at the same time. Here's my suggestion:
I've also added some comments.
You'll notice that two tests will break with this change, but if you look at the test proto you'll see that we actually set the default value as
42
in those tests, so actually we fix the test. We should update theexpect
s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like you don't try to set the map field in the tests. Is there a reason for this? To make sure we handle maps correctly we should set the map field too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The map value is getting set, just in a different way... For consistency, I followed the existing example of how the int32s are set using the
addAll
function.See here for an example of setting/testing the map field.
https://github.com/google/protobuf.dart/pull/592/files#diff-f9dfd1b28cb8ed5241edf3d57212698aeb5c6b3faa64d982213236cba5bd89f8R44
With this in mind, it is currently being tested, but if it would be better to set the map differently, just let me know.