Protobuf: How to .Unpack( an Any Message in Python

Created on 23 Mar 2018  路  3Comments  路  Source: protocolbuffers/protobuf

Hi there,

I have some trouble, unpacking an Any message from a protobuf message. Here is a description of what I did:

  1. While building my Protobuf message, I generate a message based on my proprietary protocol file as follow:
# we generate our proprietary message based on our proprietary proto-file
myMessageExtension = extension_pb2.GeneralContent_Extension()
myMessageExtension.fancyID = 345
myMessageExtension.fancyProjectDescription = "All awesome!"
  1. I hand-over the proprietary message to my general message, that contains the "extension" defined as Any:
# We generate a carrier message to push our proprietary message into the Any-extension of the official spec
some_any = any_pb2.Any()
some_any.Pack(myMessageExtension)
  1. I attach the extension to my general message:
# Fourth, we push our proprietary message to the extension of the official standard
myMessage.extension.CopyFrom(some_any)

So far, so good.

But now I want to read the same extension:

  1. I generate a local any, copy the content of the already de-serialized protobuf message in there and unpack it:
# we now get the extension out of the protobuf
some_any2 = any_pb2.Any()
some_any2.CopyFrom(myMessage_rewrite.extension)
some_any2.Unpack(extension_pb2.GeneralContent_Extension())
  1. I double-check, if the some_any2 message now reflects the extension definition (very simple message with just the two attributes):
print some_any2
print "---"
print myMessage_rewrite.extension
  1. ISSUE: Both prints are reflecting just the same message (see below). My web-search unveiled, that the "Unpack(" will just return a "TRUE", if it is possible to convert. But: WHERE DO I FIND THE UNPACKED message itself?
[type.googleapis.com/project.protobuf.extension.data.GeneralContent_Extension] {
  fancyID: 345
  fancyProjectDescription: "All awesome!"
}
---
[type.googleapis.com/project.protobuf.extension.data.GeneralContent_Extension] {
  fancyID: 345
  fancyProjectDescription: "All awesome!"
}
python question

Most helpful comment

In for 4th step, you need to create your message before unpack. Please try:

some_any2 = any_pb2.Any()
some_any2.CopyFrom(myMessage_rewrite.extension)
unpacked_msg = extension_pb2.GeneralContent_Extension())
some_any2.Unpack(unpacked_msg)

Closing it for clean up. Feel free to reopen if it is still an issue

All 3 comments

Hi all,

I am still having this issue? Could anyone find a solution for that our give me a hint?

Thanks,
Timm

In for 4th step, you need to create your message before unpack. Please try:

some_any2 = any_pb2.Any()
some_any2.CopyFrom(myMessage_rewrite.extension)
unpacked_msg = extension_pb2.GeneralContent_Extension())
some_any2.Unpack(unpacked_msg)

Closing it for clean up. Feel free to reopen if it is still an issue

Hi Anandolee!

Thanks for the help. It now works just fine!

Best,
Timm

Was this page helpful?
0 / 5 - 0 ratings