“可选重复”与谷歌protobuf用于Java

时间:2022-08-01 15:23:20

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

我正在使用谷歌Protobuf使用java。我写了一个类似的声明

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

如果我避免了可选关键字,那么它正在编译,但是在java中,我得到了一个com.google.protobuf.ProtocolStringList类没有找到错误的类

Thanks in advance

谢谢提前

2 个解决方案

#1


24  

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.

至于com.google.protobuf。ProtocolStringList:检查正在使用的.proto编译器(protoc)的版本是否与正在使用的库版本完全匹配。

#2


-2  

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.

生成的文件包含检索计数的方法。例如,int getXXXCount();一个问题是,对于与protoc def以前版本对应的生成文件,这种方法不可用。

#1


24  

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.

至于com.google.protobuf。ProtocolStringList:检查正在使用的.proto编译器(protoc)的版本是否与正在使用的库版本完全匹配。

#2


-2  

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.

生成的文件包含检索计数的方法。例如,int getXXXCount();一个问题是,对于与protoc def以前版本对应的生成文件,这种方法不可用。