The following are my protobuf definitions:
enum Foo {
BAR = 0;
BAZ = 1;
}
message UpdateRequest {
string id = 1;
Foo foo = 2;
.
.
.
}
I need the foo
field of the UpdateRequest
to be nullable. However, if I don't set the value of this field then as per protobuf semantics it always ends up picking BAR
as the default value.
Looking around, I found a couple of ways to handle this.
- Add an additional
UNKNOWN
value in the enum and map it to0
so that this value will be used as the default value instead ofBAR
and I can treat this value as null.
enum Foo {
UNKNOWN = 0;
BAR = 1;
BAZ = 2;
}
- Use
oneof
construct.
However, somehow I feel that both of the above approaches are a kind of a workaround and I am not able to locate official documentation explaining the best practice to handle this case.
What is the best practice to handle this use case?
UNKNOWN_FOO
instead ofUNKNOWN
. – Woolf