Java – Protocol buffer: enumeration problem

I have the following Proto file:

enum Enum1{
    X=0;
    Y=1;    
}
message SomeClass{
    required  Enum1 enum1=1;
    required Enum2 enum2=2;
}
enum Enum2{
    X=0;
    Z=1;    
}

When I tried to compile it using protocol, I received the following error:

I have a way to overcome this problem!

Solution

You can include your enumeration in another message so that visibility does not conflict

example:

message Enum1{
 enum Enum{
     X=0;
     Y=1;    
 }
}

message Enum2{
 enum Enum{
     X=0;
     Y=1;    
 }
}

message SomeClass{
    required  Enum1.Enum enum1=1;
    required Enum2.Enum enum2=0;
}

You can also add something to the enumeration value If you do not change the number after the value name, it should be compatible with older versions: for example:

enum Enum1{
    E1_X=0;
    E1_Y=1;    
}
enum Enum2{
    E2_X=0;
    E2_Z=1;    
}
message SomeClass{
    required  Enum1 enum1=1;
    required Enum2 enum2=2;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>