google / google/go-cmp

Add universal Transformer and Comparers

Open
#182 0 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

Perhaps the API for `Transformer` and `Comparer` should be expanded to permit functions of following signatures:
```go
Comparer(func(cmp.Path) (eq, ok bool) { ... })
Transformer(func(cmp.Path) (vx, vy reflect.Value, ok bool) { ... })
```
For `Comparer`, the `eq` output reports whether the comparison determined the values to be true. For `Transformer`, the `vx` and `vy` output are the transformed outputs and must both be valid and of the same type. In both cases, the `ok` bool reports whether the option is applicable.

These two signatures are more complex, but provide a more efficient and natural way for libraries to implement certain options. While a combination of `Filters`+`Comparer` or `Filters`+`Transformer` can accomplish mostly the same thing, there is oftentimes computed information in the filter that ought to be reused in the comparison or transformation. Combining these together makes implementation both easier and faster.

In the early days of `cmp`, the signatures above were the original design, but was deemed too complex. While, I think we made the right choice of breaking apart the complex signature into the simpler ones we have today, I'm running into cases where it would be easier to implement with the more complex signature. I imagine the use of this will be limited to libraries like [`protocmp`](https://pkg.go.dev/google.golang.org/protobuf/testing/protocmp) that are then used by many people.

Most usages of comparers and transformers will just use the simpler signatures.

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.