Add cmpopts.FilterValue
- Dominant language
- Go
- Stars
- 4.7k
- Forks
- 243
- PR merge metrics
- No merged PRs in 30d
Description
`cmp.FilterValues` expects a function of the signature `func(T, T) bool` where the function is called with values from both the left argument and the right argument.
I propose the addition of `cmpopts.FilterValue` that is semantically equivalent to:
```go
func FilterValue[T any](f func(T) bool) cmp.Option {
return cmp.FilterValues(func(x, y T) bool {
return f(x) && f(y)
})
}
```
The current `cmp.FilterValues` is more powerful as you can make a filtering decision based on information from both values, but most usages of it want the logical AND of a single-argument predicate function. In fact, all of the usages of `cmp.FilterValues` in `cmpopts` would prefer to use a single-argument predicate function. Examples:
* https://github.com/google/go-cmp/blob/6faefd0594fae82639a62c23f0aed1451509dcc0/cmp/cmpopts/equate.go#L26-L31
* https://github.com/google/go-cmp/blob/6faefd0594fae82639a62c23f0aed1451509dcc0/cmp/cmpopts/equate.go#L60-L62
* https://github.com/google/go-cmp/blob/6faefd0594fae82639a62c23f0aed1451509dcc0/cmp/cmpopts/equate.go#L85-L87
* https://github.com/google/go-cmp/blob/6faefd0594fae82639a62c23f0aed1451509dcc0/cmp/cmpopts/equate.go#L104-L106
* https://github.com/google/go-cmp/blob/6faefd0594fae82639a62c23f0aed1451509dcc0/cmp/cmpopts/equate.go#L144-L148
Alternatively, we could modify `cmp.FilterValues` to also accept a function of the form `func(T) bool` where it effectively performs the AND of the predicate function applies to both arguments.
\cc @neild
Contributor guide
Assessment
This issue has not been assessed yet.