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
Printing a protobuf message changes its equality #1173
Comments
Read operations on messages can modify internal cached state in the message, as you are seeing here. For this and other reasons, Instead, use the if !cmp.Equal(pbt1, pbt2, protocmp.Transform()) {
....
} |
...or in the simple case of just comparing two messages (not slices of messages, or some other more complex data structure including a message), just use The |
Makes sense, but it is a little harder when using test helpers like go mock: This is the actual place it failed in our code: mockSnowflake.EXPECT().GetAggregationCounts(
gomock.Any(), // ctx
expectedParams, // Includes protobuf messages. Fails on golang/protobuf v1.4.2, passes on v1.3.5
).Return(
data,
nil,
) I've solved it with this matcher: func EqualsProto(x interface{}) gomock.Matcher {
return equalsProtobufMatcher{x}
}
type equalsProtobufMatcher struct {
x interface{}
}
func (e equalsProtobufMatcher) Matches(x interface{}) bool {
return cmp.Equal(e.x, x, protocmp.Transform())
}
func (e equalsProtobufMatcher) String() string {
return fmt.Sprintf("is equal to %v", e.x)
} Then my test code looks like: mockSnowflake.EXPECT().GetAggregationCounts(
gomock.Any(), // ctx
EqualsProto(expectedParams),
).Return(
data,
nil,
) |
For future reference, I've added a FAQ entry for DeepEqual on messages: https://developers.google.com/protocol-buffers/docs/reference/go/faq#deepequal |
Moved to buf build for provisioning generated code. This ensures our protos conform to standards with `buf lint`. It also ensures reliable builds by utilizing a standard build toolchain. Vendored dependent protobufs so project can be built without special containers. Updated gogo/protobuf and small API changes as a result. Fixed grpc tests that were using unsupported reflect.DeepEqual see golang/protobuf#1173 (comment)
What version of protobuf and what language are you using?
Version:
v1.4.2
What did you do?
What did you expect to see?
Test should pass
What did you see instead?
Test fails with:
Anything else we should know about your project / environment?
This bug came up when I tried upgrading to the newest package version (from v1.3.5).
We have a test that uses gomock, and it takes in a protobuff with WKT timestamps in it.
Previously this test passed, but now it fails because we are printing the protobuf at some point.
This really should still pass.
The text was updated successfully, but these errors were encountered: