You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a struct has a method of the form (T) Equal(T) bool, cmp.Equal is using that instead of the default evaluation process, as documented in the Godoc.
If one wants to use options such as cmpopts.IgnoreFields, they are ignored, because the rule above takes precedence.
In some situations (e.g. in tests), it might be useful to be able to ignore fields even on structs that have an Equal method.
Example
package main
import (
"fmt""github.com/google/go-cmp/cmp""github.com/google/go-cmp/cmp/cmpopts"
)
typeEntrystruct {
IDstringNamestring
}
func (eEntry) Equal(otherEntry) bool {
returne.ID==other.ID&&e.Name==other.Name
}
funcmain() {
a:=Entry{
ID: "123",
Name: "EntryA",
}
b:=Entry{
ID: "124",
Name: "EntryA",
}
// Returns false, as expected.fmt.Println(cmp.Equal(a, b))
// Also returns false, though one might expect true from reading this statement.fmt.Println(cmp.Equal(a, b, cmpopts.IgnoreFields(Entry{}, "ID")))
}
Suggestion
An idea would be to provide an additional Option to ignore the struct's Equal method even if it exists.
Please let me know if there's already a way to bypass this that I might have missed.
Thanks!
The text was updated successfully, but these errors were encountered:
Hi, thanks for the issue report. It may make sense to provide an option to ignore the Equal method.
In the mean time you can do something like:
typeEntryNoMethodsEntry// type definitions in Go drop methods of the parent
cmp.Equal(...,
// Transform Entry into EntryNoMethods to avoid calling the equal method.cmp.Transformer("", func(eEntry) EntryNoMethods { returnEntryNoMethods(e) }),
// Ignore the ID field on post-transformation EntryNoMethods type.cmpopts.IgnoreFields(EntryNoMethods{}, "ID"),
)
Summary
If a struct has a method of the form
(T) Equal(T) bool
,cmp.Equal
is using that instead of the default evaluation process, as documented in the Godoc.If one wants to use options such as
cmpopts.IgnoreFields
, they are ignored, because the rule above takes precedence.In some situations (e.g. in tests), it might be useful to be able to ignore fields even on structs that have an
Equal
method.Example
Suggestion
An idea would be to provide an additional
Option
to ignore the struct'sEqual
method even if it exists.Please let me know if there's already a way to bypass this that I might have missed.
Thanks!
The text was updated successfully, but these errors were encountered: