Documentation
¶
Overview ¶
Package compare provides customizable functionality for comparing values.
Whether what you want to compare is something like a struct or map, or a string representing a JSON object, the first step is to specify how values of basic types like int, float64 and string should be compared. You can accomplish that by implementing the BasicEqualer interface yourself or by using the existing TolerantBasicEqualer implementation.
Once you have a BasicEqualer, you can use it to create a DeepEqualer (to determine whether two values are deeply equal) or a JSONDiffer (to determine if two strings represent the same JSON value and to inspect the differences, if any).
Index ¶
- type BasicEqualer
- type DeepEqualer
- type JSONDiff
- type JSONDiffer
- type StringTransformer
- type SubstringDeleter
- type TolerantBasicEqualer
- func (e TolerantBasicEqualer) Bool(a, b bool) bool
- func (e TolerantBasicEqualer) Complex128(a, b complex128) bool
- func (e TolerantBasicEqualer) Float64(a, b float64) bool
- func (e TolerantBasicEqualer) Int64(a, b int64) bool
- func (e TolerantBasicEqualer) String(a, b string) bool
- func (e TolerantBasicEqualer) Uint64(a, b uint64) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BasicEqualer ¶
type BasicEqualer interface {
// Bool determines if two Boolean values are equal.
Bool(bool, bool) bool
// Int64 determines if two integer values are equal.
Int64(int64, int64) bool
// Uint64 determines if two unsigned integer values are equal.
Uint64(uint64, uint64) bool
// Float64 determines if two floating-point values are equal.
Float64(float64, float64) bool
// Complex128 determines if two complex numbers are equal.
Complex128(complex128, complex128) bool
// String determines if two strings are equal.
String(string, string) bool
}
BasicEqualer provides functions for determining if values of basic types are equal.
type DeepEqualer ¶
type DeepEqualer struct {
// BasicEqualer specifies how values of basic types should be compared.
BasicEqualer
}
DeepEqualer provides functionality for the deep comparison of values.
func (DeepEqualer) Equal ¶
func (e DeepEqualer) Equal(a, b interface{}) (same bool, err error)
Equal determines if two values contain the same information.
The implementation closely follows the implementation of reflect.DeepEqual(), but with certain limitations:
1. Unlike reflect.DeepEqual(), we assume that there are no loops and don't safeguard against them at all.
2. For types that we don't support directly (i.e. channels, functions and unsafe pointers), we try to fall back to reflect.DeepEqual(). Unfortunately, this approach doesn't work for unexported struct fields, because we can't access the underlying values. In such cases, we return an error.
Example (Float) ¶
de := DeepEqualer{TolerantBasicEqualer{Float64Tolerance: 0.1}}
same, err := de.Equal([]float64{1.6, 3.8}, []float64{1.544, 3.89})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(same)
Output: true
Example (String) ¶
de := DeepEqualer{TolerantBasicEqualer{
// ignore everything after first space
StringTransformer: SubstringDeleter{Regexp: regexp.MustCompile(" .*$")},
}}
same, err := de.Equal(
map[string]string{"greeting": "Hello Alice!"},
map[string]string{"greeting": "Hello Bob!"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(same)
Output: true
type JSONDiff ¶
type JSONDiff struct {
// contains filtered or unexported fields
}
JSONDiff represents the differences between two JSON values.
func (*JSONDiff) Deltas ¶
func (d *JSONDiff) Deltas() []gojsondiff.Delta
Deltas returns Deltas that describe individual differences between two JSON values.
type JSONDiffer ¶
type JSONDiffer struct {
// BasicEqualer specifies how values of basic types should be compared.
BasicEqualer
}
JSONDiffer compares JSON strings.
func (JSONDiffer) Compare ¶
func (jd JSONDiffer) Compare(left, right []byte) (*JSONDiff, error)
Compare returns the differences between two JSON strings. Returns an error iff the strings don't adhere to the JSON syntax.
Example ¶
jd := JSONDiffer{TolerantBasicEqualer{Float64Tolerance: 0.1}}
d, err := jd.Compare(
[]byte(`{"x": 1.6, "y": [3.8, "hello"]}`),
[]byte(`{"x": 1.57, "y": [3.6, "hello"], "z": 0}`))
if err != nil {
fmt.Println(err)
return
}
diff, err := d.Format(false) // no colouring
if err != nil {
fmt.Println(err)
return
}
fmt.Println(diff)
Output: { "x": 1.6, "y": [ - 0: 3.8, + 0: 3.6, 1: "hello" ] + "z": 0 }
type StringTransformer ¶
type StringTransformer interface {
// Transform transforms a string into another string.
Transform(string) string
}
StringTransformer provides a function for transforming strings.
type SubstringDeleter ¶
type SubstringDeleter struct {
// Regexp specifies which substrings should be deleted.
Regexp *regexp.Regexp
}
SubstringDeleter provides functionality for deleting substrings matching a regular expression.
func (SubstringDeleter) Transform ¶
func (sd SubstringDeleter) Transform(s string) string
Transform removes all substrings matching a regular expression.
type TolerantBasicEqualer ¶
type TolerantBasicEqualer struct {
// Float64Tolerance specifies how much two floating-point values may differ
// while still being considered equal.
Float64Tolerance float64
// StringTransformer specifies how string values should be transformed
// before comparing them.
StringTransformer StringTransformer
// TimeLayout specifies the layout of time values.
TimeLayout string
// TimeTolerance specifies how much two times may differ while still being
// considered equal.
TimeTolerance time.Duration
}
TolerantBasicEqualer is an example implementation of the Equaler interface. Rather than comparing values exactly, it allows some leeway.
func (TolerantBasicEqualer) Bool ¶
func (e TolerantBasicEqualer) Bool(a, b bool) bool
Bool compares two Boolean values exactly.
func (TolerantBasicEqualer) Complex128 ¶
func (e TolerantBasicEqualer) Complex128(a, b complex128) bool
Complex128 compares two complex numbers exactly.
func (TolerantBasicEqualer) Float64 ¶
func (e TolerantBasicEqualer) Float64(a, b float64) bool
Float64 compares two floating-point numbers within a tolerance. For example, if the tolerance is 0.5, then 4.07 and 4.57 are considered equal, but 4.07 and 4.571 are not.
func (TolerantBasicEqualer) Int64 ¶
func (e TolerantBasicEqualer) Int64(a, b int64) bool
Int64 compares two integer values exactly.
func (TolerantBasicEqualer) String ¶
func (e TolerantBasicEqualer) String(a, b string) bool
String compares two string values.
If a tolerance for time values is specified, and if both values represent valid times according to the specified layout (time.RFC3339 by default), then this function checks if the times are within the specified tolerance. For example, if the tolerance is a second, then "2018-03-30T14:36:09.778" and "2018-03-30T14:36:10.778" are considered equal, but "2018-03-30T14:36:09.778" and "2018-03-30T14:36:10.779" are not.
Otherwise, if a StringTransformer is specified, it will be used to transform both strings before comparing them.
If neither of the above applies, the strings are compared exactly.
func (TolerantBasicEqualer) Uint64 ¶
func (e TolerantBasicEqualer) Uint64(a, b uint64) bool
Uint64 compares two integer values exactly.