gpt4 book ai didi

java - Protocol Buffers : How to make . 枚举的原型(prototype)文件?

转载 作者:行者123 更新时间:2023-11-29 08:39:51 25 4
gpt4 key购买 nike

我在 java 中的枚举看起来像这样(在下面提到)。它在 .proto 中是什么?无法弄清楚如何处理变量(类型和代码)的构造函数和 Getter 方法。

public enum PaxType {
ADULT("ADT", 'A'),
CHILD("CNN", 'C'),
INFANT("IFT", 'I');

private String type;
private char code;

PaxType(String type, char code) {
this.type = type;
this.code = code;
}

public String getType() {
return type;
}

public char getCode() {
return code;
}
}

最佳答案

基于已接受的答案和一些进一步的调查,这里有一个完整的工作示例。

假设当前目录下有以下文件

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)
);
}

}
}
  1. 在当前目录解压protoc-3.1.0-linux-x86_64.zip
  2. 设置环境变量

    PROTO_HOME=$(pwd)
    PATH=${PROTO_HOME}/bin:${PATH}
  3. *.proto 文件生成 Java 源代码

    protoc PaxTypeEnum.proto --java_out=. --proto_path=${PROTO_HOME}/include:.
  4. 编译Java演示

    javac -cp .:protobuf-java-3.1.0.jar TestProtobufEnum.java
  5. 运行 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

关于java - Protocol Buffers : How to make . 枚举的原型(prototype)文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41119308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com