protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

C++ public enum for field numbers instead of anonymous enum

Open eronnen opened this issue 1 year ago • 6 comments

What language does this apply to? C++

Describe the problem you are trying to solve.

I would like to have a Public enum for all the field numbers for every message. This will help programmers to ensure they handle all possible fields in a message, by writing a switch statement over the field number and compiling the program with the -Wswitch flag.

Describe the solution you'd like

Instead of declaring field numbers in the generated c++ with anonymous enum like

enum : int {
 kFirstFieldNumber = 1,
 kSecondFieldNumber =2,
}

I suggest declaring a named enum that can be used by the programmer, like

enum MessageNameFieldNumber : int {
 kFirstFieldNumber = 1,
 kSecondFieldNumber =2,
}

Describe alternatives you've considered

Probably could be done by external plugin, but it seems much easier generating named enum from protoc itself and shouldn't interfere with existing API .

eronnen avatar Aug 08 '24 13:08 eronnen