-
Notifications
You must be signed in to change notification settings - Fork 3
/
mermaid.go
94 lines (82 loc) · 2.23 KB
/
mermaid.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
package gdag
import (
"fmt"
)
// Mermaid outputs dag mermaidjs.
func (start *Node) Mermaid() (string, error) {
mg := newMermaidGenerator()
ret := "graph TD" + "\n"
ret += "classDef doneColor fill:#868787" + "\n"
ret += mg.generateComponents(start) + "\n"
ret += mg.generateRelations(start) + "\n"
return ret, nil
}
type mermaidGenerator struct {
uniqueComponents map[int]struct{}
uniqueRelations map[string]struct{}
}
func newMermaidGenerator() *mermaidGenerator {
return &mermaidGenerator{
uniqueComponents: map[int]struct{}{},
uniqueRelations: map[string]struct{}{},
}
}
func (mg *mermaidGenerator) generateComponents(start *Node) string {
return mg.generateComponent(start)
}
func (mg *mermaidGenerator) generateComponent(node *Node) string {
if _, ok := mg.uniqueComponents[node.index]; ok {
return ""
}
mg.uniqueComponents[node.index] = struct{}{}
ret := (*mermaidGenerator)(nil).renderComponent(node)
for _, d := range node.downstream {
ret += mg.generateComponent(d)
}
return ret
}
func (*mermaidGenerator) renderComponent(node *Node) string {
ret := ""
// TODO: mermaid用に修正するかどうか。リファクタは必要
switch node.nodeType {
case rectangle:
s := fmt.Sprintf("%d(\"%s\")", node.index, node.text)
if len(node.colorMermaid) != 0 {
s += fmt.Sprintf(":::%s", node.colorMermaid)
}
s += "\n"
ret += s
case usecase:
s := fmt.Sprintf("%d([\"%s\"])", node.index, node.text)
if node.hour > 0 {
s = fmt.Sprintf("%d([\"%s (%.1fh)\"])", node.index, node.text, node.hour)
}
if len(node.colorMermaid) != 0 {
s += fmt.Sprintf(":::%s", node.colorMermaid)
}
s += "\n"
ret += s
}
// nolint:staticcheck
if len(node.note) != 0 {
// noop
}
return ret
}
func (mg *mermaidGenerator) generateRelations(start *Node) string {
return mg.generateRelation(start, "")
}
func (mg *mermaidGenerator) generateRelation(node *Node, out string) string {
edge := fmt.Sprintf("%d --> ", node.index)
ret := out
for _, d := range node.downstream {
key := fmt.Sprintf("%d-%d", node.index, d.index)
if _, ok := mg.uniqueRelations[key]; ok {
continue
}
mg.uniqueRelations[key] = struct{}{}
tmp := fmt.Sprintf("%s%d\n", edge, d.index)
ret += mg.generateRelation(d, tmp)
}
return ret
}