Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf repeated message option

I'm trying to append some documentation metainformation to a protobuf message by extending google.protobuf.MessageOptions. One of my metainfo option may appear more than once. Looks like I can declare repeated option but how can I use it on a message?

Here is an example of what I try to achieve:

extend google.protobuf.MessageOptions {
    optional string description = 51234;
    repeated string usages = 51235;
}

message MyMsg {
    option (description) = "MyMsg description";
    option (usages) = ???

    optional bool myFlag = 1;
    optional string myStr = 2;
}

What should I type instead of ??? if I want fo document two different ways of usage?

like image 878
VestniK Avatar asked Feb 26 '15 08:02

VestniK


1 Answers

If I recall correctly, you can specify a repeated option multiple times:

message MyMsg {
  option (description) = "MyMsg description";
  option (usages) = "usage1";
  option (usages) = "usage2";

  optional bool myFlag = 1;
  optional string myStr = 2;
}

Edit: the way to access repeated field is not documented and took some time to look throug headers so I decide to add it to this answer:

auto opts = MyMsg::descriptor()->options();
std::cout << opts.GetExtension(description) << std::endl;
for (int i = 0; i < opts.ExtensionSize(usages); ++i)
    std::cout << opts.GetExtension(usages, i) << std::endl;
like image 148
Kenton Varda Avatar answered Nov 11 '22 14:11

Kenton Varda