协议缓冲区:如何为枚举制作.proto文件?

时间:2016-12-13 10:52:56

标签: java enums protocol-buffers grpc

我在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;
    }
}

2 个答案:

答案 0 :(得分:2)

没有任何推荐的方法来表示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界面访问注释。

答案 1 :(得分:2)

根据已接受的答案和一些进一步调查,这是一个完整的工作示例。

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

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
    
  6. 输出

    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