VestniK
VestniK

Reputation: 1920

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?

Upvotes: 6

Views: 12122

Answers (1)

Kenton Varda
Kenton Varda

Reputation: 45151

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;

Upvotes: 7

Related Questions