-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
APIv2: proto: Equal behavior on nil and empty messages #965
Comments
I think the v2 answer is the correct one: A nil message pointer is equal to an empty message. A nil pointer is used to represent an unset message field. A nil pointer is also used to represent the default value of a message field, which is an empty message. If I'm increasingly convinced, however, that we should surface nil-ness in the |
Solid arguments. |
I'm looking at a number of A number of both tests and production code do something like the following: if proto.Equal(m1.GetFoo(), m2.GetFoo()) {
...
} In such a use-case, they are depending on presence of the top-level message. That is, this type of code heavily depends on the fact that |
Unfortunate. Perhaps we need to retain the v1 definition of equality as a matter of practicality. A small inconsistency that eases the transition to the v2 API is preferable to a theoretically perfect API that nobody uses. The v1 API mostly treats a nil message as invalid: It refuses to marshal a nil message and it disallows nils in repeated fields. Maintaining those limitations is probably not important, however. |
Ouch… well, the only thing worse than breaking other people’s code is doing it silently. :( Changing semantics is always iffy, no matter how simple it appears. |
The v1 proto.Equal function treats (*Message)(nil) and new(Message) as being different, while v2 proto.Equal treated them as equal since a typed nil pointer is functionally an empty message since the protobuf data model has no concept of presence as a first-class property of messages. Unfortunately, a significant amount of code depends on this distinction that it would be difficult to migrate users from v1 to v2 unless we preserved similar semantics in the v2 proto.Equal. Also, double down on these semantics for protocmp.Transform. Fixes #965 Change-Id: I21e78ba6251401a0ac0ccf495188093973cd7f3f Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/213238 Reviewed-by: Damien Neil <[email protected]>
What should the behavior of the following be?
In v1, the answer is false, but in v2 it is true since a typed nil pointer is functionally equivalent to a read-only empty message.
The challenge right now is that the generated API gives illusions of both:
Get
methods on a typed nil pointer presents the illusion that the message is empty.The text was updated successfully, but these errors were encountered: