google protobuffer 如何在proto 文件中定义list of lists?
google protobuffer how to define list of lists in proto file?
我有一个 class 列表字段列表如下:
public class MyClass{
private List<List<String>>
}
如何在proto文件中定义?
- 您可以在名为 message.
的原型文件中声明自己的“类型”
- 如果您想声明一个列表,您应该使用 repeated 关键字。
结合这两个给我们:
message ListOfListsOfStrings {
repeated ListOfStrings listOfStrings=1;
}
message ListOfStrings {
repeated string strings=1;
}
然后您可以在您的原型中使用 ListOfListsOfStrings 消息是合适的。
我也在想同样的事情,我了解到我可以:
- 定义为
stream
- 定义为
repeated
如下图:
syntax = "proto3";
import "google/protobuf/empty.proto";
message Dummy {
string foo = 1;
string bar = 2;
}
message DummyList {
repeated Dummy dummy = 1;
}
service DummyService {
rpc getDummyListWithStream(google.protobuf.Empty) returns (stream Dummy) {}
rpc getDummyListWithRepeated(google.protobuf.Empty) returns (DummyList) {}
}
所以,哪一个?
In general, if your use case would allow the client to process the
incoming messages one at a time, the stream is the better choice. If
your client will just be blocking until all of the messages arrive and
then processing them in aggregate, the repeated field may be
appropriate, but even in this scenario the stream would work just as
well, except for losing some potential compressibility.
从这里引用:https://groups.google.com/forum/#!topic/grpc-io/F23vXwilTq0
协议缓冲区没有任何类型的列表。但是列表是字符串的重复值。因此,我们可以创建一个具有字符串输入的消息,并使用来自另一条消息的重复关键字调用该消息
例如
message person{
repeated nameList list = 1;
}
message nameList{
string name = 1;
}
我有一个 class 列表字段列表如下:
public class MyClass{
private List<List<String>>
}
如何在proto文件中定义?
- 您可以在名为 message. 的原型文件中声明自己的“类型”
- 如果您想声明一个列表,您应该使用 repeated 关键字。
结合这两个给我们:
message ListOfListsOfStrings {
repeated ListOfStrings listOfStrings=1;
}
message ListOfStrings {
repeated string strings=1;
}
然后您可以在您的原型中使用 ListOfListsOfStrings 消息是合适的。
我也在想同样的事情,我了解到我可以:
- 定义为
stream
- 定义为
repeated
如下图:
syntax = "proto3";
import "google/protobuf/empty.proto";
message Dummy {
string foo = 1;
string bar = 2;
}
message DummyList {
repeated Dummy dummy = 1;
}
service DummyService {
rpc getDummyListWithStream(google.protobuf.Empty) returns (stream Dummy) {}
rpc getDummyListWithRepeated(google.protobuf.Empty) returns (DummyList) {}
}
所以,哪一个?
In general, if your use case would allow the client to process the incoming messages one at a time, the stream is the better choice. If your client will just be blocking until all of the messages arrive and then processing them in aggregate, the repeated field may be appropriate, but even in this scenario the stream would work just as well, except for losing some potential compressibility.
从这里引用:https://groups.google.com/forum/#!topic/grpc-io/F23vXwilTq0
协议缓冲区没有任何类型的列表。但是列表是字符串的重复值。因此,我们可以创建一个具有字符串输入的消息,并使用来自另一条消息的重复关键字调用该消息
例如
message person{
repeated nameList list = 1;
}
message nameList{
string name = 1;
}