google / google/go-cmp

Add cmpopts.FilterValue

Open
#277 4 comments 1 reaction 0 assignees View on GitHub
enhancement
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.