45

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

Harikrishnan
  • 3,664
  • 7
  • 48
  • 77

2 Answers2

71

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 2
    Thank you for your answer. My compiler version was 2.6 and I was using library 2.5. Now it is working. Thank you very much. – Harikrishnan Sep 03 '14 at 06:49
  • @Harikrishnan interesting; did you build the 2.6 compiler yourself? I thought there were only downloads for 2.5? (I'm curious because of keeping an eye on the release of the new `oneof` feature) – Marc Gravell Sep 03 '14 at 06:50
  • @Harikrishnan ah, fair enough; I was only looking at "featured" downloads, but yes, 2.6 is there – Marc Gravell Sep 03 '14 at 08:13
  • 4
    So, what if I want to distinguish an empty array (=new type [0]) from no array (=null)? Additional boolean field or is there any better solution? – Alex Che Mar 31 '16 at 07:31
  • 1
    @AlexChe they're completely indistinguishable in the wire specification (outside of my control), so : yeah, you'd need to handle that separately. – Marc Gravell Mar 31 '16 at 07:34
  • For string, bytes, and message fields, optional is compatible with repeated. Given serialized data of a repeated field as input, clients that expect this field to be optional will take the last input value if it's a primitive type field or merge all input elements if it's a message type field. Note that this is not generally safe for numeric types, including bools and enums. Repeated fields of numeric types can be serialized in the packed format, which will not be parsed correctly when an optional field is expected. https://developers.google.com/protocol-buffers/docs/proto#optional – Leonardo Ramos Duarte Oct 14 '21 at 17:30
  • I want it to be None, or 0..n array in Python. or std::optional> in c++. – vijayst Dec 22 '22 at 03:03
-3

The generated file contains method for retrieving count. e.g. int getXXXCount(); One issue is that such method wouldn't be available for generated file corresponding to prior versions of the protoc def.

Ted
  • 379
  • 1
  • 5
  • 18