Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was implicit presence with commonly used defaults decided for Protobuf v3?

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

  • the sender manually set the default value
  • the sender didn't set a value (in which the default is used)

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?

like image 633
Noah Stebbins Avatar asked Oct 17 '25 19:10

Noah Stebbins


1 Answers

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.

like image 160
Marc Gravell Avatar answered Oct 22 '25 06:10

Marc Gravell