-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
collections.go
163 lines (133 loc) · 4.74 KB
/
collections.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
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
package memviz
import (
"fmt"
"reflect"
"unsafe"
)
func (m *mapper) mapStruct(structVal reflect.Value) (nodeID, string) {
uType := structVal.Type()
id := m.getNodeID(structVal)
key := getNodeKey(structVal)
m.nodeSummaries[key] = escapeString(uType.String())
var fields string
var links []string
for index := 0; index < uType.NumField(); index++ {
field := structVal.Field(index)
if !field.CanAddr() {
// TODO: when does this happen? Can we work around it?
continue
}
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
fieldID, summary := m.mapValue(field, id, true)
// if field was inlined (id == 0) then print summary, else just the name and a link to the actual
if fieldID == 0 {
fields += fmt.Sprintf("|{<f%d> %s | %s} ", index, uType.Field(index).Name, summary)
} else {
fields += fmt.Sprintf("|<f%d> %s", index, uType.Field(index).Name)
links = append(links, fmt.Sprintf(" %d:f%d -> %d:name;\n", id, index, fieldID))
}
}
node := fmt.Sprintf(" %d [label=\"<name> %s %s \"];\n", id, structVal.Type().Name(), fields)
fmt.Fprint(m.writer, node)
for _, link := range links {
fmt.Fprint(m.writer, link)
}
return id, m.nodeSummaries[key]
}
func (m *mapper) mapSlice(sliceVal reflect.Value, parentID nodeID, inlineable bool) (nodeID, string) {
sliceID := m.getNodeID(sliceVal)
key := getNodeKey(sliceVal)
sliceType := escapeString(sliceVal.Type().String())
m.nodeSummaries[key] = sliceType
if sliceVal.Len() == 0 {
m.nodeSummaries[key] = sliceType + "\\{\\}"
if inlineable {
return 0, m.nodeSummaries[key]
}
return m.newBasicNode(sliceVal, m.nodeSummaries[key]), sliceType
}
// sourceID is the nodeID that links will start from
// if inlined then these come from the parent
// if not inlined then these come from this node
sourceID := sliceID
if inlineable && sliceVal.Len() <= m.inlineableItemLimit {
sourceID = parentID
}
length := sliceVal.Len()
var elements string
var links []string
for index := 0; index < length; index++ {
indexID, summary := m.mapValue(sliceVal.Index(index), sliceID, true)
if indexID != 0 {
// need pointer to value
elements += fmt.Sprintf("|<%dindex%d> %d", sliceID, index, index)
links = append(links, fmt.Sprintf(" %d:<%dindex%d> -> %d:name;\n", sourceID, sliceID, index, indexID))
} else {
// field was inlined so print summary
elements += fmt.Sprintf("|{<%dindex%d> %d|<%dvalue%d> %s}", sliceID, index, index, sliceID, index, summary)
}
}
for _, link := range links {
fmt.Fprint(m.writer, link)
}
if inlineable && length <= m.inlineableItemLimit {
// inline slice
// remove stored summary so this gets regenerated every time
// we need to do this so that we get a chance to print out the new links
delete(m.nodeSummaries, key)
// have to remove invalid leading |
return 0, "{" + elements[1:] + "}"
}
// else create a new node
node := fmt.Sprintf(" %d [label=\"<name> %s %s \"];\n", sliceID, sliceType, elements)
fmt.Fprint(m.writer, node)
return sliceID, m.nodeSummaries[key]
}
func (m *mapper) mapMap(mapVal reflect.Value, parentID nodeID, inlineable bool) (nodeID, string) {
// create a string type while escaping graphviz special characters
mapType := escapeString(mapVal.Type().String())
nodeKey := getNodeKey(mapVal)
if mapVal.Len() == 0 {
m.nodeSummaries[nodeKey] = mapType + "\\{\\}"
if inlineable {
return 0, m.nodeSummaries[nodeKey]
}
return m.newBasicNode(mapVal, m.nodeSummaries[nodeKey]), mapType
}
mapID := m.getNodeID(mapVal)
var id nodeID
if inlineable && mapVal.Len() <= m.inlineableItemLimit {
m.nodeSummaries[nodeKey] = mapType
id = parentID
} else {
id = mapID
}
var links []string
var fields string
for index, mapKey := range mapVal.MapKeys() {
keyID, keySummary := m.mapValue(mapKey, id, true)
valueID, valueSummary := m.mapValue(mapVal.MapIndex(mapKey), id, true)
fields += fmt.Sprintf("|{<%dkey%d> %s| <%dvalue%d> %s}", mapID, index, keySummary, mapID, index, valueSummary)
if keyID != 0 {
links = append(links, fmt.Sprintf(" %d:<%dkey%d> -> %d:name;\n", id, mapID, index, keyID))
}
if valueID != 0 {
links = append(links, fmt.Sprintf(" %d:<%dvalue%d> -> %d:name;\n", id, mapID, index, valueID))
}
}
for _, link := range links {
fmt.Fprint(m.writer, link)
}
if inlineable && mapVal.Len() <= m.inlineableItemLimit {
// inline map
// remove stored summary so this gets regenerated every time
// we need to do this so that we get a chance to print out the new links
delete(m.nodeSummaries, nodeKey)
// have to remove invalid leading |
return 0, "{" + fields[1:] + "}"
}
// else create a new node
node := fmt.Sprintf(" %d [label=\"<name> %s %s \"];\n", id, mapType, fields)
fmt.Fprint(m.writer, node)
return id, m.nodeSummaries[nodeKey]
}