Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What parameters HasField function of protobuf expects?

Consider following structure

message Fly {
  uint32 dtime = 1;
}

But HasField function doesnt work:

>>> d.ListFields()[0][0].name
'dtime'
>>> d.ListFields()[0][0].full_name
'Fly.dtime'
>>> 
>>> d.HasField('dtime')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
    raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "dtime"
>>> d.HasField('Fly.dtime')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
    raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "Fly.dtime"

What parameters does HasField expects?

like image 767
rkuska Avatar asked Mar 12 '23 13:03

rkuska


1 Answers

I think you've misunderstood what HasField() does. It does not check whether a protobuf type defines a specific field by a name. What it does is to check if, given the name of a message field, that that field is set for the current instance.

As the documentation notes, calling HasField for a non-message field in proto3 will raise an error.

like image 161
Daniel Roseman Avatar answered Mar 16 '23 00:03

Daniel Roseman