开发者

What's the default enum value in Protobuf?

Hello What's the default enum value (if there isn't any default value defined) in Google Pro开发者_开发百科tocol buffer using with Java?


It is the first one defined in .proto order.

From the .proto language guide (since all implementations use the same logic here):

Optional Fields And Default Values

(snip) For enums, the default value is the first value listed in the enum's type definition.


from the official spec:

optional: the field may or may not be set. If an optional field value isn't set, a default value is used. For simple types, you can specify your own default value, as we've done for the phone number type in the example. Otherwise, a system default is used: zero for numeric types, the empty string for strings, false for bools. For embedded messages, the default value is always the "default instance" or "prototype" of the message, which has none of its fields set. Calling the accessor to get the value of an optional (or required) field which has not been explicitly set always returns that field's default value.

You can set a default value as follows:

optional PhoneType type = 2 [default = HOME];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜