import "google/protobuf/descriptor.proto"
  message SelfDescribingMessage {
    required google.protobuf.FileDescriptorSet descriptor_set = 1;
    required string type_name = 2;
    required bytes data = 3;

If you have a FileDescriptorSet, you can (in C++) construct a
SimpleDescriptorDatabase containing the files, then construct a
DescriptorPool wrapping that database, then call
pool.FindMessageTypeByName(type_name) to get a Descriptor for your type.
 Once you have that, you can use DynamicMessageFactory to construct a
message instance corresponding to that descriptor, use it to parse the
message, and then use the reflection interface on it to access the fields.
It sounds complicated but it's only 12 lines of code:

  SelfDescribingMessage message;
  google::protobuf::SimpleDescriptorDatabase db;
  for (int i = 0; i < message.descriptor_set().file_size(); i++) {
  google::protobuf::DescriptorPool pool(&db);
  const proto2::Descriptor* descriptor =
  google::protobuf::DynamicMessageFactory factory;
  google::protobuf::Message* real_message =

(Perhaps someone should create a library encapsulating the above.  The
problem is that there's a lot of different ways to use all these classes and
any library that attempts to hide details will only be able to cover a
subset of use cases.)

On Wed, Aug 26, 2009 at 9:08 AM, Mohammad <> wrote:

> I am trying to see if there is any way meta data could be included in
> the message so if one does not have the proto file, she could still
> parse the message. I saw something about using FileDescriptorSet and
> type_name as the first and second attributes of the message, but the
> description stops there. I appreciate it if you could provide me more
> information on how to generate and parse a message with meta data,
> preferably through an example.
> >

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply via email to