protobuf messages as repeated field?

12,553 views
Skip to first unread message

Lotte R. Leben

unread,
Jun 23, 2013, 9:49:44 AM6/23/13
to prot...@googlegroups.com
Hi,
this might be quite the newbie question, but I haven't been able to find any hints through googling, so here it goes:
What I'm trying to to is send orotobuf messages between an Android App a Python Script containing (among other things) a list of key-value pairs (if ints). 
The solution that I came up with was just defining my own “tuple” as another protobuf message and then including a repated field of said type, like so:

message CheckMessage {
message BalanceUpdateMessage{
required int32 transactionID = 1;
required sint32 amount = 2;
}
required int32 amount = 3;
required sint32 newBalance = 5;
repeated BalanceUpdateMessage balanceUpdates = 4;
}  

and then try to fill the balaceUpdates in Python (the checkMessages are embedded in another packetMessage):

        balanceUpdateMessage = protoPackets_pb2.CheckMessage.BalanceUpdateMessage()
        balanceUpdateMessage.transactionID = 1111
        balanceUpdateMessage.amount = 2222

        packetMessage.checkMessage.balanceUpdates.append(balanceUpdateMessage)

But when I run this, I get the error:

AttributeError: 'RepeatedCompositeFieldContainer' object has no attribute 'append'

... I kind of expected my approach to fail, but not with an error message like this :D

So my question is: Is it even possible to send multiple key,value - pairs with protobuf and if yes, how? Or if I'm on the right track: what is my mistake? 

( I know that one usually would set the contents of a “nested” message with packetMessage.checkMessage.balanceUpdateMessage.transactionID = 333 (or so), but since I'm dealing with a repeated field that neither works nor would it make sense (to me, at least). )

Ilia Mirkin

unread,
Jun 24, 2013, 2:26:47 PM6/24/13
to Lotte R. Leben, prot...@googlegroups.com
Try

x = packetMessage.checkMessage.balanceUpdates.add()
x.tid = 111
x.amount = 222

See https://developers.google.com/protocol-buffers/docs/pythontutorial
for more info.
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+u...@googlegroups.com.
> To post to this group, send email to prot...@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Lotte R. Leben

unread,
Jun 24, 2013, 3:44:48 PM6/24/13
to prot...@googlegroups.com, Lotte R. Leben, imi...@alum.mit.edu
Hi Ilia,

Am Montag, 24. Juni 2013 20:26:47 UTC+2 schrieb Ilia Mirkin:
Try

x = packetMessage.checkMessage.balanceUpdates.add()
x.tid = 111
x.amount = 222

works like a charm, thank you so much!

Mingjian Lu

unread,
Jul 20, 2017, 11:06:33 AM7/20/17
to Protocol Buffers, lo...@zombietetris.de, imi...@alum.mit.edu
For anyone who might reach here for the same question. I suggest using extend instead of add and then give values to fields.  For example:

packetMessage.checkMessage.balanceUpdates.extend([balanceUpdateMessage])

This is much simpler and also easy for assigning object after deserialization and nested structures.
Reply all
Reply to author
Forward
0 new messages