Documentation
¶
Overview ¶
Package compare provide comparison utilities.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // 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. MaxSliceDifferences = 10 )
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 interface{}. If the returned value "stop" is true, the comparison will stop.
type IndexedPath ¶
IndexedPath is a Path for a slice/array index.
func (IndexedPath) PathString ¶
func (p IndexedPath) PathString() string
PathString implements PathItem
func (IndexedPath) String ¶
func (p IndexedPath) String() string
type Result ¶
type Result []Difference
Result is a list of Difference.
func Compare ¶
func Compare(v1, v2 interface{}) Result
Compare compares 2 values.
Example ¶
type T struct {
String string
Int int
Map map[string]interface{}
Slice []int
}
v1 := T{
String: "aaa",
Int: 1,
Map: map[string]interface{}{
"a": "a",
"b": "b",
"c": "c",
},
Slice: []int{1, 2, 3},
}
v2 := T{
String: "bbb",
Int: 2,
Map: map[string]interface{}{
"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 StructPath ¶
StructPath is a Path for a struct field.
func (StructPath) PathString ¶
func (p StructPath) PathString() string
PathString implements PathItem
func (StructPath) String ¶
func (p StructPath) String() string