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)
);
}
}
}
protoc-3.1.0-linux-x86_64.zip in current directoryset environment variables
PROTO_HOME=$(pwd)
PATH=${PROTO_HOME}/bin:${PATH}
generate the Java source from the *.proto file
protoc 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With