0

I'm unable to find specific guidance on naming conventions for Google protobuff enum values to avoid naming collisions.

since protobuff enums are global, the following would cause a naming collision :

enum CommissionType {
    UNKNOWN = 0;
    AMOUNT = 1;
    PERCENTAGE = 2;
}

enum ChargeType {
    UNKNOWN = 0;
    AMOUNT = 1;
    PERCENTAGE = 2;
}

Therefore, the advice I have received is to include the TypeName, but its unclear whether the convention is to prepend or append that typename (as below). I'm personally of the opinion that its more readable to append it and get the benefit of intellisense, but I'm unable to find any evidence for or against. I would be grateful if you could link to any examples or discussions. Thanks.

enum CommissionType {
    COMMISSION_TYPE_UNKNOWN = 0;
    COMMISSION_TYPE_AMOUNT = 1;
    COMMISSION_TYPE_PERCENTAGE = 2;
}

enum CommissionType {
    UNKNOWN_COMMISSION_TYPE = 0;
    AMOUNT_COMMISSION_TYPE = 1;
    PERCENTAGE_COMMISSION_TYPE = 2;
}
Ryan C
  • 572
  • 5
  • 18
Jonnie
  • 101
  • 14

1 Answers1

0

ok, answered this myself after some digging around and experimentation. Posting here for completeness.

The correct convention is to prefix the Type Name within the enum value. The c# transpiler is smart enough to strip the Type Name out of the generated code. This will not happen if the TypeName is appended :

enum CommissionType {
    COMMISSION_TYPE_UNKNOWN = 0;
    COMMISSION_TYPE_AMOUNT = 1;
    COMMISSION_TYPE_PERCENTAGE = 2;
}

This will be resolved by the protobuff transpiler into generated code :

#region Designer generated code
...
  public enum CommissionType {
    [pbr::OriginalName("COMMISSION_TYPE_UNKNOWN")] Unknown = 0,
    [pbr::OriginalName("COMMISSION_TYPE_AMOUNT")] Amount = 1,
    [pbr::OriginalName("COMMISSION_TYPE_PERCENTAGE")] Percentage = 2,
  }
...
#endregion Designer generated code

and is consumed in the following legible and usable manner :

TypeOfCommission = CommissionType.Percentage,

For completeness, the solution I initially thought best, more readable *.proto code, ends up with the undesirable result :

enum CommissionType {
    UNKNOWN_COMMISSION_TYPE = 0;
    AMOUNT_COMMISSION_TYPE = 1;
    PERCENTAGE_COMMISSION_TYPE = 2;
}

#region Designer generated code
...
public enum CommissionType {
    [pbr::OriginalName("UNKNOWN_COMMISSION_TYPE")] UnknownCommissionType = 0,
    [pbr::OriginalName("AMOUNT_COMMISSION_TYPE")] AmountCommissionType = 1,
    [pbr::OriginalName("PERCENTAGE_COMMISSION_TYPE")] PercentageCommissionType = 2,
  }
...
#endregion Designer generated code

Consuming this enum :

TypeOfCommission = CommissionType.PercentageCommissionType,
Jonnie
  • 101
  • 14