1

I am working with a large number of message types with similar but not identical structure. All the stuff that's common among these is in another message. When a message comes in, I parse it using the common message type. However, I can't seem to find a way to access the fields outside of this type (i.e. the non-common fields). Is there a way to access the unknown field set in python?

Edit: I just saw this in the documentation:

"If a message has unknown fields, the current Java and C++ implementations write them in arbitrary order after the sequentially-ordered known fields. The current Python implementation does not track unknown fields."

Does this mean that if I parse using the common type, eg:

proto = msg_pb2.Common() proto.ParseFromString(raw_msg)

Any fields not defined in message Common are thrown away?

sgrg
  • 1,210
  • 9
  • 15
  • 1
    Access the object to get the field (attribute) list. Do you know how to do that? – Prune Oct 01 '15 at 22:14
  • google for `protobuf introspection` – user3159253 Oct 01 '15 at 22:17
  • Yes, I can access the attributes using dir(), but I always see the same attributes regardless of the message coming in - these seem to be the attributes for the base message. – sgrg Oct 01 '15 at 22:28
  • Maybe I should clarify: I receive the message as a serialized blob and instantiate an object from that using the base message type. My problem is, when the message is being parsed, if there are unknown fields, what happens to them - are they stored and if so, where? – sgrg Oct 01 '15 at 22:47
  • 1
    @sgrg Check out [these](http://stackoverflow.com/questions/16607074/accessing-field-of-protobuf-message-of-unknown-type-in-python). – psv Oct 02 '15 at 07:25
  • That's helpful, but I would ideally not want to modify the existing structure. Looks like I might have to resort to that though. – sgrg Oct 02 '15 at 13:24
  • @sgrg Ok, when you have found a solution to your question remember to come back here and post it in an answer :) – psv Oct 07 '15 at 07:38

1 Answers1

1

To someone looking for an answer to this, the reflection module helped me: https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.reflection-module

The relevant sample code:

Sample usage:

file_descriptor = descriptor_pb2.FileDescriptorProto()
file_descriptor.ParseFromString(proto2_string)   
msg_descriptor = descriptor.MakeDescriptor(file_descriptor.message_type[0])
msg_class = reflection.MakeClass(msg_descriptor)
msg = msg_class()

Args:
   descriptor: A descriptor.Descriptor object describing the protobuf.
Returns:
   The Message class object described by the descriptor.
sgrg
  • 1,210
  • 9
  • 15