I'm new to Protobuf v3.
It seems per this doc that Protobuf v3 has implicit presence of fields. Thus, there is no way for a receiver of a Protobuf message to know whether
My question is - why was this decision made?
Let's say for example that I have a field in a Protobuf message called num_children, e.g.
syntax = "proto3";
message Family {
uint32 num_children = 1;
}
As a receiver of Family messages if I see num_children = 0 I have no way of knowing if the family actually has 0 children or if the sender forgot to specify the number of children.
At first glance, this appears to me to be a design flaw. What's the reasoning for this? And what're the best practices around this?
Good news. Presence detection is now back in proto3 and has been for a few months. Just add optional before the fields - they'll be optional either way (in proto3), but this keyword enables presence tracking. This solves the problem of implicit vs explicit default values. It is a bit confusing that optional now means something completely different here, but that's probably better than the problems associated with adding a new keyword/syntax!
syntax = "proto3";
message Family {
optional uint32 num_children = 1;
}
The document you link to is out of date; this is no longer an experimental feature, and is enabled by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With