forked from davecgh/go-spew
-
Notifications
You must be signed in to change notification settings - Fork 2
/
walk.go
123 lines (110 loc) · 3 KB
/
walk.go
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
/*
* Copyright (c) 2013 Dave Collins <[email protected]>
* Copyright (c) 2015 Dan Kortschak <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package utter
import "reflect"
// walkPtr handles walking of pointers by indirecting them as necessary.
func (d *dumpState) walkPtr(v reflect.Value) {
// Remove pointers at or below the current depth from map used to detect
// circular refs.
for k, depth := range d.pointers {
if depth >= d.depth {
delete(d.pointers, k)
}
}
var nilFound, cycleFound bool
for v.Kind() == reflect.Ptr {
if v.IsNil() {
nilFound = true
break
}
addr := v.Pointer()
if pd, ok := d.pointers[addr]; ok && pd < d.depth {
cycleFound = true
break
}
d.pointers[addr] = d.depth
d.nodes[addrType{addr, v.Type()}] = struct{}{}
v = v.Elem()
if v.Kind() == reflect.Interface {
if v.IsNil() {
nilFound = true
break
}
v = v.Elem()
}
d.nodes[addrType{addr, v.Type()}] = struct{}{}
}
if !nilFound && !cycleFound {
d.walk(v, false, false, false, 0)
}
}
// walkSlice handles walking of arrays and slices.
func (d *dumpState) walkSlice(v reflect.Value) {
d.depth++
// Recursively call walk for each item.
for i := 0; i < v.Len(); i++ {
d.walk(d.unpackValue(v.Index(i)))
}
d.depth--
}
// walk is the main workhorse for walking a value. It uses the passed reflect
// value to figure out what kind of object we are dealing with and follows it
// appropriately. It is a recursive function, however circular data structures
// are detected and escaped from.
func (d *dumpState) walk(v reflect.Value, _, _, _ bool, _ uintptr) {
// Handle invalid reflect values immediately.
kind := v.Kind()
if kind == reflect.Invalid {
return
}
// Handle pointers specially.
if kind == reflect.Ptr {
d.walkPtr(v)
return
}
switch kind {
case reflect.Slice:
if v.IsNil() {
break
}
fallthrough
case reflect.Array:
d.walkSlice(v)
case reflect.Map:
if v.IsNil() {
break
}
d.depth++
keys := v.MapKeys()
for _, key := range keys {
d.walk(d.unpackValue(key))
d.walk(d.unpackValue(v.MapIndex(key)))
}
d.depth--
case reflect.Struct:
d.depth++
vt := v.Type()
for i := 0; i < v.NumField(); i++ {
vtf := vt.Field(i)
if d.cs.IgnoreUnexported && vtf.PkgPath != "" {
continue
}
d.walk(d.unpackValue(v.Field(i)))
}
d.depth--
}
}