-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdot.go
64 lines (48 loc) · 1.53 KB
/
dot.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
package ograph
import (
"bytes"
"fmt"
"reflect"
"strings"
)
func (pipeline *Pipeline) DumpDOT() ([]byte, error) {
buf := &bytes.Buffer{}
buf.WriteString(fmt.Sprintf("digraph %s {\n", pipeline.name))
for _, v := range pipeline.graph.Vertices {
if err := pipeline.dumpDOTNode(buf, v.Elem, "\t"); err != nil {
return nil, err
}
}
for edge := range pipeline.graph.Edges {
if err := pipeline.dumpDOTEdge(buf, edge.From.Elem, edge.To.Elem, "\t"); err != nil {
return nil, err
}
}
buf.WriteString("}\n")
return buf.Bytes(), nil
}
func (pipeline *Pipeline) dumpDOTNode(buf *bytes.Buffer, elem *Element, indent string) error {
var tags []string
tags = append(tags, fmt.Sprintf("Name: %s", elem.Name))
if elem.Virtual {
tags = append(tags, "Virtual Node")
} else if elem.FactoryName != "" {
tags = append(tags, fmt.Sprintf("Factory: %s", elem.FactoryName))
if len(elem.SubElements) > 0 {
var group []string
for _, e := range elem.SubElements {
group = append(group, e.Name)
}
tags = append(tags, "Include: "+strings.Join(group, ","))
}
} else if elem.Singleton != nil {
tags = append(tags, fmt.Sprintf("Type: %v", reflect.TypeOf(elem.Singleton)))
}
label := fmt.Sprintf("{%s}", strings.Join(tags, "|"))
buf.WriteString(fmt.Sprintf("%s%s [shape=record, label=\"%s\"];\n", indent, elem.Name, label))
return nil
}
func (pipeline *Pipeline) dumpDOTEdge(buf *bytes.Buffer, from, to *Element, indent string) error {
buf.WriteString(fmt.Sprintf("%s%s -> %s;\n", indent, from.Name, to.Name))
return nil
}