-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_diagram.go
286 lines (270 loc) · 8.76 KB
/
create_diagram.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// diagram-creation.go: all the methods attached to the generator object to be used in templating
package catalog
import (
"os"
"path"
"strings"
"github.com/anz-bank/protoc-gen-sysl/newsysl"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/anz-bank/sysl/pkg/syslwrapper"
)
// GetReturnType converts an application and a param into a type, useful for getting attributes.
func (p *Generator) GetParamType(app *sysl.Application, param *sysl.Param) *sysl.Type {
var appName, typeName string
appName, typeName = GetAppTypeName(param)
if appName == "" {
appName = path.Join(app.GetName().GetPart()...)
}
return p.RootModule.Apps[appName].GetTypes()[typeName]
}
// GetReturnType converts an endpoint and a statement into a type, useful for getting attributes.
func (p *Generator) GetReturnType(endpoint *sysl.Endpoint, stmnt *sysl.Statement) *sysl.Type {
var appName, typeName string
ret := stmnt.GetRet()
if ret == nil {
return nil
}
t := strings.ReplaceAll(ofTypeSymbol.FindString(ret.GetPayload()), "<: ", "")
if strings.Contains(t, "sequence of") {
t = strings.ReplaceAll(t, "sequence of ", "")
}
if split := strings.Split(t, "."); len(split) > 1 {
appName = split[0]
typeName = split[1]
} else {
typeName = split[0]
}
if appName == "" {
appName = JoinAppNameString(endpoint.GetSource())
}
return p.RootModule.GetApps()[appName].GetTypes()[typeName]
}
func (p *Generator) ExtractTypeInfo(app *sysl.Application, param Param) (appName, typeName, aliasTypeName string, getRecursive bool) {
defer func() {
if err := recover(); err != nil {
p.Log.Errorf("error creating param data model: %s", err)
}
}()
aliasTypeName = param.GetName()
appName, typeName = GetAppTypeName(param)
if aliasTypeName == "" {
aliasTypeName = typeName
}
if appName == "" {
appName = path.Join(app.GetName().GetPart()...)
}
if appName == "primitive" {
getRecursive = false
} else {
getRecursive = true
}
return
}
func (p *Generator) ExtractReturnInfo(appname string, stmnt *sysl.Statement, endpoint *sysl.Endpoint) (appName string, typeName string, typeref *sysl.Type, getRecursive bool) {
var sequence bool
appName = appname
ret := stmnt.GetRet()
if ret == nil {
return
}
t := strings.ReplaceAll(ofTypeSymbol.FindString(ret.Payload), "<: ", "")
if strings.Contains(t, "sequence of") {
t = strings.ReplaceAll(t, "sequence of ", "")
sequence = true
}
if split := strings.Split(t, "."); len(split) > 1 {
appName = split[0]
typeName = split[1]
} else {
appName = appname
typeName = split[0]
}
if sequence {
newSequenceName := endpoint.GetName() + "ReturnVal"
newAppName := appname
defer delete(p.RootModule.GetApps()[newAppName].GetTypes(), newSequenceName)
p.RootModule.GetApps()[newAppName].GetTypes()[newSequenceName] = &sysl.Type{
Type: &sysl.Type_Tuple_{
Tuple: &sysl.Type_Tuple{
AttrDefs: map[string]*sysl.Type{"sequence": {Type: &sysl.Type_Sequence{
Sequence: newsysl.Type(typeName, appName)},
},
},
},
},
}
typeref = NewTypeRef(newAppName, newSequenceName)
} else {
typeref = NewTypeRef(appName, typeName)
}
if _, ok := p.RootModule.Apps[appName]; !ok {
return
}
if syslwrapper.IsPrimitive(typeName) {
getRecursive = false
typeref = syslwrapper.MakePrimitive(typeName)
} else {
getRecursive = true
}
return
}
// CreateFileName returns the absolute and relative filepaths
func CreateFileName(dir string, elems ...string) (string, string) {
var absolutefilePath, filename string
for i, e := range elems {
if i == len(elems)-1 {
filename = strings.ToLower(SanitiseOutputName(e))
absolutefilePath = path.Join(absolutefilePath, filename)
break
}
absolutefilePath = path.Join(absolutefilePath, SanitiseOutputName(e))
}
if dir != "" {
dir += "/"
}
return path.Join(dir, absolutefilePath), dir
}
func (p *Generator) getProjectApp(m *sysl.Module) (*sysl.Application, map[string]string) {
includedProjects := Filter(
SortedKeys(m.Apps),
func(i string) bool {
if !syslutil.HasPattern(m.GetApps()[i].GetAttrs(), "project") {
return false
}
for _, ctx := range m.GetApps()[i].SourceContexts {
if path.Base(ctx.File) == path.Base(p.SourceFileName) {
return true
}
}
return false
},
)
if len(includedProjects) > 0 {
set := make(map[string]string)
app := m.GetApps()[includedProjects[0]]
for _, e := range app.GetEndpoints() {
if syslutil.HasPattern(e.GetAttrs(), "ignore") {
continue
}
for _, e2 := range e.GetStmt() {
set[e2.GetAction().GetAction()] = e.Name
}
}
return m.GetApps()[includedProjects[0]], set
}
return nil, nil
}
// ModuleAsMacroPackage returns "macro packages" that map to the endpoints on the "project" application
/*
project[~project]: <-- first map
FirstDivision: <-- ["FirstDivision"]:
package1 <-- all apps in this package will be contained in this module
package2 <-- same with this one
SecondDivision: <-- ["SecondDivision"]:
package3 <-- You get the point
*/
func (p *Generator) ModuleAsMacroPackage(m *sysl.Module) map[string]*sysl.Module {
packages := make(map[string]*sysl.Module)
_, includedProjects := p.getProjectApp(m)
for _, key := range SortedKeys(m.GetApps()) {
app := m.GetApps()[key]
packageName := GetPackageName(p.RootModule, app)
if packageName == "" {
packageName = key
}
// what endpoint on the "project app" are we on?
projectEndpoint, ok := includedProjects[packageName]
if len(includedProjects) > 0 && !ok || (projectEndpoint == "") {
continue
}
if syslutil.HasPattern(app.GetAttrs(), "ignore") || syslutil.HasPattern(app.GetAttrs(), "project") {
continue
}
if _, ok := packages[projectEndpoint]; !ok {
packages[projectEndpoint] = newsysl.Module()
}
if _, ok := packages[projectEndpoint]; !ok {
packages[projectEndpoint] = &sysl.Module{Apps: map[string]*sysl.Application{}}
}
packages[projectEndpoint].GetApps()[GetAppNameString(app)] = app
}
return packages
}
// MacroPackages executes the markdown for a MacroPackage and returns a slice of the rows
func (p *Generator) MacroPackages(module *sysl.Module) []string {
defer p.resetTempVars()
MacroPackages := p.ModuleAsMacroPackage(module)
for macroPackageName, macroPackage := range MacroPackages {
fileName := markdownName(p.OutputFileName, macroPackageName)
macroPackageFileName := path.Join(p.OutputDir, macroPackageName, fileName)
p.CurrentDir = macroPackageName
p.TempDir = macroPackageName // this is for p.Packages()
p.Title = macroPackageName
p.Links = map[string]string{
"Back": "../" + p.OutputFileName,
}
newGenerator := *p
newGenerator.Module = macroPackage
err := newGenerator.CreateMarkdown(newGenerator.Templates[1], macroPackageFileName, newGenerator)
if err != nil {
p.Log.Error("Error generating project table:", err)
os.Exit(1)
}
}
return SortedKeys(MacroPackages)
}
func (p *Generator) resetTempVars() {
p.CurrentDir = p.TempDir
p.TempDir = ""
}
// Packages executes the markdown for a package and returns a slice of the rows
func (p *Generator) Packages(m *sysl.Module) []string {
defer p.resetTempVars()
MacroPackages := p.ModuleAsPackages(m)
for packageName, pkg := range MacroPackages {
p.CurrentDir = path.Join(p.TempDir, packageName)
fileName := markdownName(p.OutputFileName, packageName)
fullOutputName := path.Join(p.OutputDir, p.CurrentDir, fileName)
// Add a synthetic package app to make the packageName available (unless the name is used).
for _, app := range pkg.Apps {
if app.Attrs == nil {
app.Attrs = make(map[string]*sysl.Attribute, 1)
}
app.Attrs[macropackage_name] = &sysl.Attribute{Attribute: &sysl.Attribute_S{S: packageName}}
}
if err := p.CreateMarkdown(p.Templates[len(p.Templates)-1], fullOutputName, pkg); err != nil {
p.Log.Error("error in generating "+fullOutputName, err)
}
}
return SortedKeys(MacroPackages)
}
// ModuleAsPackages returns a map of [packagename]*sysl.Module
func (p *Generator) ModuleAsPackages(m *sysl.Module) map[string]*sysl.Module {
packages := make(map[string]*sysl.Module)
_, includedProjects := p.getProjectApp(m)
for _, key := range SortedKeys(m.GetApps()) {
app := m.GetApps()[key]
packageName, _ := GetAppPackageName(app)
if packageName == "" {
packageName = key
}
ns := strings.Join(app.Name.Part[:len(app.Name.Part)-1], namespaceSeparator)
if len(includedProjects) > 0 {
if _, ok := includedProjects[ns]; !ok {
if _, ok := includedProjects[packageName]; !ok {
continue
}
}
}
packageName = GetPackageName(p.RootModule, app)
if syslutil.HasPattern(app.GetAttrs(), "ignore") || syslutil.HasPattern(app.GetAttrs(), "project") {
continue
}
if _, ok := packages[packageName]; !ok {
packages[packageName] = &sysl.Module{Apps: map[string]*sysl.Application{}}
}
packages[packageName].GetApps()[GetAppNameString(app)] = app
}
return packages
}