What is the correct way in proto3 to use HasField?
See original GitHub issueOriginally came up in #1329. HasField with proto3 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:
# NOTE: As of proto3, HasField() only works for message fields, not for
# singular (non-message) fields. First try to use HasField and
# if it fails (with a ValueError) we manually consult the fields.
try:
return message_pb.HasField(property_name)
except ValueError:
all_fields = set([field.name for field in message_pb._fields])
return property_name in all_fields
Should we bother with checking “has field”, e.g.
if _has_field(key_pb.partition_id, 'dataset_id'):
...
or should we instead just check Truth-iness
if key_pb.partition_id.dataset_id:
...
# OR MORE STRICT
if key_pb.partition_id.dataset_id != '':
...
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (5 by maintainers)
Top Results From Across the Web
Check if a field has been set in protocol buffer 3 - Stack Overflow
First try to use HasField and # if it fails (with a ValueError) we ... but proto3 already provides built-in wrappers with hasField...
Read more >Language Guide (proto3) | Protocol Buffers - Google Developers
The first line of the file specifies that you're using proto3 syntax: if you don't do this the protocol buffer compiler will assume...
Read more >com.google.protobuf.Message.hasField java code examples
How to use. hasField. method. in. com.google.protobuf.Message. Best Java code snippets using com.google.protobuf.Message.hasField (Showing top 20 results ...
Read more >protobuf/python/google/protobuf/internal/message_test.py
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ... and doesn't preserve unknown fields, so for proto3 we use...
Read more >Default Values vs Missing Values - Google Groups
almost every protobuf use-case I've had, the presence accessors were imperative to proper operation. ... hasField() I think?.. Yoav H's profile photo ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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
NULL, that’s why you can test presence for it.Please don’t access
msg._fields. It’s a private variable with no defined semantics.Suffering the same pain with @TheJKFever . Can’t tell whether it’s a ENUM value with 0 or not receiving at all.
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.