如何编写 protobuf 的内联数组
How to write an inline array of protobuf
我使用 Google 的 ProtoBuf 并设置了很多值,如下所示:
optional string force_sampling = 1;
optional string status = 2;
optional string host = 3;
optional string server_addr = 4;
optional string server_port = 5;
optional string client_addr = 6;
optional string request = 7;
optional string msec = 8;
optional string request_time = 9;
optional string logid = 10;
optional string request_body = 11;
optional string response_body = 12;
optional string other = 100;
所以,当我为一条消息设置一个值时,我写了很多像下面这样的结构:
set_logid(); set_request_body(); set_other(); set_request_body(); etc.
我能有更简单的方法吗?
例如,类似于:
array way={"set_logid","set_other"}
for (;i = 0;i < len)
{
sample.way[i]()
}
顺便说一下,set_logid
是内联
您可以使用 Message::GetReflection()
函数并使用它按字符串中给定的名称访问字段。
文档在这里:
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#Reflection
然而,这会变得更慢且更复杂,因此可能不值得。
我使用 Google 的 ProtoBuf 并设置了很多值,如下所示:
optional string force_sampling = 1;
optional string status = 2;
optional string host = 3;
optional string server_addr = 4;
optional string server_port = 5;
optional string client_addr = 6;
optional string request = 7;
optional string msec = 8;
optional string request_time = 9;
optional string logid = 10;
optional string request_body = 11;
optional string response_body = 12;
optional string other = 100;
所以,当我为一条消息设置一个值时,我写了很多像下面这样的结构:
set_logid(); set_request_body(); set_other(); set_request_body(); etc.
我能有更简单的方法吗? 例如,类似于:
array way={"set_logid","set_other"}
for (;i = 0;i < len)
{
sample.way[i]()
}
顺便说一下,set_logid
是内联
您可以使用 Message::GetReflection()
函数并使用它按字符串中给定的名称访问字段。
文档在这里: https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#Reflection
然而,这会变得更慢且更复杂,因此可能不值得。