2012-03-27 23:13:14 +00: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.
|
2014-09-21 17:33:12 +00:00
|
|
|
// Visited comparisons are stored in a map indexed by visit.
|
2012-03-27 23:13:14 +00:00
|
|
|
type visit struct {
|
2014-09-21 17:33:12 +00:00
|
|
|
a1 uintptr
|
|
|
|
a2 uintptr
|
|
|
|
typ Type
|
2012-03-27 23:13:14 +00: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.
|
2014-09-21 17:33:12 +00:00
|
|
|
func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
|
2012-03-27 23:13:14 +00:00
|
|
|
if !v1.IsValid() || !v2.IsValid() {
|
|
|
|
return v1.IsValid() == v2.IsValid()
|
|
|
|
}
|
|
|
|
if v1.Type() != v2.Type() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if depth > 10 { panic("deepValueEqual") } // for debugging
|
2014-09-21 17:33:12 +00:00
|
|
|
hard := func(k Kind) bool {
|
|
|
|
switch k {
|
|
|
|
case Array, Map, Slice, Struct:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
if v1.CanAddr() && v2.CanAddr() && hard(v1.Kind()) {
|
2012-03-27 23:13:14 +00:00
|
|
|
addr1 := v1.UnsafeAddr()
|
|
|
|
addr2 := v2.UnsafeAddr()
|
|
|
|
if addr1 > addr2 {
|
|
|
|
// Canonicalize order to reduce number of entries in visited.
|
|
|
|
addr1, addr2 = addr2, addr1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Short circuit if references are identical ...
|
|
|
|
if addr1 == addr2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... or already seen
|
|
|
|
typ := v1.Type()
|
2014-09-21 17:33:12 +00:00
|
|
|
v := visit{addr1, addr2, typ}
|
|
|
|
if visited[v] {
|
|
|
|
return true
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember for later.
|
2014-09-21 17:33:12 +00:00
|
|
|
visited[v] = true
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v1.Kind() {
|
|
|
|
case Array:
|
|
|
|
for i := 0; i < v1.Len(); i++ {
|
|
|
|
if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case Slice:
|
|
|
|
if v1.IsNil() != v2.IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v1.Len() != v2.Len() {
|
|
|
|
return false
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if v1.Pointer() == v2.Pointer() {
|
|
|
|
return true
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
for i := 0; i < v1.Len(); i++ {
|
|
|
|
if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case Interface:
|
|
|
|
if v1.IsNil() || v2.IsNil() {
|
|
|
|
return v1.IsNil() == v2.IsNil()
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case Map:
|
|
|
|
if v1.IsNil() != v2.IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v1.Len() != v2.Len() {
|
|
|
|
return false
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if v1.Pointer() == v2.Pointer() {
|
|
|
|
return true
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
for _, k := range v1.MapKeys() {
|
|
|
|
if !deepValueEqual(v1.MapIndex(k), v2.MapIndex(k), visited, depth+1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case Func:
|
|
|
|
if v1.IsNil() && v2.IsNil() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Can't do better than this:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
// Normal equality suffices
|
|
|
|
return valueInterface(v1, false) == valueInterface(v2, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00: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.
|
|
|
|
// An empty slice is not equal to a nil slice.
|
2012-03-27 23:13:14 +00:00
|
|
|
func DeepEqual(a1, a2 interface{}) bool {
|
|
|
|
if a1 == nil || a2 == nil {
|
|
|
|
return a1 == a2
|
|
|
|
}
|
|
|
|
v1 := ValueOf(a1)
|
|
|
|
v2 := ValueOf(a2)
|
|
|
|
if v1.Type() != v2.Type() {
|
|
|
|
return false
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
return deepValueEqual(v1, v2, make(map[visit]bool), 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|