使用 google protobuf for Java 的“可选重复”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25637687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:22:11  来源:igfitidea点击:

'optional repeated' with google protobuf for Java

javaprotocol-buffers

提问by Harikrishnan

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

我正在使用 Java 使用 Google Protobuf。我写了一个声明,如

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

PS:如果我避免了可选关键字,那么它正在编译,但在 Java 中,我收到了一个找不到类的错误 com.google.protobuf.ProtocolStringList

Thanks in advance

提前致谢

回答by Marc Gravell

All you need is:

所有你需要的是:

repeated string users = 9;

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

您不需要optional修饰符,看起来它使解析器感到困惑。一个repeated领域本质上是optional:你只是不添加任何价值

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) 的版本是否与您使用的库版本完全匹配。

回答by Ted

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 的先前版本相对应的生成文件。