Protocol Buffers:如何为枚举制作 .proto 文件?

Protocol Buffers: How to make .proto file for enum?

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

没有任何推荐的方法来表示 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)
            );
        }

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