Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google protobuffer how to define list of lists in proto file?

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?

like image 550
PaniniGelato Avatar asked Apr 19 '16 10:04

PaniniGelato


People also ask

How do you create a list in 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.

How do I use a list in Protobuf?

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.

What is the difference between proto2 and Proto3?

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.

What is repeated in proto file?

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.


Video Answer


2 Answers

  1. You can declare your own "types" in proto files called message.
  2. If you'd like to declare a list you should use repeated keyword.

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.

like image 163
krems Avatar answered Sep 22 '22 05:09

krems


I was wondering the same thing and I learned that I can:

  1. define as a stream
  2. define as a 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

like image 36
RNA Avatar answered Sep 19 '22 05:09

RNA