Hi, Do you know if it's possible do dynamic decoding at run-time using the Python API?
- https://developers.google.com/protocol-buffers/docs/reference/python/ Indeed there is a descriptor.FileDescriptor, however I'm not sure what the other classes correspond to? Thanks, Mike. On Tuesday, August 12, 2014 at 10:36:13 AM UTC-7, Feng Xiao wrote: > > Protobuf supports creating message types dynamically at runtime and use > them for parsing/serialization/etc. > > First you need to build up a DescriptorPool > <https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.h#1141> > > that contains all types that you may want to use. There are two approaches > to construct this pool. One is to call DescriptorPool::BuildFile() directly > with parsed proto files. For example: > // Convert .proto files into parsed FileDescriptorProto > bool ParseProtoFile(string filename, FileDescriptorProto* result) { > FileInputStream stream(filename); > google::protobuf::io::Tokenizer tokenizer(&stream); > google::protobuf::compiler::Parser parser; > return parser.Parse(&tokenizer, result); > } > // Build the descriptor pool > DescriptorPool pool; > for (string filename : proto_files) { > FileDescriptorProto proto; > ParseProtoFile(filename, &proto); > pool.BuildFile(proto); > } > > After you have the pool, you can query for a type by its name. For > example, DescriptorPool::FindMessageTypeByName(). > > Then to actually parse/serialize/use message types in the pool, you need > to construct message objects around them. DynamicMessage > <https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/dynamic_message.h#53> > > is used for that: > // Suppose you want to parse a message type with a specific type name. > Descriptor* descriptor = > pool.FindMessageTypeByName(message_type_to_parse); > DynamicMessageFactory factory; > unique_ptr<Message> message = factory.GetPrototype(descriptor)->New(); > // Use the message object for parsing/etc. > message->ParseFromString(input_data); > // Access a specific field in the message > FieldDescriptor* field = descriptor->FindFieldByName(field_to_read); > switch (field->type()) { > case TYPE_INT32: message->GetReflection()->GetInt32(*message, field); > break; > ... > } > > On Mon, Aug 11, 2014 at 9:31 PM, Jan Kyjovský <[email protected] > <javascript:>> wrote: > >> Hi, >> >> I have very specific problem. I have data and proto file available and my >> application should take both and based on external configuration determine >> how to interpret data (many different types/messages in proto). Yet that >> can be determine only during run. My question is if there is any support >> for that, I mean that I will be able to parse proto and decode data based >> on content of interpret intermediate structures. >> >> I have been trying to analyze this possibility directly from codes but >> not with much success. I would be glad for any guidance. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Protocol Buffers" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> Visit this group at http://groups.google.com/group/protobuf. >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
