Java 协议缓冲区:如何为枚举生成.proto文件?
我的java枚举如下所示(如下所述)。那会是什么,原版?无法理解如何处理变量(类型和代码)的构造函数和Getter方法Java 协议缓冲区:如何为枚举生成.proto文件?,java,enums,protocol-buffers,grpc,Java,Enums,Protocol Buffers,Grpc,我的java枚举如下所示(如下所述)。那会是什么,原版?无法理解如何处理变量(类型和代码)的构造函数和Getter方法 没有任何推荐的方法来表示java枚举类。但是你可以按照下面的方法来做 import "google/protobuf/descriptor.proto"; extend google.protobuf.EnumValueOptions { optional string type= 51234; optional string code= 51235; } enum
没有任何推荐的方法来表示java枚举类。但是你可以按照下面的方法来做
import "google/protobuf/descriptor.proto";
extend google.protobuf.EnumValueOptions {
optional string type= 51234;
optional string code= 51235;
}
enum PaxType {
ADULT = 0 [(type) = "ADT", (code) = 'A'];
CHILD = 1 [(type) = "CNN", (code) = 'C'];
INFANT = 2 [(type) = "IFT", (code) = 'I']
}
注释可通过
EnumValueDescriptor
界面访问。基于接受的答案和一些进一步的调查,这里是一个完整的工作示例
假设当前目录中有以下文件
PaxTypeEnum.proto
TestProtobufEnum.java
// https://github.com/google/protobuf/releases
protoc-3.1.0-linux-x86_64.zip
// https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java
protobuf-java-3.1.0.jar
PaxTypeEnum.proto
syntax = "proto2";
import "google/protobuf/descriptor.proto";
message EnumProto {
extend google.protobuf.EnumValueOptions {
optional string name = 50000;
optional string singleCharCode = 50001;
}
enum PaxType {
ADULT = 0 [(name) = "ADT", (singleCharCode) = 'A'];
CHILD = 1 [(name) = "CNN", (singleCharCode) = 'C'];
INFANT = 2 [(name) = "IFT", (singleCharCode) = 'I'];
}
}
TestProtobufEnum.java
import com.google.protobuf.DescriptorProtos;
import com.google.protobuf.Descriptors;
import java.util.Map;
import java.util.Set;
class TestProtobufEnum {
public static void main(String[] args) {
PaxTypeEnum.EnumProto.PaxType CHILD =.
PaxTypeEnum.EnumProto.PaxType.CHILD;
System.out.println("get fields dynamically for PaxType.CHILD");
Set<Map.Entry<Descriptors.FieldDescriptor, Object>> allFields =.
CHILD.getValueDescriptor().getOptions().getAllFields().entrySet();
for (Map.Entry<Descriptors.FieldDescriptor, Object> entry : allFields){
System.out.printf("field: descriptor: %-14s value: %s%n",
entry.getKey().getName(),
entry.getValue()
);
}
System.out.println("get fields statically");
PaxTypeEnum.EnumProto.PaxType[] paxTypes =.
PaxTypeEnum.EnumProto.PaxType.values();
for (PaxTypeEnum.EnumProto.PaxType value : paxTypes) {
DescriptorProtos.EnumValueOptions options =.
value.getValueDescriptor().getOptions();
System.out.printf("PaxType: %-6s name: %s singleCharCode: %s%n",
value.toString(),
options.getExtension(PaxTypeEnum.EnumProto.name),
options.getExtension(PaxTypeEnum.EnumProto.singleCharCode)
);
}
}
}
*.proto
文件生成Java源代码
protoc PaxTypeEnum.proto --java_out=. --proto_path=${PROTO_HOME}/include:.
javac -cp .:protobuf-java-3.1.0.jar TestProtobufEnum.java
java -cp .:protobuf-java-3.1.0.jar TestProtobufEnum
javac -cp .:protobuf-java-3.1.0.jar TestProtobufEnum.java
java -cp .:protobuf-java-3.1.0.jar TestProtobufEnum
get fields dynamically for PaxType.CHILD
field: descriptor: name value: CNN
field: descriptor: singleCharCode value: C
get fields statically
PaxType: ADULT name: ADT singleCharCode: A
PaxType: CHILD name: CNN singleCharCode: C
PaxType: INFANT name: IFT singleCharCode: I
你能详细说明一下数字50000和50001的含义吗?试图理解这些数字,thanks@skal数字是字段编号,用于唯一标识编码中的字段。有关更多信息,请查看文档。您能否详细说明51234和51235数字的含义?努力理解那些数字,谢谢