github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/comparison/equal.go (about) 1 package comparison 2 3 import ( 4 // "fmt" 5 6 "github.com/mitchellh/hashstructure" 7 ) 8 9 // Equalitor describes an algorithm for equivalence between two data structures. 10 type Equalitor interface { 11 Equal(a, b interface{}) bool 12 } 13 14 // EqualFunc is a function that implements Equalitor. 15 type EqualFunc func(a, b interface{}) bool 16 17 // Equal allows an EqualFunc to implement Equalitor. 18 func (e EqualFunc) Equal(a, b interface{}) bool { 19 return e(a, b) 20 } 21 22 // NewHashEqualitor returns an EqualFunc that returns true if the hashes of the given 23 // arguments are equal, false otherwise. 24 // 25 // This function panics if an error is encountered generating a hash for either argument. 26 func NewHashEqualitor() EqualFunc { 27 return func(a, b interface{}) bool { 28 hashA, err := hashstructure.Hash(a, nil) 29 if err != nil { 30 panic(err.Error()) 31 } 32 33 hashB, err := hashstructure.Hash(b, nil) 34 if err != nil { 35 panic(err.Error()) 36 } 37 38 // fmt.Printf("hashA: %d, hashB: %d\n", hashA, hashB) 39 40 return hashA == hashB 41 } 42 }