github.com/x04/go/src@v0.0.0-20200202162449-3d481ceb3525/reflect/deepequal.go (about) 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 // Deep equality test via reflection 6 7 package reflect 8 9 import "github.com/x04/go/src/unsafe" 10 11 // During deepValueEqual, must keep track of checks that are 12 // in progress. The comparison algorithm assumes that all 13 // checks in progress are true when it reencounters them. 14 // Visited comparisons are stored in a map indexed by visit. 15 type visit struct { 16 a1 unsafe.Pointer 17 a2 unsafe.Pointer 18 typ Type 19 } 20 21 // Tests for deep equality using reflected types. The map argument tracks 22 // comparisons that have already been seen, which allows short circuiting on 23 // recursive types. 24 func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool { 25 if !v1.IsValid() || !v2.IsValid() { 26 return v1.IsValid() == v2.IsValid() 27 } 28 if v1.Type() != v2.Type() { 29 return false 30 } 31 32 // if depth > 10 { panic("deepValueEqual") } // for debugging 33 34 // We want to avoid putting more in the visited map than we need to. 35 // For any possible reference cycle that might be encountered, 36 // hard(v1, v2) needs to return true for at least one of the types in the cycle, 37 // and it's safe and valid to get Value's internal pointer. 38 hard := func(v1, v2 Value) bool { 39 switch v1.Kind() { 40 case Map, Slice, Ptr, Interface: 41 // Nil pointers cannot be cyclic. Avoid putting them in the visited map. 42 return !v1.IsNil() && !v2.IsNil() 43 } 44 return false 45 } 46 47 if hard(v1, v2) { 48 addr1 := v1.ptr 49 addr2 := v2.ptr 50 if uintptr(addr1) > uintptr(addr2) { 51 // Canonicalize order to reduce number of entries in visited. 52 // Assumes non-moving garbage collector. 53 addr1, addr2 = addr2, addr1 54 } 55 56 // Short circuit if references are already seen. 57 typ := v1.Type() 58 v := visit{addr1, addr2, typ} 59 if visited[v] { 60 return true 61 } 62 63 // Remember for later. 64 visited[v] = true 65 } 66 67 switch v1.Kind() { 68 case Array: 69 for i := 0; i < v1.Len(); i++ { 70 if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) { 71 return false 72 } 73 } 74 return true 75 case Slice: 76 if v1.IsNil() != v2.IsNil() { 77 return false 78 } 79 if v1.Len() != v2.Len() { 80 return false 81 } 82 if v1.Pointer() == v2.Pointer() { 83 return true 84 } 85 for i := 0; i < v1.Len(); i++ { 86 if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) { 87 return false 88 } 89 } 90 return true 91 case Interface: 92 if v1.IsNil() || v2.IsNil() { 93 return v1.IsNil() == v2.IsNil() 94 } 95 return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1) 96 case Ptr: 97 if v1.Pointer() == v2.Pointer() { 98 return true 99 } 100 return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1) 101 case Struct: 102 for i, n := 0, v1.NumField(); i < n; i++ { 103 if !deepValueEqual(v1.Field(i), v2.Field(i), visited, depth+1) { 104 return false 105 } 106 } 107 return true 108 case Map: 109 if v1.IsNil() != v2.IsNil() { 110 return false 111 } 112 if v1.Len() != v2.Len() { 113 return false 114 } 115 if v1.Pointer() == v2.Pointer() { 116 return true 117 } 118 for _, k := range v1.MapKeys() { 119 val1 := v1.MapIndex(k) 120 val2 := v2.MapIndex(k) 121 if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(val1, val2, visited, depth+1) { 122 return false 123 } 124 } 125 return true 126 case Func: 127 if v1.IsNil() && v2.IsNil() { 128 return true 129 } 130 // Can't do better than this: 131 return false 132 default: 133 // Normal equality suffices 134 return valueInterface(v1, false) == valueInterface(v2, false) 135 } 136 } 137 138 // DeepEqual reports whether x and y are ``deeply equal,'' defined as follows. 139 // Two values of identical type are deeply equal if one of the following cases applies. 140 // Values of distinct types are never deeply equal. 141 // 142 // Array values are deeply equal when their corresponding elements are deeply equal. 143 // 144 // Struct values are deeply equal if their corresponding fields, 145 // both exported and unexported, are deeply equal. 146 // 147 // Func values are deeply equal if both are nil; otherwise they are not deeply equal. 148 // 149 // Interface values are deeply equal if they hold deeply equal concrete values. 150 // 151 // Map values are deeply equal when all of the following are true: 152 // they are both nil or both non-nil, they have the same length, 153 // and either they are the same map object or their corresponding keys 154 // (matched using Go equality) map to deeply equal values. 155 // 156 // Pointer values are deeply equal if they are equal using Go's == operator 157 // or if they point to deeply equal values. 158 // 159 // Slice values are deeply equal when all of the following are true: 160 // they are both nil or both non-nil, they have the same length, 161 // and either they point to the same initial entry of the same underlying array 162 // (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. 163 // Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) 164 // are not deeply equal. 165 // 166 // Other values - numbers, bools, strings, and channels - are deeply equal 167 // if they are equal using Go's == operator. 168 // 169 // In general DeepEqual is a recursive relaxation of Go's == operator. 170 // However, this idea is impossible to implement without some inconsistency. 171 // Specifically, it is possible for a value to be unequal to itself, 172 // either because it is of func type (uncomparable in general) 173 // or because it is a floating-point NaN value (not equal to itself in floating-point comparison), 174 // or because it is an array, struct, or interface containing 175 // such a value. 176 // On the other hand, pointer values are always equal to themselves, 177 // even if they point at or contain such problematic values, 178 // because they compare equal using Go's == operator, and that 179 // is a sufficient condition to be deeply equal, regardless of content. 180 // DeepEqual has been defined so that the same short-cut applies 181 // to slices and maps: if x and y are the same slice or the same map, 182 // they are deeply equal regardless of content. 183 // 184 // As DeepEqual traverses the data values it may find a cycle. The 185 // second and subsequent times that DeepEqual compares two pointer 186 // values that have been compared before, it treats the values as 187 // equal rather than examining the values to which they point. 188 // This ensures that DeepEqual terminates. 189 func DeepEqual(x, y interface{}) bool { 190 if x == nil || y == nil { 191 return x == y 192 } 193 v1 := ValueOf(x) 194 v2 := ValueOf(y) 195 if v1.Type() != v2.Type() { 196 return false 197 } 198 return deepValueEqual(v1, v2, make(map[visit]bool), 0) 199 }