-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathbasic.go
71 lines (61 loc) · 2.06 KB
/
basic.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
package memviz
import (
"fmt"
"reflect"
"strconv"
)
func (m *mapper) mapPtrIface(iVal reflect.Value, inlineable bool) (nodeID, string) {
pointee := iVal.Elem()
key := getNodeKey(iVal)
// inlineable=false so an invalid parentID is fine
pointeeNode, pointeeSummary := m.mapValue(pointee, 0, false)
summary := escapeString(iVal.Type().String())
m.nodeSummaries[key] = summary
if !pointee.IsValid() {
m.nodeSummaries[key] += "(" + pointeeSummary + ")"
return pointeeNode, m.nodeSummaries[key]
}
if !inlineable {
id := m.newBasicNode(iVal, summary)
fmt.Fprintf(m.writer, " %d:name -> %d:name;\n", id, pointeeNode)
return id, summary
}
return pointeeNode, summary
}
func (m *mapper) mapString(stringVal reflect.Value, inlineable bool) (nodeID, string) {
// We want the output to look like a Go quoted string literal. The first
// Quote achieves that. The second is to quote it for graphviz itself.
quoted := strconv.Quote(strconv.Quote(stringVal.String()))
// Lastly, quoting adds quotation-marks around the string, but it is
// inserted into a graphviz string literal, so we have to remove those.
quoted = quoted[1 : len(quoted)-1]
if inlineable {
return 0, quoted
}
m.nodeSummaries[getNodeKey(stringVal)] = "string"
return m.newBasicNode(stringVal, quoted), "string"
}
func (m *mapper) mapBool(stringVal reflect.Value, inlineable bool) (nodeID, string) {
value := fmt.Sprintf("%t", stringVal.Bool())
if inlineable {
return 0, value
}
m.nodeSummaries[getNodeKey(stringVal)] = "bool"
return m.newBasicNode(stringVal, value), "bool"
}
func (m *mapper) mapInt(numVal reflect.Value, inlineable bool) (nodeID, string) {
printed := strconv.Itoa(int(numVal.Int()))
if inlineable {
return 0, printed
}
m.nodeSummaries[getNodeKey(numVal)] = "int"
return m.newBasicNode(numVal, printed), "int"
}
func (m *mapper) mapUint(numVal reflect.Value, inlineable bool) (nodeID, string) {
printed := strconv.Itoa(int(numVal.Uint()))
if inlineable {
return 0, printed
}
m.nodeSummaries[getNodeKey(numVal)] = "uint"
return m.newBasicNode(numVal, printed), "uint"
}