-
Notifications
You must be signed in to change notification settings - Fork 209
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
cmpopts.IgnoreFields does not work on structs with an Equal method #352
Comments
Hi, thanks for the issue report. It may make sense to provide an option to ignore the In the mean time you can do something like: type EntryNoMethods Entry // type definitions in Go drop methods of the parent
cmp.Equal(...,
// Transform Entry into EntryNoMethods to avoid calling the equal method.
cmp.Transformer("", func(e Entry) EntryNoMethods { return EntryNoMethods(e) }),
// Ignore the ID field on post-transformation EntryNoMethods type.
cmpopts.IgnoreFields(EntryNoMethods{}, "ID"),
) |
@dsnet I ran across this suprising issue when I tried to use
will hang forever but this:
works reliably. It would be nice to have a |
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: