-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.go
264 lines (235 loc) · 7.24 KB
/
util.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
// util.go: misc functions to convert/send http requests/sort maps
package catalog
import (
"fmt"
"reflect"
"sort"
"strings"
"google.golang.org/protobuf/encoding/protojson"
"github.com/anz-bank/protoc-gen-sysl/newsysl"
"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/anz-bank/sysl/pkg/diagrams"
"github.com/anz-bank/sysl/pkg/sequencediagram"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/sirupsen/logrus"
)
const namespaceSeparator = " :: "
// macroPackageNameAttr is the name of a synthetic attribute used to pass the name of the macro-
// package to a template.
const macropackage_name = "_macropackage_name"
// SanitiseOutputName removes characters so that the string can be used as a hyperlink.
func SanitiseOutputName(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(s, " ", ""), "/", "")
}
func SortedKeys(m interface{}) []string {
keys := reflect.ValueOf(m).MapKeys()
ret := make([]string, 0, len(keys))
for _, v := range keys {
ret = append(ret, v.String())
}
sort.Strings(ret)
return ret
}
// NewTypeRef returns a type reference, needed to correctly generate data model diagrams
func NewTypeRef(appName, typeName string) *sysl.Type {
return &sysl.Type{
Type: &sysl.Type_TypeRef{
TypeRef: &sysl.ScopedRef{
Ref: &sysl.Scope{
Appname: &sysl.AppName{Part: []string{appName}},
Path: []string{typeName},
},
},
},
}
}
// TernaryOperator returns the first element if bool is true and the second element is false
func TernaryOperator(condition bool, i ...interface{}) interface{} {
if condition {
return i[0]
}
return i[1]
}
// createProjectApp returns a "project" app used to make integration diagrams for any "sub module" apps
func createProjectApp(Apps map[string]*sysl.Application) *sysl.Application {
app := newsysl.Application("")
app.Endpoints = make(map[string]*sysl.Endpoint)
app.Endpoints["_"] = newsysl.Endpoint("_")
app.Endpoints["_"].Stmt = []*sysl.Statement{}
for key := range Apps {
app.Endpoints["_"].Stmt = append(app.Endpoints["_"].Stmt, newsysl.StringStatement(key))
}
if app.Attrs == nil {
app.Attrs = make(map[string]*sysl.Attribute)
}
if _, ok := app.Attrs["appfmt"]; !ok {
app.Attrs["appfmt"] = &sysl.Attribute{
Attribute: &sysl.Attribute_S{S: "%(appname)"},
}
}
return app
}
func Attribute(a Attr, query string) string {
if description := a.GetAttrs()[query]; description != nil {
return description.GetS()
}
return ""
}
func ServiceMetadata(a Attr) string {
queries := []string{
"Repo.URL",
"Owner.Email",
"Owner.Slack",
"Server.Prod.URL",
"Server.UAT.URL",
"Lifecycle",
}
queryMap := make(map[string]string)
for _, q := range queries {
queryMap[strings.ToLower(q)] = ""
}
for attrName := range a.GetAttrs() {
q := strings.ToLower(attrName)
if _, exists := queryMap[q]; exists {
queryMap[q] = Attribute(a, attrName)
}
}
metadata := strings.Builder{}
for _, q := range queries {
if val := queryMap[strings.ToLower(q)]; val != "" {
metadata.WriteString(fmt.Sprintf("%s: %s\n\n", q, val))
}
}
return metadata.String()
}
func Fields(t *sysl.Type) map[string]*sysl.Type {
if tuple := t.GetTuple(); tuple != nil {
return tuple.GetAttrDefs()
}
return nil
}
func FieldType(t *sysl.Type) string {
typeName, typeDetail := syslutil.GetTypeDetail(t)
if typeName == "primitive" {
return strings.ToLower(typeDetail)
}
if typeName == "sequence" {
return "sequence of " + typeDetail
}
if typeName == "type_ref" {
return strings.Join(t.GetTypeRef().GetRef().GetPath(), ".")
}
if typeName != "" {
return typeName
}
return typeDetail
}
// GetAppNameString returns an app's name as a string, with the namespace joined on "::".
func GetAppNameString(a Namer) string {
return JoinAppNameString(a.GetName())
}
// JoinAppNameString transforms an AppName to a string, with the namespace joined on "::".
func JoinAppNameString(an *sysl.AppName) string {
return strings.Join(an.GetPart(), namespaceSeparator)
}
// GetAppPackageName returns the package and app name of any sysl application
func GetAppPackageName(a Namer) (string, string) {
appName := GetAppNameString(a)
packageName := appName
if attr := a.GetAttrs()["package"]; attr != nil {
packageName = attr.GetS()
} else if attr := a.GetAttrs()[macropackage_name]; attr != nil {
packageName = attr.GetS()
} else if len(a.GetName().Part) > 1 {
packageName = strings.Join(a.GetName().Part[:len(a.GetName().Part)-1], namespaceSeparator)
}
return packageName, appName
}
func GetPackageName(m *sysl.Module, a Namer) string {
packageName, _ := GetAppPackageName(a)
pkg := m.Apps[packageName]
if attr := pkg.GetAttrs()["package_alias"]; attr != nil {
return attr.GetS()
}
return packageName
}
// SimpleName returns the last part of an app name.
func SimpleName(app *sysl.Application) string {
return app.Name.Part[len(app.Name.Part)-1]
}
// ModuleNamespace returns the namespace associated with the module (if the module is grouped by a
// namespace).
func ModuleNamespace(m *sysl.Module) string {
keys := SortedKeys(m.GetApps())
key := keys[len(keys)-1]
app := m.Apps[key]
return strings.Join(app.Name.Part[:len(app.Name.Part)-1], namespaceSeparator)
}
// ModulePackageName returns the package name associated with the module (that of one of its apps).
func ModulePackageName(m *sysl.Module) string {
keys := SortedKeys(m.GetApps())
// A package app will be the first.
key := keys[len(keys)-1]
app := m.Apps[key]
return GetPackageName(m, app)
}
// Map applies a function to every element in a string slice
func Filter(vs []string, f func(string) bool) []string {
vsm := make([]string, 0, len(vs))
for _, v := range vs {
if f(v) {
vsm = append(vsm, v)
}
}
return vsm
}
// PlantUMLURL returns a PlantUML url
func PlantUMLURL(plantumlService, contents string) string {
encoded, _ := diagrams.DeflateAndEncode([]byte(contents))
return fmt.Sprint(plantumlService, "/", "svg", "/~1", encoded)
}
// CreateSequenceDiagram creates an sequence diagram and returns the sequence diagram string and any errors
func CreateSequenceDiagram(m *sysl.Module, call string) (string, error) {
l := &cmdutils.Labeler{}
p := &sequencediagram.SequenceDiagParam{
AppLabeler: l,
EndpointLabeler: l,
Endpoints: []string{call},
Title: call,
}
return sequencediagram.GenerateSequenceDiag(m, p, logrus.New())
}
// GetAppTypeName takes a Sysl Type and returns the appName and typeName of a param
// If the type is a primitive, the appName returned is "primitive"
func GetAppTypeName(param Typer) (appName string, typeName string) {
ref := param.GetType().GetTypeRef().GetRef()
appNameParts := ref.GetAppname().GetPart()
if a, b := syslutil.GetTypeDetail(param.GetType()); a == "primitive" {
return a, b
}
if len(appNameParts) > 0 {
typeNameParts := ref.GetPath()
if typeNameParts != nil {
appName = JoinAppNameString(ref.GetAppname())
typeName = typeNameParts[0]
} else {
typeName = appNameParts[0]
}
} else {
typeName = ref.GetPath()[0]
}
return appName, typeName
}
// Unmarshall Json unmarshalls json bytes into a sysl module
func UnmarshallJson(b []byte, m *sysl.Module) error {
if m == nil {
return fmt.Errorf("module is nil")
}
ma := protojson.UnmarshalOptions{}
err := ma.Unmarshal(b, m)
if err != nil {
return err
}
return err
}