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; message.ParseFromString(data); google::protobuf::SimpleDescriptorDatabase db; for (int i = 0; i < message.descriptor_set().file_size(); i++) { db.Add(message.descriptor_set().file(i)); } google::protobuf::DescriptorPool pool(&db); const proto2::Descriptor* descriptor = pool.FindMessageTypeByName(message.type_name()); google::protobuf::DynamicMessageFactory factory; google::protobuf::Message* real_message = factory.GetPrototype(descriptor)->New(); real_message->ParseFromString(message.data()); (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 <kolahdou...@gmail.com> 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 protobuf@googlegroups.com To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/protobuf?hl=en -~----------~----~----~----~------~----~------~--~---