Documentation
¶
Overview ¶
Package compare provide comparison utilities.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultComparator = NewComparator()
DefaultComparator is the default Comparator.
Functions ¶
This section is empty.
Types ¶
type Comparator ¶ added in v1.3.0
type Comparator struct {
// MaxDepth is the maximum depth of the comparison.
// If the value is reached, the comparison is stopped.
// Default: 0 (no limit).
MaxDepth int
// SliceMaxDifferences is the maximum number of different items for a slice.
// If the value is reached, the comparison is stopped for the current slice.
// It is also used for array.
// Set to 0 disables it.
// Default: 10.
SliceMaxDifferences int
// MapMaxDifferences is the maximum number of different items for a map.
// If the value is reached, the comparison is stopped for the current map.
// Set to 0 disables it.
// Default: 10.
MapMaxDifferences int
// Funcs is the list of custom comparison functions.
// Default: []byte, reflect.Value, .Equal().
Funcs []Func
}
Comparator compares 2 values.
It should be created with NewComparator.
func NewComparator ¶ added in v1.3.0
func NewComparator() *Comparator
NewComparator returns a new Comparator initialized with default values.
func (*Comparator) Compare ¶ added in v1.3.0
func (c *Comparator) Compare(v1, v2 any) Result
Compare compares 2 values.
type Difference ¶
type Difference struct {
Path Path `json:"path,omitempty"`
Message string `json:"message,omitempty"`
V1 string `json:"v1,omitempty"`
V2 string `json:"v2,omitempty"`
}
Difference represents a difference between 2 values.
func (Difference) Format ¶
func (d Difference) Format(s fmt.State, verb rune)
Format implements fmt.Formatter.
It only supports the 'v' verb. By default, it show the path and message. The '+' flag shows values V1 and V2.
type Func ¶
Func represents a comparison function. It is guaranteed that both values are valid and of the same type. If the returned value "stop" is true, the comparison will stop.
func NewBytesEqualFunc ¶ added in v1.3.0
func NewBytesEqualFunc() Func
NewBytesEqualFunc returns a Func that compares byte slices with bytes.Equal().
func NewMethodCmpFunc ¶ added in v1.3.0
func NewMethodCmpFunc() Func
NewMethodCmpFunc returns a Func that compares with the method .Cmp().
func NewMethodEqualFunc ¶ added in v1.3.0
func NewMethodEqualFunc() Func
NewMethodEqualFunc returns a Func that compares with the method .Equal().
func NewReflectValueFunc ¶ added in v1.3.0
func NewReflectValueFunc() Func
NewReflectValueFunc returns a Func that compares reflect.Value.
type Path ¶
type Path []PathElem
Path represents a field path, which is a list of PathElem.
Elements are stored in reverse order, the first element is the deepest. It helps to prepend elements to the path efficiently.
type PathElem ¶ added in v1.2.0
type PathElem struct {
Struct *string `json:"struct,omitempty"`
Map *string `json:"map,omitempty"`
Index *int `json:"index,omitempty"`
}
PathElem is a single element in a Path.
type Result ¶
type Result []Difference
Result is a list of Difference.
func Compare ¶
Compare compares 2 values with DefaultComparator.
Example ¶
type T struct {
String string
Int int
Map map[string]any
Slice []int
}
v1 := T{
String: "aaa",
Int: 1,
Map: map[string]any{
"a": "a",
"b": "b",
"c": "c",
},
Slice: []int{1, 2, 3},
}
v2 := T{
String: "bbb",
Int: 2,
Map: map[string]any{
"a": "z",
"b": 5,
"d": "c",
},
Slice: []int{1, 2, 4},
}
diff := Compare(v1, v2)
if len(diff) != 0 {
fmt.Printf("%+v", diff)
}
Output: .String: string not equal v1="aaa" v2="bbb" .Int: int not equal v1=1 v2=2 .Map[a]: string not equal v1="a" v2="z" .Map[b]: type not equal v1=string v2=int .Map[c]: map key not defined v1=true v2=false .Map[d]: map key not defined v1=false v2=true .Slice[2]: int not equal v1=3 v2=4
func (Result) Format ¶
Format implements fmt.Formatter.
See Difference.Format for supported verb and flag.