-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.go
57 lines (49 loc) · 1.01 KB
/
visualize.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
package dscopeutil
import (
"io"
"reflect"
"sync"
"github.com/emicklei/dot"
"github.com/reusee/dscope"
)
func Visualize(s dscope.Scope, w io.Writer) error {
s = s.Fork(dscope.DebugDefs)
var debugInfo dscope.DebugInfo
s.Assign(&debugInfo)
edges := make(map[[2]reflect.Type]bool)
for _, info := range debugInfo.Values {
for _, defType := range info.DefTypes {
if defType.Kind() != reflect.Func {
continue
}
for i := 0; i < defType.NumIn(); i++ {
in := defType.In(i)
for j := 0; j < defType.NumOut(); j++ {
out := defType.Out(j)
edges[[2]reflect.Type{out, in}] = true
}
}
}
}
g := dot.NewGraph(dot.Directed)
var nodes sync.Map
getNode := func(t reflect.Type) dot.Node {
if v, ok := nodes.Load(t); ok {
return v.(dot.Node)
}
node := g.Node(t.String())
nodes.Store(t, node)
return node
}
for edge := range edges {
g.Edge(
getNode(edge[0]),
getNode(edge[1]),
)
}
_, err := w.Write([]byte(g.String()))
if err != nil {
return we(err)
}
return nil
}