/
sequencediagram.go
171 lines (155 loc) · 6.63 KB
/
sequencediagram.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
164
165
166
167
168
169
170
171
package sequencediagram
import (
"fmt"
"regexp"
"strings"
"github.com/anz-bank/sysl/pkg/mermaid"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslutil"
)
// sequencePair keeps track of the application pairs and the associated endpoint we visit during the recursion
type sequencePair struct {
firstApp, secondApp, endPoint string
}
var startElse = regexp.MustCompile("^else.*")
var isElse = regexp.MustCompile("^else$")
var isLoop = regexp.MustCompile("^loop.*")
// GenerateSequenceDiagram accepts an application name and an endpoint as inputs and returns a string and an error
// The resulting string is the mermaid code for the sequence diagram for that application and endpoint
func GenerateSequenceDiagram(m *sysl.Module, appname string, epname string) (string, error) {
return generateSequenceDiagramHelper(m, syslutil.CleanAppName(appname), epname, "...", 1, &[]sequencePair{}, true)
}
// generateSequenceDiagramHelper is a helper which has additional arguments which need not be entered by the user
func generateSequenceDiagramHelper(m *sysl.Module, appName string, epName string,
previousApp string, indent int, sequencePairs *[]sequencePair, theStart bool) (string, error) {
var result string
if err := isValidAppNameAndEndpoint(m, appName, epName); err != nil {
return "", err
}
if theStart {
result = mermaid.GeneratedHeader + "sequenceDiagram\n"
result += fmt.Sprintf(" %s ->> %s: %s\n", previousApp, cleanAppName(appName), epName)
}
statements := m.Apps[appName].Endpoints[epName].GetStmt()
result += printSequenceDiagramStatements(m, statements, appName, previousApp, indent, sequencePairs, theStart)
return result, nil
}
// printSequenceDiagramStatements is where the printing takes place
// Uses a switch statement to decide what to print and what recursion needs to be done
func printSequenceDiagramStatements(m *sysl.Module, statements []*sysl.Statement, appName string,
previousApp string, indent int, sequencePairs *[]sequencePair, theStart bool) string {
var result string
count := 0
for _, statement := range statements {
switch c := statement.Stmt.(type) {
case *sysl.Statement_Group:
result += fmt.Sprintf("%s%s\n", addIndent(indent), c.Group.Title)
result += printSequenceDiagramStatements(m, c.Group.Stmt, appName, previousApp, indent+1, sequencePairs, theStart)
isloop := isLoop.MatchString(c.Group.Title)
iselse := isElse.MatchString(c.Group.Title)
if isloop || iselse {
result += fmt.Sprintf("%send\n", addIndent(indent))
}
case *sysl.Statement_Call:
nextapp := syslutil.GetAppName(c.Call.Target)
nextep := c.Call.Endpoint
pair := sequencePair{appName, nextep, nextep}
if !sequencePairsContain(*sequencePairs, pair) {
*sequencePairs = append(*sequencePairs, pair)
result += callStatement(appName, nextep, nextapp, indent)
previous := appName
out, err := generateSequenceDiagramHelper(m, nextapp, nextep, previous, indent, sequencePairs, false)
if err != nil {
panic("Error in generating sequence diagram; check if app names or endpoints are correct")
}
result += out
}
case *sysl.Statement_Ret:
retEndpoint := c.Ret.Payload
result += retStatement(appName, retEndpoint, previousApp, indent, theStart)
case *sysl.Statement_Action:
result += actionStatement(appName, c.Action.Action, indent)
case *sysl.Statement_Cond:
result += fmt.Sprintf("%salt %s\n", addIndent(indent), c.Cond.Test)
result += printSequenceDiagramStatements(m, c.Cond.Stmt, appName, previousApp, indent+1, sequencePairs, theStart)
if count+1 < len(statements) {
switch temp := statements[count+1].Stmt.(type) {
case *sysl.Statement_Group:
if ok := startElse.MatchString(temp.Group.Title); !ok {
result += fmt.Sprintf("%send\n", addIndent(indent))
}
default:
result += fmt.Sprintf("%send\n", addIndent(indent))
}
} else {
result += fmt.Sprintf("%send\n", addIndent(indent))
}
case *sysl.Statement_Foreach:
result += fmt.Sprintf("%sloop %s\n", addIndent(indent), c.Foreach.Collection)
result += printSequenceDiagramStatements(m, c.Foreach.Stmt, appName, previousApp, indent+1, sequencePairs, theStart)
result += fmt.Sprintf("%send\n", addIndent(indent))
case *sysl.Statement_Loop:
result += fmt.Sprintf("%sloop %s\n", addIndent(indent), c.Loop.Criterion)
result += printSequenceDiagramStatements(m, c.Loop.Stmt, appName, previousApp, indent+1, sequencePairs, theStart)
result += fmt.Sprintf("%send\n", addIndent(indent))
case *sysl.Statement_LoopN:
result += fmt.Sprintf("%sloop %d times\n", addIndent(indent), c.LoopN.Count)
result += printSequenceDiagramStatements(m, c.LoopN.Stmt, appName, previousApp, indent+1, sequencePairs, theStart)
result += fmt.Sprintf("%send\n", addIndent(indent))
default:
result += ""
}
count++
}
return result
}
// isValidAppNameAndEndpoint checks if the entered application name and endpoint exists in the sysl module or not
func isValidAppNameAndEndpoint(m *sysl.Module, appName string, epName string) error {
if _, ok := m.Apps[appName]; !ok {
return fmt.Errorf("invalid app name %s", appName)
}
if _, ok := m.Apps[appName].Endpoints[epName]; !ok {
return fmt.Errorf("invalid endpoint %s", epName)
}
return nil
}
// callStatement is a printer to print a call statement
func callStatement(appName string, epName string, nextApp string, indent int) string {
return fmt.Sprintf("%s%s ->> %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(nextApp), epName)
}
// retStatement is a printer to print a return statement
func retStatement(appName string, epName string, previousApp string, indent int, theStart bool) string {
if theStart {
return fmt.Sprintf("%s%s -->> %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(previousApp), epName)
}
return fmt.Sprintf("%s%s -->> %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(previousApp), epName)
}
// actionStatement is a printer to print an action statement
func actionStatement(appName string, action string, indent int) string {
return fmt.Sprintf("%s%s ->> %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(appName), action)
}
// addIndent adds indents based on the input
func addIndent(indent int) string {
var out string
for i := 0; i < indent; i++ {
out += " "
}
return out
}
// sequencePairsContain checks if the application-endpoint group have been already visited or not
func sequencePairsContain(s []sequencePair, sp sequencePair) bool {
for _, a := range s {
if a == sp {
return true
}
}
return false
}
func cleanAppName(s string) string {
removeColons := strings.ReplaceAll(s, " :: ", "_")
return strings.ReplaceAll(removeColons, "-", "_")
}