| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package reflect is a fork of go's standard library reflection package, which |
| 6 |
// allows for deep equal with equality functions defined. |
| 7 |
package reflect |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
"strings" |
| 13 |
) |
| 14 |
|
| 15 |
// Equalities is a map from type to a function comparing two values of |
| 16 |
// that type. |
| 17 |
type Equalities map[reflect.Type]reflect.Value |
| 18 |
|
| 19 |
// EqualitiesOrDie adds the given funcs and panics on any error. |
| 20 |
func EqualitiesOrDie(funcs ...any) Equalities { |
| 21 |
e := Equalities{} |
| 22 |
if err := e.AddFuncs(funcs...); err != nil { |
| 23 |
panic(err) |
| 24 |
} |
| 25 |
return e |
| 26 |
} |
| 27 |
|
| 28 |
// AddFuncs is a shortcut for multiple calls to AddFunc. |
| 29 |
func (e Equalities) AddFuncs(funcs ...any) error { |
| 30 |
for _, f := range funcs { |
| 31 |
if err := e.AddFunc(f); err != nil { |
| 32 |
return err |
| 33 |
} |
| 34 |
} |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
// AddFunc uses func as an equality function: it must take |
| 39 |
// two parameters of the same type, and return a boolean. |
| 40 |
func (e Equalities) AddFunc(eqFunc any) error { |
| 41 |
fv := reflect.ValueOf(eqFunc) |
| 42 |
ft := fv.Type() |
| 43 |
if ft.Kind() != reflect.Func { |
| 44 |
return fmt.Errorf("expected func, got: %v", ft) |
| 45 |
} |
| 46 |
if ft.NumIn() != 2 { |
| 47 |
return fmt.Errorf("expected two 'in' params, got: %v", ft) |
| 48 |
} |
| 49 |
if ft.NumOut() != 1 { |
| 50 |
return fmt.Errorf("expected one 'out' param, got: %v", ft) |
| 51 |
} |
| 52 |
if ft.In(0) != ft.In(1) { |
| 53 |
return fmt.Errorf("expected arg 1 and 2 to have same type, but got %v", ft) |
| 54 |
} |
| 55 |
boolType := reflect.TypeFor[bool]() |
| 56 |
if ft.Out(0) != boolType { |
| 57 |
return fmt.Errorf("expected bool return, got: %v", ft) |
| 58 |
} |
| 59 |
e[ft.In(0)] = fv |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
// Below here is forked from go's reflect/deepequal.go |
| 64 |
|
| 65 |
// During deepValueEqual, must keep track of checks that are |
| 66 |
// in progress. The comparison algorithm assumes that all |
| 67 |
// checks in progress are true when it reencounters them. |
| 68 |
// Visited comparisons are stored in a map indexed by visit. |
| 69 |
type visit struct { |
| 70 |
a1 uintptr |
| 71 |
a2 uintptr |
| 72 |
typ reflect.Type |
| 73 |
} |
| 74 |
|
| 75 |
// unexportedTypePanic is thrown when you use this DeepEqual on something that has an |
| 76 |
// unexported type. It indicates a programmer error, so should not occur at runtime, |
| 77 |
// which is why it's not public and thus impossible to catch. |
| 78 |
type unexportedTypePanic []reflect.Type |
| 79 |
|
| 80 |
func (u unexportedTypePanic) Error() string { return u.String() } |
| 81 |
func (u unexportedTypePanic) String() string { |
| 82 |
strs := make([]string, len(u)) |
| 83 |
for i, t := range u { |
| 84 |
strs[i] = fmt.Sprintf("%v", t) |
| 85 |
} |
| 86 |
return "an unexported field was encountered, nested like this: " + strings.Join(strs, " -> ") |
| 87 |
} |
| 88 |
|
| 89 |
func makeUsefulPanic(v reflect.Value) { |
| 90 |
if x := recover(); x != nil { |
| 91 |
if u, ok := x.(unexportedTypePanic); ok { |
| 92 |
u = append(unexportedTypePanic{v.Type()}, u...) |
| 93 |
x = u |
| 94 |
} |
| 95 |
panic(x) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// deepValueEqual tests for deep equality using reflected types. The map argument tracks |
| 100 |
// comparisons that have already been seen, which allows short circuiting on |
| 101 |
// recursive types. |
| 102 |
func (e Equalities) deepValueEqual(v1, v2 reflect.Value, visited map[visit]bool, depth int) bool { |
| 103 |
defer makeUsefulPanic(v1) |
| 104 |
|
| 105 |
if !v1.IsValid() || !v2.IsValid() { |
| 106 |
return v1.IsValid() == v2.IsValid() |
| 107 |
} |
| 108 |
if v1.Type() != v2.Type() { |
| 109 |
return false |
| 110 |
} |
| 111 |
if fv, ok := e[v1.Type()]; ok { |
| 112 |
return fv.Call([]reflect.Value{v1, v2})[0].Bool() |
| 113 |
} |
| 114 |
if v1.CanAddr() { |
| 115 |
if fv, ok := e[v1.Addr().Type()]; ok { |
| 116 |
return fv.Call([]reflect.Value{v1.Addr(), v2.Addr()})[0].Bool() |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
hard := func(k reflect.Kind) bool { |
| 121 |
switch k { |
| 122 |
case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct: |
| 123 |
return true |
| 124 |
} |
| 125 |
return false |
| 126 |
} |
| 127 |
|
| 128 |
if v1.CanAddr() && v2.CanAddr() && hard(v1.Kind()) { |
| 129 |
addr1 := v1.UnsafeAddr() |
| 130 |
addr2 := v2.UnsafeAddr() |
| 131 |
if addr1 > addr2 { |
| 132 |
// Canonicalize order to reduce number of entries in visited. |
| 133 |
addr1, addr2 = addr2, addr1 |
| 134 |
} |
| 135 |
|
| 136 |
// Short circuit if references are identical ... |
| 137 |
if addr1 == addr2 { |
| 138 |
return true |
| 139 |
} |
| 140 |
|
| 141 |
// ... or already seen |
| 142 |
typ := v1.Type() |
| 143 |
v := visit{addr1, addr2, typ} |
| 144 |
if visited[v] { |
| 145 |
return true |
| 146 |
} |
| 147 |
|
| 148 |
// Remember for later. |
| 149 |
visited[v] = true |
| 150 |
} |
| 151 |
|
| 152 |
switch v1.Kind() { |
| 153 |
case reflect.Array: |
| 154 |
// We don't need to check length here because length is part of |
| 155 |
// an array's type, which has already been filtered for. |
| 156 |
for i := 0; i < v1.Len(); i++ { |
| 157 |
if !e.deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) { |
| 158 |
return false |
| 159 |
} |
| 160 |
} |
| 161 |
return true |
| 162 |
case reflect.Slice: |
| 163 |
if (v1.IsNil() || v1.Len() == 0) != (v2.IsNil() || v2.Len() == 0) { |
| 164 |
return false |
| 165 |
} |
| 166 |
if v1.IsNil() || v1.Len() == 0 { |
| 167 |
return true |
| 168 |
} |
| 169 |
if v1.Len() != v2.Len() { |
| 170 |
return false |
| 171 |
} |
| 172 |
if v1.Pointer() == v2.Pointer() { |
| 173 |
return true |
| 174 |
} |
| 175 |
for i := 0; i < v1.Len(); i++ { |
| 176 |
if !e.deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) { |
| 177 |
return false |
| 178 |
} |
| 179 |
} |
| 180 |
return true |
| 181 |
case reflect.Interface: |
| 182 |
if v1.IsNil() || v2.IsNil() { |
| 183 |
return v1.IsNil() == v2.IsNil() |
| 184 |
} |
| 185 |
return e.deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1) |
| 186 |
case reflect.Pointer: |
| 187 |
return e.deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1) |
| 188 |
case reflect.Struct: |
| 189 |
for i, n := 0, v1.NumField(); i < n; i++ { |
| 190 |
if !e.deepValueEqual(v1.Field(i), v2.Field(i), visited, depth+1) { |
| 191 |
return false |
| 192 |
} |
| 193 |
} |
| 194 |
return true |
| 195 |
case reflect.Map: |
| 196 |
if (v1.IsNil() || v1.Len() == 0) != (v2.IsNil() || v2.Len() == 0) { |
| 197 |
return false |
| 198 |
} |
| 199 |
if v1.IsNil() || v1.Len() == 0 { |
| 200 |
return true |
| 201 |
} |
| 202 |
if v1.Len() != v2.Len() { |
| 203 |
return false |
| 204 |
} |
| 205 |
if v1.Pointer() == v2.Pointer() { |
| 206 |
return true |
| 207 |
} |
| 208 |
for _, k := range v1.MapKeys() { |
| 209 |
if !e.deepValueEqual(v1.MapIndex(k), v2.MapIndex(k), visited, depth+1) { |
| 210 |
return false |
| 211 |
} |
| 212 |
} |
| 213 |
return true |
| 214 |
case reflect.Func: |
| 215 |
if v1.IsNil() && v2.IsNil() { |
| 216 |
return true |
| 217 |
} |
| 218 |
// Can't do better than this: |
| 219 |
return false |
| 220 |
default: |
| 221 |
// Normal equality suffices |
| 222 |
if !v1.CanInterface() || !v2.CanInterface() { |
| 223 |
panic(unexportedTypePanic{}) |
| 224 |
} |
| 225 |
return v1.Interface() == v2.Interface() |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// DeepEqual is like reflect.DeepEqual, but focused on semantic equality |
| 230 |
// instead of memory equality. |
| 231 |
// |
| 232 |
// It will use e's equality functions if it finds types that match. |
| 233 |
// |
| 234 |
// An empty slice *is* equal to a nil slice for our purposes; same for maps. |
| 235 |
// |
| 236 |
// Unexported field members cannot be compared and will cause an informative panic; you must add an Equality |
| 237 |
// function for these types. |
| 238 |
func (e Equalities) DeepEqual(a1, a2 any) bool { |
| 239 |
if a1 == nil || a2 == nil { |
| 240 |
return a1 == a2 |
| 241 |
} |
| 242 |
v1 := reflect.ValueOf(a1) |
| 243 |
v2 := reflect.ValueOf(a2) |
| 244 |
if v1.Type() != v2.Type() { |
| 245 |
return false |
| 246 |
} |
| 247 |
return e.deepValueEqual(v1, v2, make(map[visit]bool), 0) |
| 248 |
} |
| 249 |
|
| 250 |
func (e Equalities) deepValueDerive(v1, v2 reflect.Value, visited map[visit]bool, depth int) bool { |
| 251 |
defer makeUsefulPanic(v1) |
| 252 |
|
| 253 |
if !v1.IsValid() || !v2.IsValid() { |
| 254 |
return v1.IsValid() == v2.IsValid() |
| 255 |
} |
| 256 |
if v1.Type() != v2.Type() { |
| 257 |
return false |
| 258 |
} |
| 259 |
if fv, ok := e[v1.Type()]; ok { |
| 260 |
return fv.Call([]reflect.Value{v1, v2})[0].Bool() |
| 261 |
} |
| 262 |
if v1.CanAddr() { |
| 263 |
if fv, ok := e[v1.Addr().Type()]; ok { |
| 264 |
return fv.Call([]reflect.Value{v1.Addr(), v2.Addr()})[0].Bool() |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
hard := func(k reflect.Kind) bool { |
| 269 |
switch k { |
| 270 |
case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct: |
| 271 |
return true |
| 272 |
} |
| 273 |
return false |
| 274 |
} |
| 275 |
|
| 276 |
if v1.CanAddr() && v2.CanAddr() && hard(v1.Kind()) { |
| 277 |
addr1 := v1.UnsafeAddr() |
| 278 |
addr2 := v2.UnsafeAddr() |
| 279 |
if addr1 > addr2 { |
| 280 |
// Canonicalize order to reduce number of entries in visited. |
| 281 |
addr1, addr2 = addr2, addr1 |
| 282 |
} |
| 283 |
|
| 284 |
// Short circuit if references are identical ... |
| 285 |
if addr1 == addr2 { |
| 286 |
return true |
| 287 |
} |
| 288 |
|
| 289 |
// ... or already seen |
| 290 |
typ := v1.Type() |
| 291 |
v := visit{addr1, addr2, typ} |
| 292 |
if visited[v] { |
| 293 |
return true |
| 294 |
} |
| 295 |
|
| 296 |
// Remember for later. |
| 297 |
visited[v] = true |
| 298 |
} |
| 299 |
|
| 300 |
switch v1.Kind() { |
| 301 |
case reflect.Array: |
| 302 |
// We don't need to check length here because length is part of |
| 303 |
// an array's type, which has already been filtered for. |
| 304 |
for i := 0; i < v1.Len(); i++ { |
| 305 |
if !e.deepValueDerive(v1.Index(i), v2.Index(i), visited, depth+1) { |
| 306 |
return false |
| 307 |
} |
| 308 |
} |
| 309 |
return true |
| 310 |
case reflect.Slice: |
| 311 |
if v1.IsNil() || v1.Len() == 0 { |
| 312 |
return true |
| 313 |
} |
| 314 |
if v1.Len() > v2.Len() { |
| 315 |
return false |
| 316 |
} |
| 317 |
if v1.Pointer() == v2.Pointer() { |
| 318 |
return true |
| 319 |
} |
| 320 |
for i := 0; i < v1.Len(); i++ { |
| 321 |
if !e.deepValueDerive(v1.Index(i), v2.Index(i), visited, depth+1) { |
| 322 |
return false |
| 323 |
} |
| 324 |
} |
| 325 |
return true |
| 326 |
case reflect.String: |
| 327 |
if v1.Len() == 0 { |
| 328 |
return true |
| 329 |
} |
| 330 |
if v1.Len() > v2.Len() { |
| 331 |
return false |
| 332 |
} |
| 333 |
return v1.String() == v2.String() |
| 334 |
case reflect.Interface: |
| 335 |
if v1.IsNil() { |
| 336 |
return true |
| 337 |
} |
| 338 |
return e.deepValueDerive(v1.Elem(), v2.Elem(), visited, depth+1) |
| 339 |
case reflect.Pointer: |
| 340 |
if v1.IsNil() { |
| 341 |
return true |
| 342 |
} |
| 343 |
return e.deepValueDerive(v1.Elem(), v2.Elem(), visited, depth+1) |
| 344 |
case reflect.Struct: |
| 345 |
for i, n := 0, v1.NumField(); i < n; i++ { |
| 346 |
if !e.deepValueDerive(v1.Field(i), v2.Field(i), visited, depth+1) { |
| 347 |
return false |
| 348 |
} |
| 349 |
} |
| 350 |
return true |
| 351 |
case reflect.Map: |
| 352 |
if v1.IsNil() || v1.Len() == 0 { |
| 353 |
return true |
| 354 |
} |
| 355 |
if v1.Len() > v2.Len() { |
| 356 |
return false |
| 357 |
} |
| 358 |
if v1.Pointer() == v2.Pointer() { |
| 359 |
return true |
| 360 |
} |
| 361 |
for _, k := range v1.MapKeys() { |
| 362 |
if !e.deepValueDerive(v1.MapIndex(k), v2.MapIndex(k), visited, depth+1) { |
| 363 |
return false |
| 364 |
} |
| 365 |
} |
| 366 |
return true |
| 367 |
case reflect.Func: |
| 368 |
if v1.IsNil() && v2.IsNil() { |
| 369 |
return true |
| 370 |
} |
| 371 |
// Can't do better than this: |
| 372 |
return false |
| 373 |
default: |
| 374 |
// Normal equality suffices |
| 375 |
if !v1.CanInterface() || !v2.CanInterface() { |
| 376 |
panic(unexportedTypePanic{}) |
| 377 |
} |
| 378 |
return v1.Interface() == v2.Interface() |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
// DeepDerivative is similar to DeepEqual except that unset fields in a1 are |
| 383 |
// ignored (not compared). This allows us to focus on the fields that matter to |
| 384 |
// the semantic comparison. |
| 385 |
// |
| 386 |
// The unset fields include a nil pointer and an empty string. |
| 387 |
func (e Equalities) DeepDerivative(a1, a2 any) bool { |
| 388 |
if a1 == nil { |
| 389 |
return true |
| 390 |
} |
| 391 |
v1 := reflect.ValueOf(a1) |
| 392 |
v2 := reflect.ValueOf(a2) |
| 393 |
if v1.Type() != v2.Type() { |
| 394 |
return false |
| 395 |
} |
| 396 |
return e.deepValueDerive(v1, v2, make(map[visit]bool), 0) |
| 397 |
} |