/
type_helpers.go
66 lines (56 loc) · 1.05 KB
/
type_helpers.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
56
57
58
59
60
61
62
63
64
65
66
package client
import "reflect"
// Returns a pointer to the given value
func ToPtr[T any](v T) *T {
return &v
}
// Determines if two slices of the same type are equivalent
// as opposed to equal
//
// For example: []string{"bob", "alice"} is equivalent but not equal to []string{"alice", "bob"}
func Equivalent[T any](a, b []T) bool {
if len(a) != len(b) {
return false
}
for _, ours := range a {
found := false
for _, theirs := range b {
if reflect.DeepEqual(ours, theirs) {
found = true
break
}
}
if !found {
return false
}
}
// do it the other way around to make sure we're not missing a match
for _, theirs := range b {
found := false
for _, ours := range a {
if reflect.DeepEqual(theirs, ours) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func IsZero[T comparable](v T) bool {
return v == *new(T)
}
func PtrValueOrDefault[T any](v *T, d T) T {
if v != nil {
return *v
}
return d
}
func ValueOrDefault[T comparable](v, d T) T {
if !IsZero(v) {
return v
}
return d
}