缓冲区:如何使.proto文件为枚举? [英] Protocol Buffers: How to make .proto file for enum?
本文介绍了缓冲区:如何使.proto文件为枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public enum PaxType {
ADULT(ADT,A),
CHILD(CNN,C),
INFANT(IFT,I);
private String type;
私有char代码;
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
code> syntax =proto2;
importgoogle / protobuf / descriptor.proto;
message EnumProto {
extends google.protobuf.EnumValueOptions {
可选字符串名称= 50000;
可选字符串singleCharCode = 50001;
}
枚举PaxType {
ADULT = 0 [(name)=ADT,(singleCharCode)='A'];
CHILD = 1 [(name)=CNN,(singleCharCode)='C'];
INFANT = 2 [(name)=IFT,(singleCharCode)='I'];
}
}
TestProtobufEnum.java / p>
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(为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)
);
}
}
}
- unzip
protoc-3.1.0-linux-x86_64.zip
在当前目录中 -
设置环境变量
PROTO_HOME = $(pwd)
PATH = $ {PROTO_HOME} / bin:$ {PATH}
-
从
*中生成Java源代码
文件protoc PaxTypeEnum.proto --java_out =。 --proto_path = $ {PROTO_HOME} /包括:。
-
编译Java演示
javac -cp。:protobuf-java-3.1.0.jar TestProtobufEnum.java
-
运行Java演示
java -cp。:protobuf-java-3.1.0。 jar TestProtobufEnum
输出
为PaxType.CHILD动态获取字段
字段:描述符:名称值:CNN
字段:描述符:singleCharCode值:C
静态获取字段
PaxType:ADULT名称:ADT singleCharCode:A
PaxType:CHILD名称:CNN singleCharCode:C
PaxType:INFANT名称:IFT singleCharCode:I
My enum in java looks like this(mentioned below). What it would be in .proto? Not able to figure out how to take care of constructor and Getter methods for the variables (type and code).
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;
}
}
解决方案
Based on the accepted answer and some further investigation here a full working example.
assume following files in the current directory
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)
);
}
}
}
- unzip
protoc-3.1.0-linux-x86_64.zip
in current directory set environment variables
PROTO_HOME=$(pwd) PATH=${PROTO_HOME}/bin:${PATH}
generate the Java source from the
*.proto
fileprotoc PaxTypeEnum.proto --java_out=. --proto_path=${PROTO_HOME}/include:.
compile the Java demo
javac -cp .:protobuf-java-3.1.0.jar TestProtobufEnum.java
run the Java demo
java -cp .:protobuf-java-3.1.0.jar TestProtobufEnum
output
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
这篇关于缓冲区:如何使.proto文件为枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文