google / google/go-cmp

out-of-the-box support for atomic.Pointer

Open
#326 9 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
4.7k
Forks
243
PR merge metrics
No merged PRs in 30d

Description

Suppose a struct contains an atomic.Pointer instead of a normal pointer:
```
import "sync/atomic"

type A struct {
Ptr atomic.Pointer[int]
}
```

Using `cmp.Diff` for such struct instances fails because the `Pointer` type contains unexported fields:
```
panic: cannot handle unexported field at {*main.A}.Ptr.v:
"sync/atomic".Pointer[int]
consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
```

See https://go.dev/play/p/Jp91WpSKFRx for a full example.

It would be nice if `cmp.Diff` automatically did the same thing for `atomic.Pointer` as it does for normal pointers: compare the value that is being pointed to.

The argument for such special support is two-fold:
- atomic.Pointer is part of the Go standard library.
- Doing it via a Transformer for `atomic.Pointer` is impossible without copying that value, which correctly fails the "go vet" `copylocks` check. See https://github.com/kubernetes/kubernetes/pull/115777/files#diff-9b169e646cb953f34c0362a885d844daea86f5bef52012cb3af974f176bb281f and https://github.com/google/go-cmp/issues/310#issuecomment-1430338479.

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.