-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
jsonpb: change Marshal/Unmarshal to return error if any required field is not set #472
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f269205
jsonpb: change Marshal/Unmarshal to return error if any required field
cybrcodr 49ce6df
jsonpb: improve Unmarshal check for required fields.
cybrcodr c24ad06
jsonpb: improve Unmarshal check for required fields.
cybrcodr f899cde
jsonpb: fix Marshal/Unmarshal logic in checking required fields.
cybrcodr 5f2e3b1
jsonpb: revert accidental changes in jsonpb_test.go in PR#472.
cybrcodr 1f41e39
jsonpb: add safety check on OneOf wrapper structs to contain a field.
cybrcodr a7bdcd1
jsonpb: minor fix in check for OneOf struct fields.
cybrcodr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
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.
&& v.NumFields() > 0
maybe, just to be sure?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 think you meant to check for NumField() == 0 so that Field(0) below won't panic. That would be a bug in protoc-gen-go, but I went ahead and added the check.
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.
Right, but any value of NumFields() less than 0 would be an error state. And checking for greater than zero, and “is not zero” is either: identical with unsigned ints, or just extra CYA for signed ints.
Sure NumField shouldn’t ever return a negative number, but iI find it to generally be a good idea to remember that technically it might… even if only erroneously. So, I typically prefer to avoid weak/narrow tests, when a stronger/broader test is available for the same cost. (I also usually test if
len(array) < 1
rather than justlen(array) == 0
for the same reasons.)It could be viewed as an example of “overengineering” but I prefer to cover all load-scenarios rather than only the specific load-scenarios that one thinks might cause a problem.
I mean, if you can order 100 nuts for $100 in either 3 lbs load-hanlding, or 1 lb load-handling, even if you expect that you will only need at most 1 lb of load-handling, it doesn’t cost anything extra to get the higher load handling ones.
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.
Done.