Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different enums, same value in protobuf

I have protocol buffer definitions like this:

package com.asd;

enum AType {
    A1 = 0;
    A2 = 1;
    Unknown = 2;
}

enum BType {
    B1 = 0;
    B2 = 1;
    Unknown = 2;
}

While compiling, I am getting this error:

"Unknown" is already defined in "com.asd". Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it. Therefore, "Other" must be unique within "com.asd", not just within "BType".

Is there a workaround for this problem other than using different packages?

like image 518
Alperen Cetin Avatar asked Dec 17 '25 13:12

Alperen Cetin


2 Answers

I believe there is no straightforward way of using the same enum value in the same package but there are two workarounds (link):

  1. Use prefix:

     package com.asd;
    
     enum AType {
         AType_A1 = 0;
         AType_A2 = 1;
         AType_Unknown = 2;
     }
    
     enum BType {
         BType_B1 = 0;
         BType_B2 = 1;
         BType_Unknown = 2;
     }
    
  2. Put the enums in different messages:

     message ATypeMessage{
         enum AType{
             A1 = 0;
             A2 = 1;
             Unknown = 2;
         }
     }
    
     message BTypeMessage{
         enum BType{
             B1 = 0;
             B2 = 1;
             Unknown = 2;   
         }
     }
    
like image 149
Alperen Cetin Avatar answered Dec 19 '25 07:12

Alperen Cetin


I don't have enough reputation to post this as a comment, and my suggestion is a hack that might not even work. But have you tried to add the option allow_alias = true; in both enum and use the same value.

Ref: https://developers.google.com/protocol-buffers/docs/proto#enum

like image 40
Sirode Avatar answered Dec 19 '25 05:12

Sirode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!