Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    }
}
like image 371
Vivek Sinha Avatar asked Oct 26 '25 23:10

Vivek Sinha


1 Answers

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

    }
}
  1. unzip protoc-3.1.0-linux-x86_64.zip in current directory
  2. set environment variables

    PROTO_HOME=$(pwd)
    PATH=${PROTO_HOME}/bin:${PATH}
    
  3. generate the Java source from the *.proto file

    protoc PaxTypeEnum.proto --java_out=. --proto_path=${PROTO_HOME}/include:.
    
  4. compile the Java demo

    javac -cp .:protobuf-java-3.1.0.jar TestProtobufEnum.java
    
  5. 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
like image 118
SubOptimal Avatar answered Oct 28 '25 16:10

SubOptimal