5 ms·
> Fields with scalar types are always present. Even if you don’t set them. Did I mention that (at least in proto31) all protobuffers can be zero-initialized wit
by ronnier 7y ago
> Fields with scalar types are always present. Even if you don’t set them. Did I mention that (at least in proto31) all protobuffers can be zero-initialized with absolutely no data in them? Scalar fields get false-y values—uint32 is initialized to 0 for example, and string is initialized as "".
> It’s impossible to differentiate a field that was missing in a protobuffer from one that was assigned to the default value. Presumably this decision is in place in order to allow for an optimization of not needing to send default scalar values over the wire.
I believe there’s a trick you can do if you mark it as a “oneof” with only one field.
- humbledrone 7y ago> It’s impossible to differentiate a field that was missing in a protobuffer from one that was assigned to the default value. Presumably this decision is in place in order to allow for an optimization of not needing to send default scalar values over the wire. Isn't this just flat incorrect? You can tell the difference between set-to-default and not-set with buffer.has_some_field().
- ronnier 7y agoNot for things like ints and strings.
- kentonv 7y agoIt depends on which version you're using. In proto1 and proto2, every field had a "has" method and "explicitly set to default value" was different from "absent". In proto3, they tried to remove this feature, and instead said that for basic types, "set to default" and "absent" are the same thing. (I wrote proto2. I left Google before proto3 came about.)
- humbledrone 7y agoThanks. I must be only used to proto2.