Luka Špoljarić
Luka Špoljarić

Reputation: 999

Protobuff message from Java List<List<Object>>

I've been trying to package Java List> to a protobuf3 message and I am just wondering what would be the best way to define a proto file that can package such a List and how to map it effectively, since I've been trying and couldn't make it work.

My initial idea:

// Proto file
message TutorialAPIResponse {
    repeated google.protobuf.Any values = 1;
}

//Packaging
List<List<Object>> apiValues = Api.getWhatever();
AnalyzeSignalsResponse response = AnalyzeSignalsResponse.newBuilder()
                // line below gives compile error "error: incompatible types: List<List<Object>> cannot be converted to Iterable<? extends Any>"
                .addAllValues(values) 
                .build();

Upvotes: 0

Views: 1787

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062502

You can't really express a doubly-nested list in protobuf; you'd need to express a list of something that itself has a list; so.. a List<Foo> for some Foo that has a List<Object>, or in protobuf terms:

message TutorialAPIResponse {
    repeated Foo items = 1;
}
message Foo {
    repeated google.protobuf.Any values = 1;
}

Also, personally I'd avoid the use of Any. If you can't express what you're returning without saying Any, IMO it isn't a good API surface.

Upvotes: 4

Related Questions