Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol Buffer: Enum issue

Tags:

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 try to comile it using protoc , I get the following error :

proto.proto:19:5: "X" is already defined proto.proto:19:5: Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it. Therefore, "X" must be unique , not just within "Enum2".

I there any way I could overcome this issue !

like image 418
Echo Avatar asked Oct 31 '11 10:10

Echo


People also ask

What is enum in Protobuf?

enum is one of the composite datatypes of Protobuf. It translates to an enum in the languages that we use, for example, Java.

What is the difference between proto2 and proto3?

Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.

How do I set default value in Protobuf?

For bool s, the default value is false. For numeric types, the default value is zero. For enums , the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list.

Can Protobuf fields be null?

Protobuf treats strings as primitive types and therefore they can not be null.


1 Answers

You could include your enum inside another message so the visibility will not conflict.

Exemple :

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 could also prefix your enum value with something. If you don't change the number after the name of your value, it should stay compatible with your old version : ex:

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; } 
like image 162
Mike Avatar answered Sep 20 '22 15:09

Mike