Skip to content

v0.1.0

Compare
Choose a tag to compare
@dsnet dsnet released this 09 Aug 16:48
· 144 commits to master since this release

Package cmp is intended to be a more powerful and safer alternative to
reflect.DeepEqual for comparing whether two values are semantically equal.

The primary features of cmp are:

  • When the default behavior of equality does not suit the needs of the test,
    custom equality functions can override the equality operation.
    For example, an equality function may report floats as equal so long as they
    are within some tolerance of each other.

  • Types that have an Equal method may use that method to determine equality.
    This allows package authors to determine the equality operation for the types
    that they define.

  • If no custom equality functions are used and no Equal method is defined,
    equality is determined by recursively comparing the primitive kinds on both
    values, much like reflect.DeepEqual. Unlike reflect.DeepEqual, unexported
    fields are not compared by default; they result in panics unless suppressed
    by using an Ignore option (see cmpopts.IgnoreUnexported) or explictly
    compared using the AllowUnexported option.

Package API:

func Equal(x, y interface{}, opts ...Option) bool { ... }
func Diff(x, y interface{}, opts ...Option) string { ... }

// Section: 1. Configuration options for Equal and Diff
type Option interface{ ... }
    func AllowUnexported(types ...interface{}) Option { ... }
type Options []Option

// Section: 1.1. Fundamental options to customize comparison of values
func Ignore() Option { ... }
func Comparer(f func(T, T) bool) Option { ... }
func Transformer(name string, f func(T) R) Option { ... }

// Section: 1.2. Filter options to control the scope of fundamental options
func FilterPath(f func(Path) bool, opt Option) Option { ... }
func FilterValues(f func(T, T) bool, opt Option) Option { ... }

// Section: 2. Path to a node in the value tree
type Path []PathStep
type PathStep interface{ ... }

// Section: 2.1. Individual steps that comprise a Path
type StructField interface{ ... }
type SliceIndex interface{ ... }
type MapIndex interface{ ... }
type Indirect interface{ ... }
type TypeAssertion interface{ ... }
type Transform interface{ ... }

Package cmpopts provides helper functions for creating cmp.Option values
to configure comparisons for common use-cases.

Package API:

// Section: Comparers
func EquateApprox(fraction, margin float64) cmp.Option { ... }
func EquateEmpty() cmp.Option { ... }
func EquateNaNs() cmp.Option { ... }

// Section: Ignorers
func IgnoreFields(typ interface{}, names ...string) cmp.Option { ... }
func IgnoreInterfaces(ifaces interface{}) cmp.Option { ... }
func IgnoreTypes(typs ...interface{}) cmp.Option { ... }
func IgnoreUnexported(typs ...interface{}) cmp.Option { ... }

// Section: Transformers
func SortMaps(less func(T, T) bool) cmp.Option { ... }
func SortSlices(less func(T, T) bool) cmp.Option { ... }