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

Fix a couple of reflection based comparisons on protos in tests #1706

Merged
merged 2 commits into from Jun 20, 2019

Conversation

Martin2112
Copy link
Contributor

This is not safe as protos have additional internal fields.

Checklist

This is not safe as protos have additional internal fields.
@@ -409,7 +408,7 @@ func TestGetLeavesByRange(t *testing.T) {
if test.wantErr != "" {
t.Errorf("GetLeavesByRange(%d, %+d)=_,nil; want nil, err containing %q", req.StartIndex, req.Count, test.wantErr)
}
if got := rsp.Leaves; !reflect.DeepEqual(got, test.want) {
if got := rsp.Leaves; !leavesEqual(got, test.want) {
Copy link
Contributor

@gdbelvin gdbelvin Jun 20, 2019

Choose a reason for hiding this comment

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

Suggested change
if got := rsp.Leaves; !leavesEqual(got, test.want) {
if got := rsp.Leaves; !go-cmp.Equal(got, test.want, cmp.Comparer(proto.Equal)) {

consider using go-cmp?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was put off by their doc that says it "may" use Equal methods. As protos have exported internal fields if it doesn't use Equal it's still wrong.

Copy link
Contributor

@gdbelvin gdbelvin Jun 20, 2019

Choose a reason for hiding this comment

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

My understanding was that it would use the comparer when possible, and reflect.Equal otherwise, which in the case of protos, is what we want IIUC.

https://godoc.org/github.com/google/go-cmp/cmp#Comparer

Comparer returns an Option that determines whether two values are equal to each other.

The comparer f must be a function "func(T, T) bool" and is implicitly filtered to input values assignable to T. If T is an interface, it is possible that f is called with two values of different concrete types that both implement T.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could just be poor wording in the go-cmp docs. We need to prevent reflect.Equal being used on protos as it contains exported fields that are not really part of the public state. If go-cmp always uses Equal then it's probably the right choice.

Copy link
Contributor

Choose a reason for hiding this comment

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

cmp.Comparer(proto.Equal) would prevent reflect.Equal from being used on protos.

I've had troubles before where reflect.Equal complained about the XXX_ fields not being the same and I resolved it with cmp.Comparer(proto.Equal)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@Martin2112 Martin2112 merged commit ba6006e into google:master Jun 20, 2019
@Martin2112 Martin2112 deleted the no_proto_reflect branch June 20, 2019 13:26
@Martin2112
Copy link
Contributor Author

Towards #1738.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants