How to design for a future additional enum value in protocol buffers?

后端 未结 1 859
清歌不尽
清歌不尽 2021-01-31 03:25

One of the attractive features of protocol buffers is that it allows you extend the message definitions without breaking code that uses the older definition. In the case of an e

相关标签:
1条回答
  • 2021-01-31 04:10

    Yes, the best approach is to make the first value in the enum something like UNKNOWN = 0. Then old programs reading a protobuf with an enum value they don't recognize will see it as UNKNOWN and hopefully they can handle that reasonably, eg by skipping that element.

    If you want to do this you'll also want to make the enum be optional not required.

    required, generally, means "I'd rather the program just abort than handle something it doesn't understand."

    Note that it must be the first value declared in the proto source - just being the zero value doesn't make it the default.

    0 讨论(0)
提交回复
热议问题