There are several questions related to presence field tracking of scalar fields in protobuf 3, but I didn't find any with generic default approach recommendation.
It may be useful to be able to track field presence in protobuf message. I see at least those cases, where it may be useful:
- API usage simplicity. In this case we provide some sane default values for missed fields and allow user to set only what's actually needed. This is the same way default function parameters work in many programming languages.
- API evolution. If we introduce a new field, which effectively deprecates or replaces an old field, then we can just add a new one to the same message and use the one being set.
- Using the same messages for getting/setting entities and for partial updates, aka patches, of the entities.
Now, protobuf 3, unlike protobuf 2, does not support tracking of scalar field presence. In other words, while protobuf 2 had scalar fields with explicit presence, protobuf 3 has no presence by default for such fields. E.g., having double foo = 1;
in our message, you won't be able to distinguish whether it's set to zero explicitly, or not set at all. But there are multiple ways to overcome this: optional
, oneof
, wrappers, FieldMask
and maybe others.
So, there may be the following two main approaches in using scalar fields:
- Use normal scalar fields with no presence tracking by default, and use fields with explicit presence tracking (e.g.
optional
) only where needed. Generally it means that if we want all the use cases above to work, we can use no presence tracking if zero is not in a valid domain of a field, and use explicit presence tracking otherwise. The problem with this approach, as I see, may arise if at some time zero is not in a valid domain, but later it becomes a valid value for a field. - Use some approach which allows explicit presence tracking of scalar fields by default. E.g. instead of using
double foo = 1;
useoptional double foo = 1;
orgoogle.protobuf.DoubleValue foo = 1;
by default.
What approach would you recommend by default? Also, what specific approach (optional
, wrappers from google.protobuf, etc) would you recommend for explicit presence tracking of scalar fields? With the introduction of optional
in protobuf 3, is there any reason to use other means? And why? Or maybe there is something which I overlooked.