I noticed that they bring optional
back in protobuf 3.15. I'm trying to use optional
to check field presence. But I'm still unclear regarding the philosophy behind this.
Here is my usecase:
I'm providing some services that accept protobuf
as my input. But the client side is untrusted from my perspective, therefore I have to check the nullability for the input protobuf.
The way I expect is that,
- for a required field, either it's set, or it's null,
- for an optional field, I don't care I can just use a default value and that won't cause any problem from my system
So I end up adding optional
to every field that should not be null so that I can use hasXXX
to check the presence. This looks weird to me because those fileds are actually required
from my perspective, but I have to add optioanl
keyword for them all.......I'm not sure whether this is a good practice. Proto experts pls give me some suggestions.
Also the default value doesn't make sense to me at all regarding nullability checking, since zero or empty string usually have their own meaning in many scenarios.
google.api.field_behaviour=OPTIONAL
is different from the protobufoptional
keyword, and cannot serve as a replacement. The latter allows you to distinguish default primitive values from unset values, while the former does not. – Overelaborate