Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'optional repeated' with google protobuf for Java

I am using Google Protobuf using java. I wrote a statement like

optional repeated   string  users = 9; 

When I tried to compile I am getting an error like

message.proto:39:57: Missing field number. 

All I wanted was to create an array of strings.

Can anybody help me to resolve it.

PS: If I avoided optional keyword then it is compiling but in java I am getting a class not found error for com.google.protobuf.ProtocolStringList

Thanks in advance

like image 966
Harikrishnan Avatar asked Sep 03 '14 06:09

Harikrishnan


People also ask

What does Repeated mean in protobuf?

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.

Are repeated fields ordered in protobuf?

Yes, repeated fields retain the order of items. From Google's Protocol Buffers encoding specification: The order of the elements with respect to each other is preserved when parsing, though the ordering with respect to other fields is lost.

What is optional protobuf?

In proto3, all fields are "optional" (in that it is not an error if the sender fails to set them). But, fields are no longer "nullable", in that there's no way to tell the difference between a field being explicitly set to its default value vs. not having been set at all.

Does protobuf handle endianness?

Protocol buffers messages always use little-endian encoding.


1 Answers

All you need is:

repeated string users = 9; 

You don't need the optional modifier, and it looks like it is confusing the parser. A repeated field is inherently optional: you just don't add any values.

As for com.google.protobuf.ProtocolStringList: check that the version of the .proto compiler (protoc) you are using is an exact match for the library version you are using.

like image 104
Marc Gravell Avatar answered Sep 28 '22 01:09

Marc Gravell