Documentation
¶
Overview ¶
Package compare provide comparison utilities.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var MaxSliceDifferences = 10
MaxSliceDifferences is the maximum number of differences 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.
Functions ¶
func RegisterFunc ¶
func RegisterFunc(f Func)
RegisterFunc registers a Func. It allows to handle manually the comparison for certain values.
func RegisterMethodEqual ¶
func RegisterMethodEqual(name string)
RegisterMethodEqual registers an equal method. This methods must be callable as "v1.METHOD(v2) bool".
Types ¶
type Difference ¶
Difference represents a difference between 2 values.
type Func ¶
Func represents a comparison function. It is guaranteed that both values: are valid, of the same type, and can be converted to any. If the returned value "stop" is true, the comparison will stop.
type IndexedPathElem ¶ added in v1.2.0
type IndexedPathElem struct {
Index int
}
IndexedPathElem is a PathElem for a slice/array index.
func (IndexedPathElem) String ¶ added in v1.2.0
func (e IndexedPathElem) String() string
String returns the string representation.
type MapPathElem ¶ added in v1.2.0
type MapPathElem struct {
Key string
}
MapPathElem is a PathElem for a map key.
func (MapPathElem) String ¶ added in v1.2.0
func (e MapPathElem) String() string
String returns the string representation.
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 interface {
String() string
// contains filtered or unexported methods
}
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.
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
type StructPathElem ¶ added in v1.2.0
type StructPathElem struct {
Field string
}
StructPathElem is a PathElem for a struct field.
func (StructPathElem) String ¶ added in v1.2.0
func (e StructPathElem) String() string
String returns the string representation.