why
why

Reputation: 1295

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 Protocol buffer using with Java?

Upvotes: 25

Views: 29493

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1064324

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.

Upvotes: 34

dcn
dcn

Reputation: 4469

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];

Upvotes: 6

Related Questions