I have a class with a list of lists field as below:
public class MyClass{
private List<List<String>>
}
how to define it in a proto file?
You can declare your own "types" in proto files called message. If you'd like to declare a list you should use repeated keyword.
list interface in Java. Now our message class contains a list for snacks. Note that although we have a string list, we can as well have number, Boolean, custom data type list. So, as we see, we are able to read the serialized list by deserializing the binary data to Theater object.
Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.
repeated : this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.
Combining those two gives us:
message ListOfListsOfStrings {
repeated ListOfStrings listOfStrings=1;
}
message ListOfStrings {
repeated string strings=1;
}
You can then use ListOfListsOfStrings message in your proto were appropriate.
I was wondering the same thing and I learned that I can:
stream
repeated
as shown in below:
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) {}
}
so, which one?
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.
referenceed from here: https://groups.google.com/forum/#!topic/grpc-io/F23vXwilTq0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With