Skip to content
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

equal: Fix handling of nil values #56

Merged
merged 2 commits into from
Aug 17, 2022

Conversation

misberner
Copy link
Contributor

The current equal implementation generates code that recognizes a nil message as being equal to an empty message of the same type. However, this is not how proto.Equal works, where a nil message is only equal to another nil message of the same type.

A complication arises from the fact that nil and empty message values need to be treated as equal in certain contexts - slice elements, map values, oneofs. This is the same principle as the one described in #52, only applied to messages instead of byte slices.

This PR solves this by propagating the nullable (i.e., presence) information to the code generator for comparing message values, and substituting nil values for empty ones for non-nullable values.

Comment on lines +225 to +228
p.P(`p = &`, p.QualifiedGoIdent(msg.GoIdent), `{}`)
p.P(`}`)
p.P(`if q == nil {`)
p.P(`q = &`, p.QualifiedGoIdent(msg.GoIdent), `{}`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super happy with allocating values here, but unfortunately it's not very easy to introduce package-private helper variables on the fly for types where this is needed. Also, nil values in non-nullable contexts should be an exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not allocate values. They stay in the stack. :)

./test_messages_proto2_vtproto.pb.go:1212:9: &TestAllTypesProto2_NestedMessage{} does not escape
./test_messages_proto2_vtproto.pb.go:1215:9: &TestAllTypesProto2_NestedMessage{} does not escape
./test_messages_proto2_vtproto.pb.go:1229:9: &ForeignMessageProto2{} does not escape
./test_messages_proto2_vtproto.pb.go:1232:9: &ForeignMessageProto2{} does not escape

Comment on lines +225 to +228
p.P(`p = &`, p.QualifiedGoIdent(msg.GoIdent), `{}`)
p.P(`}`)
p.P(`if q == nil {`)
p.P(`q = &`, p.QualifiedGoIdent(msg.GoIdent), `{}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not allocate values. They stay in the stack. :)

./test_messages_proto2_vtproto.pb.go:1212:9: &TestAllTypesProto2_NestedMessage{} does not escape
./test_messages_proto2_vtproto.pb.go:1215:9: &TestAllTypesProto2_NestedMessage{} does not escape
./test_messages_proto2_vtproto.pb.go:1229:9: &ForeignMessageProto2{} does not escape
./test_messages_proto2_vtproto.pb.go:1232:9: &ForeignMessageProto2{} does not escape

@vmg vmg merged commit 9aa67ad into planetscale:main Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants