question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the correct way in proto3 to use HasField?

See original GitHub issue

Originally 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:closed
  • Created 8 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

24reactions
habermancommented, Feb 11, 2016

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.

7reactions
marshallma21commented, May 29, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found