--
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 protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Hi,
pool.BuildFile(proto);
// Suppose you want to parse a message type with a specific type name.const Descriptor* descriptor = pool.FindMessageTypeByName("AddressBook");
Variable proto contains some information about structure although proto.name is blank (don't know why).
Hi,
Hi,Thank you that was one thing I tried but was not successful up until now. Also one more issue was that i had to ask for type by full name. It took me some time to figure it our.Now I got different sort of questions. Since I can now progress further I am finding new "problems". As you suggested i will be using ParseFromString method. Although I am not entirely sure what this string is. Is it represented by hexdump or is it some other format?
I am also wondering about imports. If one proto file imports other do I have to take that consideration and manually handle that file as well or is it done automatically while processing file I have provided? In my implementation all proto files are in one folder with possible imports between each other.
Thank you in advance for your response.
--
Hi,
so we are somehow back in game. And now I am getting familiar what I have done more then half a year ago.
So for now I got some basic structure of code that will later be as a starting point for real implementation. I am trying to do some simple experiments to see how it works. Though I got problem that even if I get descriptor I am not getting any data. I have used example from encoding specification since I needed something really simple.
...
const Descriptor* descriptor = pool.FindMessageTypeByName("tutorial.Test1");
if (descriptor == NULL)
{
return;
}
DynamicMessageFactory factory;
Message *message = factory.GetPrototype(descriptor)->New();
// Use the message object for parsing/etc.
std::string input_data = "089601";
message->ParseFromString(input_data);
// Access a specific field in the message
for (int i = 0; i < descriptor->field_count(); i++)
{
const FieldDescriptor* field = descriptor->field(i);
switch (field->type())
{
case FieldDescriptor::TYPE_INT32:
{
int nVal = message->GetReflection()->GetInt32(*message, field);
printf("%d\n", nVal);
break;
}
...
But from reflection I am getting that value is 0. So either I have wrongly set input data considering that I misunderstand format or there is something else. Do you have any idea what may be cause?
Thank you in advance for your reply.
--
Hi,
yes thank you for that hint. I was able to get enough references to go through decoded data. I am more or less efficiently able to get values (in case of ENUM also original value not only symbolic meaning) but now I would require some auxiliary information which I am not sure are contained in these objects. I will need to get access to current byte offset (from data start) and size of element.
I hope my question are not too strange but I my knowledge about google protocol buffers are a bit limited.
--
Everything is fine until I try to get descriptor by FindMessageTypeByName from pool. It returns null. Am I committing some steps?
I did as you told me. And tried to compile my proto files with protoc. Well it eat them up without any problem so I guess that clear that there is no problem with them but in that case why is it causing such error to me.
As I gave some hint before problem is happening when I am trying to do following with phone.proto which is being added as second into descriptor database
compiler::Parser parser;
parser.RecordErrorsTo(&errCollector);
bool bV = parser.Parse(&tokenizer, result);
std::string strErr = errCollector.GetErrorAndWarnings();
return bV;
Parse returns false and error collector holds following error:
1:1: ERROR: Multiple package definitions.
Do you have nay idea what that could meant?
Protobuf supports creating message types dynamically at runtime and use them for parsing/serialization/etc.First you need to build up a DescriptorPool 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 FileDescriptorProtobool 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 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ý <jan.ky...@tieto.com> 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.
--