Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a ProtoBuf field which is an empty message in Python?

The following are the contents of the Google Protocol Buffer (.proto) file

message First
{
    required uint32 field1 = 1;

    optional MessageType1 request = 2;
}

message MessageType1
{
}

I want to set the MessageType1 field request. But I get this as an error:

AttributeError: Assignment not allowed to composite field "request" in protocol message object.

How to set the value of this empty message in Python?

like image 393
web_ninja Avatar asked Apr 15 '15 06:04

web_ninja


1 Answers

Got this in the source code of Message class in Proto Buffer.

  def SetInParent(self):
    """Mark this as present in the parent.

    This normally happens automatically when you assign a field of a
    sub-message, but sometimes you want to make the sub-message
    present while keeping it empty.  If you find yourself using this,
    you may want to reconsider your design."""

So the way to set such an empty message is to call this function:

first.request.SetInParent()
like image 76
web_ninja Avatar answered Sep 25 '22 22:09

web_ninja