-
Notifications
You must be signed in to change notification settings - Fork 53
/
comparator.go
55 lines (46 loc) · 1.23 KB
/
comparator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package v1
import (
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
type Comparator[T any] interface {
Equal(other T) bool
}
func (c *TokenCapability) Equal(other *TokenCapability) bool {
return equalNonzeroOperator(c.GetType(), other.GetType()) &&
equalNonzeroFunc(c.GetReference(), other.GetReference())
}
func (c *ClusterCapability) Equal(other *ClusterCapability) bool {
return equalNonzeroOperator(c.GetName(), other.GetName())
}
func (r *Reference) Equal(other *Reference) bool {
if r == nil || other == nil {
return r == other
}
return equalNonzeroOperator(r.Id, other.Id)
}
func (r *Health) Equal(other *Health) bool {
if r == nil || other == nil {
return r == other
}
return r.GetReady() == other.GetReady() &&
slices.Equal(r.GetConditions(), other.GetConditions()) &&
maps.Equal(r.GetAnnotations(), other.GetAnnotations())
}
func (r *Health) NewerThan(other *Health) bool {
return r.GetTimestamp().AsTime().After(other.GetTimestamp().AsTime())
}
func equalNonzeroFunc[T Comparator[T]](a, b T) bool {
var zero T
if a.Equal(zero) || b.Equal(zero) {
return false
}
return a.Equal(b)
}
func equalNonzeroOperator[T comparable](a, b T) bool {
var zero T
if a == zero || b == zero {
return false
}
return a == b
}