summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-cache/v2/compare.go
blob: 749d6c05fcaf40ce78923e83aab32b29c2c70a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cache

import (
	"reflect"
)

type Comparable interface {
	Equal(any) bool
}

// Compare returns whether 2 values are equal using the Comparable
// interface, or failing that falls back to use reflect.DeepEqual().
func Compare(i1, i2 any) bool {
	c1, ok1 := i1.(Comparable)
	if ok1 {
		return c1.Equal(i2)
	}
	c2, ok2 := i2.(Comparable)
	if ok2 {
		return c2.Equal(i1)
	}
	return reflect.DeepEqual(i1, i2)
}