google / go-cmp

Package for comparing Go values in tests
BSD 3-Clause "New" or "Revised" License
4.08k stars 209 forks source link

Default support for the `netip` package #339

Closed crawshaw closed 10 months ago

crawshaw commented 10 months ago

Now that netip is in the stdlib, it would be nice if go-cmp supported it by default. Today you need to add:

cmp.Comparer(func(x, y netip.Addr) bool { return x == y }),
cmp.Comparer(func(x, y netip.Prefix) bool { return x == y }),

to make cmp.Diff work with types that include netip types.

dsnet commented 10 months ago

At present, cmp has no special treatment of any type in the Go stdlib, including time.Time, which instigated the invention of this package. I think this is a reasonable design goal as special treatment of any particular type begs the question of where to draw the line. Even if we limit cmp to just the stdlib, that's a lot of types that would be linked in.

I see a few options:

  1. Have netip.Addr (and friends) implement an Equal method.
  2. Provide a cmpopts.EquateComparable helper that shallow compares the types passed to it (e.g., cmpopts.EquateComparable(netip.Addr{}, netip.Prefix{})).
  3. Propose a standard way for comparable types to self identify that their comparability is intentional (and guaranteed to not be lost in the future)
  4. Have cmp stop panicking on unexported fields, and just compare fields recursively as it would otherwise.

At minimum, we should probably do option 2 as it makes this easier.