cmpopts

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2018 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package cmpopts provides common options for the cmp package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EquateApprox

func EquateApprox(fraction, margin float64) cmp.Option

EquateApprox returns a Comparer option that determines float32 or float64 values to be equal if they are within a relative fraction or absolute margin. This option is not used when either x or y is NaN or infinite.

The fraction determines that the difference of two values must be within the smaller fraction of the two values, while the margin determines that the two values must be within some absolute margin. To express only a fraction or only a margin, use 0 for the other parameter. The fraction and margin must be non-negative.

The mathematical expression used is equivalent to:

|x-y| ≤ max(fraction*min(|x|, |y|), margin)

EquateApprox can be used in conjunction with EquateNaNs.

func EquateEmpty

func EquateEmpty() cmp.Option

EquateEmpty returns a Comparer option that determines all maps and slices with a length of zero to be equal, regardless of whether they are nil.

EquateEmpty can be used in conjunction with SortSlices and SortMaps.

func EquateNaNs

func EquateNaNs() cmp.Option

EquateNaNs returns a Comparer option that determines float32 and float64 NaN values to be equal.

EquateNaNs can be used in conjunction with EquateApprox.

func IgnoreFields

func IgnoreFields(typ interface{}, names ...string) cmp.Option

IgnoreFields returns an Option that ignores exported fields of the given names on a single struct type. The struct type is specified by passing in a value of that type.

The name may be a dot-delimited string (e.g., "Foo.Bar") to ignore a specific sub-field that is embedded or nested within the parent struct.

This does not handle unexported fields; use IgnoreUnexported instead.

func IgnoreInterfaces

func IgnoreInterfaces(ifaces interface{}) cmp.Option

IgnoreInterfaces returns an Option that ignores all values or references of values assignable to certain interface types. These interfaces are specified by passing in an anonymous struct with the interface types embedded in it. For example, to ignore sync.Locker, pass in struct{sync.Locker}{}.

func IgnoreTypes

func IgnoreTypes(typs ...interface{}) cmp.Option

IgnoreTypes returns an Option that ignores all values assignable to certain types, which are specified by passing in a value of each type.

func IgnoreUnexported

func IgnoreUnexported(typs ...interface{}) cmp.Option

IgnoreUnexported returns an Option that only ignores the immediate unexported fields of a struct, including anonymous fields of unexported types. In particular, unexported fields within the struct's exported fields of struct types, including anonymous fields, will not be ignored unless the type of the field itself is also passed to IgnoreUnexported.

func SortMaps

func SortMaps(less interface{}) cmp.Option

SortMaps returns a Transformer option that flattens map[K]V types to be a sorted []struct{K, V}. The less function must be of the form "func(T, T) bool" which is used to sort any map with key K that is assignable to T.

Flattening the map into a slice has the property that cmp.Equal is able to use Comparers on K or the K.Equal method if it exists.

The less function must be:

  • Deterministic: less(x, y) == less(x, y)
  • Irreflexive: !less(x, x)
  • Transitive: if !less(x, y) and !less(y, z), then !less(x, z)
  • Total: if x != y, then either less(x, y) or less(y, x)

SortMaps can be used in conjunction with EquateEmpty.

func SortSlices

func SortSlices(less interface{}) cmp.Option

SortSlices returns a Transformer option that sorts all []V. The less function must be of the form "func(T, T) bool" which is used to sort any slice with element type V that is assignable to T.

The less function must be:

  • Deterministic: less(x, y) == less(x, y)
  • Irreflexive: !less(x, x)
  • Transitive: if !less(x, y) and !less(y, z), then !less(x, z)

The less function does not have to be "total". That is, if !less(x, y) and !less(y, x) for two elements x and y, their relative order is maintained.

SortSlices can be used in conjunction with EquateEmpty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL