Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to give default values in protocol buffer?

message Person {
  required Empid = 1 [default = 100];
  required string name = 2 [default = "Raju"];
  optional string occupation = 3;
  repeated string snippets = 4;
}

Can I give the default values as mentioned above?

like image 883
Neelesh I Avatar asked May 26 '16 11:05

Neelesh I


2 Answers

For proto3, custom default values are disallowed.

like image 89
Noel Yap Avatar answered Sep 30 '22 06:09

Noel Yap


Update: The below answer is for proto2 only, proto3 doesn't allow custom default values.

Yes, you can give default values as you had written. default is optional for required, but for optional you have to mention the default values else type specific value is automatically assigned. Moreover you forgot to mention the type for Empid.

protobuf language guide states that

If the default value is not specified for an optional element, a type-specific default value is used instead: for strings, the default value is the empty string. For bools, the default value is false. For numeric types, the default value is zero. For enums, the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list.

like image 33
jblixr Avatar answered Sep 30 '22 05:09

jblixr