-
Notifications
You must be signed in to change notification settings - Fork 2
/
valtypes.go
110 lines (103 loc) · 3.22 KB
/
valtypes.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package watchable
import (
"fmt"
"reflect"
"google.golang.org/protobuf/proto"
)
type (
_DeepCopier[T any] interface {
DeepCopy() T
}
// DeepCopier[T] describes a type 'T' that has a 'DeepCopy() T' method; it is useful for
// asserting that your DeepCopy method will be accepted by this package's DeepEqual
// function.
DeepCopier[T _DeepCopier[T]] _DeepCopier[T]
)
type (
_Comparable[T any] interface {
Equal(T) bool
}
// Comparable[T] describes a type 'T' that has an 'Equal(T) bool' method; it is useful for
// asserting that your Equal method will be accepted by this package's DeepEqual function.
//
// The name of this interface mimics the built-in 'comparable' identifier, deviating from the
// usual Go naming convention for single-method interfaces ('Equaler').
Comparable[T _Comparable[T]] _Comparable[T]
)
func hasNoPointers(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Uintptr,
reflect.Float32, reflect.Float64,
reflect.Complex64, reflect.Complex128,
reflect.String:
return true
case reflect.Array:
return hasNoPointers(typ.Elem())
case reflect.Struct:
for i := 0; i < typ.NumField(); i++ {
if !hasNoPointers(typ.Field(i).Type) {
return false
}
}
return true
default:
return false
}
}
// DeepCopy returns a deep copy of a value.
//
// In order of precedence:
//
// - If the type 'T' has a 'DeepCopy' method (implements the 'DeepCopier[T]' interface), then that
// method is used.
//
// - If the type 'T' implements google.golang.org/protobuf/proto.Message, then
// google.golang.org/protobuf/proto.Clone is used.
//
// - If the value is a primitive ('bool', any of the 'int' types, either of the 'float' types,
// either of the 'complex' types, or 'string'), or an array (not slice) or struct that only
// contains primitives, then it is naively copied by value.
//
// - Otherwise, DeepCopy panics.
func DeepCopy[T any](val T) T {
switch tval := any(val).(type) {
case _DeepCopier[T]:
return tval.DeepCopy()
case proto.Message:
return proto.Clone(tval).(T)
default:
if hasNoPointers(reflect.TypeOf(val)) {
return val
}
panic(fmt.Errorf("watchable.DeepCopy: type is not copiable: %T", val))
}
}
// DeepEqual returns whether two values are deeply equal.
//
// In order of precedence:
//
// - If the type 'T' has an 'Equal' method (implements the 'Comparable[T]' interface), then that
// method is used.
//
// - If the types of 'a' and 'b' both implement google.golang.org/protobuf/proto.Message, then
// google.golang.org/protobuf/proto.Equal is used. (This is slightly different than saying "if
// 'T' implements proto.Message" because it could be that 'T' is an interface type, 'a' and 'b'
// have differing concrete types, and only one of them implements proto.Message).
//
// - Otherwise, reflect.DeepEqual is used.
func DeepEqual[T any](a, b T) bool {
switch ta := any(a).(type) {
case _Comparable[T]:
return ta.Equal(b)
case proto.Message:
if tb, ok := any(b).(proto.Message); ok {
return proto.Equal(ta, tb)
}
return reflect.DeepEqual(a, b)
default:
return reflect.DeepEqual(a, b)
}
}