Manipulate protobuf generated classes by name

Viewed 19

Is there a well established way to manipulate protobuf classes when I only have the "name" of a field and a string serialized description of the target value?

For example the proto definition can be

message PNumber {
  int32 prefix = 1;
  int32 number = 2;
}

message Office {
  int32 id = 1;
  string address = 2;
  PNumber phone;
}

What I found promising in the C++ generated code is:

void example_use(Office &office,            // C++ generated class
                 std::string const& field,  // Description of the field
                 std::string cosnt& target) // Serialized "payload"
{
    office.GetReflection()->SetField(field, target); // *
    //     ^^^^^^^^^^^^^^^^^^^^^^^^^
}

If there's a comprehensive guide to reflection please point that out, cause trial and error is taking me too much time. If the method I'm describing above can work, my questions are:

  1. Can GetReflectionSetField be used this way?
  2. How can I serialize a field in a form acceptable by SetField?
  3. Is it possible to manage more complex fields, e.g. what if address was a repeated field?

My end goal here is to build a CLI interface to an existing "data model" that would be able to set/get fields by issuing commands like

$SET Office.PNumber.prefix 415
0 Answers
Related