New proto3 implementation: has_foo() is optional?

429 views
Skip to first unread message

Denis Feklushkin

unread,
Aug 12, 2019, 2:08:51 AM8/12/19
to Protocol Buffers
Hello!

There is a Protobuff 3 implementation for D: https://github.com/dcarp/protobuf-d/

Its author and I am diverged in the interpretation of the Proto3 documentation:

Is it necessary to implement something like has_foo() method as in Googles's Protobuf C++ and Dart libraries or it is violates protocol version 3 and leaved in official libraries only for backward compatibility?

Can someone reasonably support this or that opinion?

Discussion beginned here:
https://github.com/dcarp/protobuf-d/issues/21

Thanks!

Adam Cozzette

unread,
Aug 12, 2019, 12:51:33 PM8/12/19
to Denis Feklushkin, Protocol Buffers
Proto3 doesn't have a concept of field presence for singular primitive fields, so for example there is no difference between an integer field set to 0 and that same field being unset entirely. As a result there shouldn't be has_foo() methods for singular primitive fields, and that is how Google's protobuf libraries work with proto3. There are has_() methods for embedded message fields, though (since those are not considered primitive fields).

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/3f826c6a-c67e-4291-9b36-4922180dd7e3%40googlegroups.com.

Denis Feklushkin

unread,
Aug 22, 2019, 4:43:29 AM8/22/19
to Adam Cozzette, Protocol Buffers


пн, 12 авг. 2019 г. в 23:51, Adam Cozzette <acoz...@google.com>:
Proto3 doesn't have a concept of field presence for singular primitive fields, so for example there is no difference between an integer field set to 0 and that same field being unset entirely.

But in this case it will be impossible to distinguish absence of a field (for example, when changing message format) from the fact that the field is zero and this zero is one of legitime field value?

Is there any explanation/rationale for this?

Alex Van Boxel

unread,
Aug 22, 2019, 8:09:32 AM8/22/19
to Denis Feklushkin, Adam Cozzette, Protocol Buffers
That why you have the Well Known Wrapper Types. They are included in the proto repo and live right next to the Timestamp and Duration:


Adam Cozzette

unread,
Aug 22, 2019, 12:17:48 PM8/22/19
to Denis Feklushkin, Protocol Buffers
On Thu, Aug 22, 2019 at 1:43 AM Denis Feklushkin <feklushk...@gmail.com> wrote:


пн, 12 авг. 2019 г. в 23:51, Adam Cozzette <acoz...@google.com>:
Proto3 doesn't have a concept of field presence for singular primitive fields, so for example there is no difference between an integer field set to 0 and that same field being unset entirely.

But in this case it will be impossible to distinguish absence of a field (for example, when changing message format) from the fact that the field is zero and this zero is one of legitime field value?

That's true, there is no way to distinguish between a field being absent and being explicitly set to zero.
 
Is there any explanation/rationale for this?

Proto3 was designed before I joined the protobuf team, but I believe the main rationale was to simplify implementations and in particular to enable an open-struct style implementation such as what Go protobuf did. The open-struct idea is that you can store fields in a simple struct and just access them directly without the need for a separate bit array indicating field presence.

Denis Feklushkin

unread,
Aug 22, 2019, 1:29:00 PM8/22/19
to Adam Cozzette, Protocol Buffers


чт, 22 авг. 2019 г. в 23:17, Adam Cozzette <acoz...@google.com>:
On Thu, Aug 22, 2019 at 1:43 AM Denis Feklushkin <feklushk...@gmail.com> wrote:


пн, 12 авг. 2019 г. в 23:51, Adam Cozzette <acoz...@google.com>:
Proto3 doesn't have a concept of field presence for singular primitive fields, so for example there is no difference between an integer field set to 0 and that same field being unset entirely.

But in this case it will be impossible to distinguish absence of a field (for example, when changing message format) from the fact that the field is zero and this zero is one of legitime field value?

That's true, there is no way to distinguish between a field being absent and being explicitly set to zero.
 
Is there any explanation/rationale for this?

Proto3 was designed before I joined the protobuf team, but I believe the main rationale was to simplify implementations and in particular to enable an open-struct style implementation such as what Go protobuf did. The open-struct idea is that you can store fields in a simple struct and just access them directly without the need for a separate bit array indicating field presence.

Ok, thanks for the clarification, such idea sounds reasonable.
 
Reply all
Reply to author
Forward
0 new messages