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?

7
  • 1
    Access the object to get the field (attribute) list. Do you know how to do that? Commented Oct 1, 2015 at 22:14
  • google for protobuf introspection Commented Oct 1, 2015 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. Commented Oct 1, 2015 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? Commented Oct 1, 2015 at 22:47
  • 1
    @sgrg Check out these. Commented Oct 2, 2015 at 7:25

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.