-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
What is the correct way in proto3 to use HasField? #1402
Comments
I think the question is: what are you trying to do? Why is it relevant to you whether a field is set or not and what do you intend to do with this information? In proto3, field presence for scalar fields simply doesn't exist. Your mental model for proto3 should be that it's a C++ or Go struct. For integers and strings, there is no such thing as being set or not, it always has a value. For submessages, it's a pointer to the submessage instance which can be Please don't access |
Thanks for the clarification (that mostly was what I assumed to be true, so that's comforting). The original reason for using Changing |
Yes, testing against 0 and the empty string is definitely one option. By the way, you can probably just use Python's truthiness testing for this (ie. It still would help to know: what are you actually going to do with this information? If the |
@haberman for example, when parsing a I am trying to excise uses of |
Ah! See this is why I am glad I asked. The proper way of solving this problem is with a |
Yup. The datastore |
|
Also, changing an existing protobuf to use |
Gotcha, |
Address this in #1450. |
@haberman we're using protobuf to transition from some legacy code where an enum of values is mapped to integers including a valid enum with value of 0. In my protobuf the default value is 0 even if the field is "unset". Is there a way that I can check if this field is unset without using |
@TheJKFever Are you using proto2 or proto3 (ie. what does the "syntax=" declaration in your .proto file say?) If you're using proto2, then you can use the If you're using proto3, then there is no way to tell, sorry. This is by design; see extensive discussion on this here: protocolbuffers/protobuf#1606 |
Thanks @haberman. We were using proto3, but decided to switch to proto2 for this reason. It seems like if you wanted to use proto3 and have the ability to see if someone explicitly set an int enum, you would need to make sure that your enum did not start at 0 or be okay with an implicit default of 0. |
Suffering the same pain with @TheJKFever . The funny thing is that, if you convert the proto to json string, you can actually tell whether the field exists or not. So the proto3 actually knows the existance of a Field, but it refuse to provide a way to tell that. |
maybe ListFields is better solution:
|
Originally came up in #1329.
HasField
withproto3
fails on simple / singular / non-message fields.@nathanielmanistaatgoogle Mentioned that @haberman might be able to shine some light. Josh, what should we be doing here? The current approach isn't great:
Should we bother with checking "has field", e.g.
or should we instead just check Truth-iness
The text was updated successfully, but these errors were encountered: