Repository navigation
Expand file tree
/
Copy pathequal.go
More file actions
159 lines (137 loc) · 3.7 KB
/
Copy pathequal.go
File metadata and controls
159 lines (137 loc) · 3.7 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package structdiff
import (
"maps"
"reflect"
"slices"
"github.com/databricks/cli/libs/structs/structaccess"
)
// IsEqual compares two Go structs and returns true if they are equal.
// It uses the same comparison logic as GetStructDiff but is more efficient
// as it short-circuits on the first difference found.
// Respects ForceSendFields if present.
// Types of a and b must match exactly, otherwise returns false.
// Note, reflect.DeepEqual() does not work for SDK structs, because ForceSendFields can contain different sets for the same value.
func IsEqual(a, b any) bool {
v1 := reflect.ValueOf(a)
v2 := reflect.ValueOf(b)
if !v1.IsValid() && !v2.IsValid() {
return true
}
if !v1.IsValid() || !v2.IsValid() {
return false
}
if v1.Type() != v2.Type() {
return false
}
return equalValues(v1, v2)
}
// equalValues returns true if v1 and v2 are equal.
func equalValues(v1, v2 reflect.Value) bool {
if !v1.IsValid() {
return !v2.IsValid()
} else if !v2.IsValid() {
return false
}
v1Type := v1.Type()
if v1Type != v2.Type() {
return false
}
if IsOpaqueStruct(v1Type) {
// A marshaling failure is reported as "not equal" so the change surfaces
// rather than being silently dropped.
equal, err := equalJSON(v1, v2)
return err == nil && equal
}
kind := v1.Kind()
// Perform nil checks for nilable types.
switch kind {
case reflect.Pointer, reflect.Map, reflect.Slice, reflect.Interface, reflect.Chan, reflect.Func:
v1Nil := v1.IsNil()
v2Nil := v2.IsNil()
if v1Nil && v2Nil {
return true
}
if v1Nil || v2Nil {
return false
}
default:
// Not a nilable type.
// Proceed with direct comparison below.
}
switch kind {
case reflect.Pointer:
return equalValues(v1.Elem(), v2.Elem())
case reflect.Struct:
return equalStruct(v1, v2)
case reflect.Slice, reflect.Array:
if v1.Len() != v2.Len() {
return false
}
for i := range v1.Len() {
if !equalValues(v1.Index(i), v2.Index(i)) {
return false
}
}
case reflect.Map:
if v1Type.Key().Kind() == reflect.String {
return equalMapStringKey(v1, v2)
}
return reflect.DeepEqual(v1.Interface(), v2.Interface())
default:
return equalLeaf(v1, v2)
}
return true
}
func equalStruct(s1, s2 reflect.Value) bool {
info := structaccess.GetStructInfo(s1.Type())
forced1 := info.ForceSendFields(s1)
forced2 := info.ForceSendFields(s2)
for _, f := range info.Fields {
v1Field := s1.Field(f.Index)
v2Field := s2.Field(f.Index)
// Continue traversing embedded structs. A tagged anonymous field is a named field to
// encoding/json, so it goes through the path below.
if f.Flattened {
if !equalValues(v1Field, v2Field) {
return false
}
continue
}
if f.OmitEmpty {
if v1Field.IsZero() && !slices.Contains(forced1, f.StructField.Name) {
v1Field = reflect.ValueOf(nil)
}
if v2Field.IsZero() && !slices.Contains(forced2, f.StructField.Name) {
v2Field = reflect.ValueOf(nil)
}
}
if !equalValues(v1Field, v2Field) {
return false
}
}
return true
}
func equalMapStringKey(m1, m2 reflect.Value) bool {
// map[string]string is the common case; reflect would copy every key and value to the heap.
if s1, ok := reflect.TypeAssert[map[string]string](m1); ok {
s2, _ := reflect.TypeAssert[map[string]string](m2)
return maps.Equal(s1, s2)
}
keySet := map[string]reflect.Value{}
for _, k := range m1.MapKeys() {
// Key kind is always String at this point; Value.String() also handles a
// named string key type, where a .(string) assertion panics.
keySet[k.String()] = k
}
for _, k := range m2.MapKeys() {
keySet[k.String()] = k
}
for _, k := range keySet {
v1 := m1.MapIndex(k)
v2 := m2.MapIndex(k)
if !equalValues(v1, v2) {
return false
}
}
return true
}