Skip to content

Commit f0f5ddc

Browse files
committed
feat(equalitor): add equalitor interface and hash implementation
1 parent 145b8fd commit f0f5ddc

File tree

2 files changed

+413
-0
lines changed

2 files changed

+413
-0
lines changed

pkg/lib/comparison/equal.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)