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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
// Package subset implements logic to check if a value is a subset of
// another using reflect.
package subset
import (
"reflect"
)
// During checkSubset, must keep track of checks that are
// in progress. The comparison algorithm assumes that all
// checks in progress are true when it reencounters them.
// Visited are stored in a map indexed by 17 * a1 + a2;
type visit struct {
a1 uintptr
a2 uintptr
typ reflect.Type
next *visit
}
// Fatalf is how our assertion will fail.
type Fatalf interface {
Fatalf(format string, args ...interface{})
}
// Ideally we'ed be able to use reflec.valueInterface(v, false) and
// look at unexported fields, but for now we just ignore them.
func safeInterface(v reflect.Value) (i interface{}) {
defer func() {
if err := recover(); err != nil {
// fmt.Println("Recovered safeInterface:", err)
i = nil
}
}()
return v.Interface()
}
// Tests for deep equality using reflected types. The map argument tracks
// comparisons that have already been seen, which allows short circuiting on
// recursive types.
func checkSubset(expected, target reflect.Value, visited map[uintptr]*visit, depth int) (b bool) {
if !expected.IsValid() {
// fmt.Println("!expected.IsValid()")
return true
}
if !target.IsValid() {
// fmt.Println("!target.IsValid()")
return false
}
if expected.Type() != target.Type() {
// fmt.Println("Type() differs")
return false
}
// if depth > 10 { panic("checkSubset") } // for debugging
if expected.CanAddr() && target.CanAddr() {
addr1 := expected.UnsafeAddr()
addr2 := target.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
h := 17*addr1 + addr2
seen := visited[h]
typ := expected.Type()
for p := seen; p != nil; p = p.next {
if p.a1 == addr1 && p.a2 == addr2 && p.typ == typ {
return true
}
}
// Remember for later.
visited[h] = &visit{addr1, addr2, typ, seen}
}
switch expected.Kind() {
case reflect.Array:
// fmt.Println("Kind: Array")
if expected.Len() == 0 {
return true
}
if expected.Len() != target.Len() {
return false
}
for i := 0; i < expected.Len(); i++ {
if !checkSubset(expected.Index(i), target.Index(i), visited, depth+1) {
return false
}
}
return true
case reflect.Slice:
// fmt.Println("Kind: Slice")
if expected.IsNil() {
return true
}
if expected.IsNil() != target.IsNil() {
return false
}
if expected.Len() != target.Len() {
return false
}
for i := 0; i < expected.Len(); i++ {
if !checkSubset(expected.Index(i), target.Index(i), visited, depth+1) {
return false
}
}
return true
case reflect.Interface:
// fmt.Println("Kind: Interface")
if expected.IsNil() {
return true
}
if expected.IsNil() || target.IsNil() {
return expected.IsNil() == target.IsNil()
}
return checkSubset(expected.Elem(), target.Elem(), visited, depth+1)
case reflect.Ptr:
// fmt.Println("Kind: Ptr")
return checkSubset(expected.Elem(), target.Elem(), visited, depth+1)
case reflect.Struct:
// fmt.Println("Kind: Struct")
for i, n := 0, expected.NumField(); i < n; i++ {
if !checkSubset(expected.Field(i), target.Field(i), visited, depth+1) {
return false
}
}
return true
case reflect.Map:
// fmt.Println("Kind: Map")
if expected.IsNil() {
return true
}
if expected.IsNil() != target.IsNil() {
return false
}
for _, k := range expected.MapKeys() {
if !checkSubset(expected.MapIndex(k), target.MapIndex(k), visited, depth+1) {
return false
}
}
return true
case reflect.Func:
// fmt.Println("Kind: Func")
if expected.IsNil() && target.IsNil() {
return true
}
// Can't do better than this:
return false
default:
expectedInterface := safeInterface(expected)
if expectedInterface == nil {
return true
}
targetInterface := target.Interface() // expect this to be safe now
// fmt.Println("Kind: default", expectedInterface, targetInterface)
// ignore zero value expectations
zeroValue := reflect.Zero(expected.Type())
if reflect.DeepEqual(expectedInterface, zeroValue.Interface()) {
// fmt.Println("Expecting zero value")
return true
}
// Normal equality suffices
return reflect.DeepEqual(expectedInterface, targetInterface)
}
}
// Check tests for deep subset. It uses normal == equality where
// possible but will scan members of arrays, slices, maps, and fields of
// structs. It correctly handles recursive types. Functions are equal
// only if they are both nil.
func Check(expected, target interface{}) bool {
if expected == nil {
return true
}
if target == nil {
return false
}
return checkSubset(
reflect.ValueOf(expected),
reflect.ValueOf(target),
make(map[uintptr]*visit),
0)
}
// Assert will fatal if not a subset with a useful message.
// TODO should pretty print and show a colored side-by-side diff?
func Assert(t Fatalf, expected interface{}, actual interface{}) {
if !Check(expected, actual) {
t.Fatalf("Did not find expected subset:\n%+v\nInstead found:\n%+v",
expected, actual)
}
}
|