2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Deep equality test via reflection
|
|
|
|
|
|
|
|
package reflect
|
|
|
|
|
|
|
|
// During deepValueEqual, must keep track of checks that are
|
|
|
|
// in progress. The comparison algorithm assumes that all
|
|
|
|
// checks in progress are true when it reencounters them.
|
2013-11-06 20:49:01 +01:00
|
|
|
// Visited comparisons are stored in a map indexed by visit.
|
2010-12-03 05:34:57 +01:00
|
|
|
type visit struct {
|
2013-11-06 20:49:01 +01:00
|
|
|
a1 uintptr
|
|
|
|
a2 uintptr
|
|
|
|
typ Type
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests for deep equality using reflected types. The map argument tracks
|
|
|
|
// comparisons that have already been seen, which allows short circuiting on
|
|
|
|
// recursive types.
|
2013-11-06 20:49:01 +01:00
|
|
|
func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
|
2011-05-20 02:18:15 +02:00
|
|
|
if !v1.IsValid() || !v2.IsValid() {
|
|
|
|
return v1.IsValid() == v2.IsValid()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if v1.Type() != v2.Type() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if depth > 10 { panic("deepValueEqual") } // for debugging
|
2013-11-06 20:49:01 +01:00
|
|
|
hard := func(k Kind) bool {
|
|
|
|
switch k {
|
|
|
|
case Array, Map, Slice, Struct:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if v1.CanAddr() && v2.CanAddr() && hard(v1.Kind()) {
|
2011-05-20 02:18:15 +02:00
|
|
|
addr1 := v1.UnsafeAddr()
|
|
|
|
addr2 := v2.UnsafeAddr()
|
|
|
|
if addr1 > addr2 {
|
|
|
|
// Canonicalize order to reduce number of entries in visited.
|
|
|
|
addr1, addr2 = addr2, addr1
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Short circuit if references are identical ...
|
|
|
|
if addr1 == addr2 {
|
2010-12-03 05:34:57 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// ... or already seen
|
|
|
|
typ := v1.Type()
|
2013-11-06 20:49:01 +01:00
|
|
|
v := visit{addr1, addr2, typ}
|
|
|
|
if visited[v] {
|
|
|
|
return true
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember for later.
|
2013-11-06 20:49:01 +01:00
|
|
|
visited[v] = true
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
switch v1.Kind() {
|
|
|
|
case Array:
|
|
|
|
for i := 0; i < v1.Len(); i++ {
|
|
|
|
if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2011-05-20 02:18:15 +02:00
|
|
|
case Slice:
|
2011-12-13 00:40:51 +01:00
|
|
|
if v1.IsNil() != v2.IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
if v1.Len() != v2.Len() {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if v1.Pointer() == v2.Pointer() {
|
|
|
|
return true
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
for i := 0; i < v1.Len(); i++ {
|
|
|
|
if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2011-05-20 02:18:15 +02:00
|
|
|
case Interface:
|
|
|
|
if v1.IsNil() || v2.IsNil() {
|
|
|
|
return v1.IsNil() == v2.IsNil()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1)
|
|
|
|
case Ptr:
|
|
|
|
return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1)
|
|
|
|
case Struct:
|
|
|
|
for i, n := 0, v1.NumField(); i < n; i++ {
|
|
|
|
if !deepValueEqual(v1.Field(i), v2.Field(i), visited, depth+1) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2011-05-20 02:18:15 +02:00
|
|
|
case Map:
|
2011-12-13 00:40:51 +01:00
|
|
|
if v1.IsNil() != v2.IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
if v1.Len() != v2.Len() {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if v1.Pointer() == v2.Pointer() {
|
|
|
|
return true
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
for _, k := range v1.MapKeys() {
|
|
|
|
if !deepValueEqual(v1.MapIndex(k), v2.MapIndex(k), visited, depth+1) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2012-03-06 18:57:23 +01:00
|
|
|
case Func:
|
|
|
|
if v1.IsNil() && v2.IsNil() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Can't do better than this:
|
|
|
|
return false
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
|
|
|
// Normal equality suffices
|
2011-10-22 18:19:46 +02:00
|
|
|
return valueInterface(v1, false) == valueInterface(v2, false)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// DeepEqual tests for deep equality. It uses normal == equality where
|
|
|
|
// possible but will scan elements of arrays, slices, maps, and fields of
|
|
|
|
// structs. In maps, keys are compared with == but elements use deep
|
|
|
|
// equality. DeepEqual correctly handles recursive types. Functions are equal
|
|
|
|
// only if they are both nil.
|
2012-10-23 06:31:11 +02:00
|
|
|
// An empty slice is not equal to a nil slice.
|
2010-12-03 05:34:57 +01:00
|
|
|
func DeepEqual(a1, a2 interface{}) bool {
|
|
|
|
if a1 == nil || a2 == nil {
|
|
|
|
return a1 == a2
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
v1 := ValueOf(a1)
|
|
|
|
v2 := ValueOf(a2)
|
2010-12-03 05:34:57 +01:00
|
|
|
if v1.Type() != v2.Type() {
|
|
|
|
return false
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return deepValueEqual(v1, v2, make(map[visit]bool), 0)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|