Skip to content

Commit bac0507

Browse files
authored
Add more template functions (#78)
1 parent 15593f0 commit bac0507

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

pkg/catalog/create_diagram.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ func (p *Generator) Packages(m *sysl.Module) []string {
232232
p.CurrentDir = path.Join(p.TempDir, packageName)
233233
fileName := markdownName(p.OutputFileName, packageName)
234234
fullOutputName := path.Join(p.OutputDir, p.CurrentDir, fileName)
235+
// Add a synthetic package app to make the packageName available (unless the name is used).
236+
for _, app := range pkg.Apps {
237+
if app.Attrs == nil {
238+
app.Attrs = make(map[string]*sysl.Attribute, 1)
239+
}
240+
app.Attrs[macropackage_name] = &sysl.Attribute{Attribute: &sysl.Attribute_S{S: packageName}}
241+
}
235242
if err := p.CreateMarkdown(p.Templates[len(p.Templates)-1], fullOutputName, pkg); err != nil {
236243
p.Log.Error("error in generating "+fullOutputName, err)
237244
}

pkg/catalog/generator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,14 @@ func (p *Generator) GetFuncMap() template.FuncMap {
256256
"hasPattern": syslutil.HasPattern,
257257
"ModuleAsPackages": p.ModuleAsPackages,
258258
"ModulePackageName": ModulePackageName,
259+
"ModuleNamespace": ModuleNamespace,
259260
"SortedKeys": SortedKeys,
260261
"Attribute": Attribute,
261262
"ServiceMetadata": ServiceMetadata,
262263
"Fields": Fields,
263264
"FieldType": FieldType,
264265
"SanitiseOutputName": SanitiseOutputName,
266+
"SimpleName": SimpleName,
265267
"ToLower": strings.ToLower,
266268
"ToCamel": strcase.ToCamel,
267269
"Remove": Remove,

pkg/catalog/util.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121

2222
const namespaceSeparator = " :: "
2323

24+
// macroPackageNameAttr is the name of a synthetic attribute used to pass the name of the macro-
25+
// package to a template.
26+
const macropackage_name = "_macropackage_name"
27+
2428
// SanitiseOutputName removes characters so that the string can be used as a hyperlink.
2529
func SanitiseOutputName(s string) string {
2630
return strings.ReplaceAll(strings.ReplaceAll(s, " ", ""), "/", "")
@@ -152,7 +156,9 @@ func JoinAppNameString(an *sysl.AppName) string {
152156
func GetAppPackageName(a Namer) (string, string) {
153157
appName := GetAppNameString(a)
154158
packageName := appName
155-
if len(a.GetName().Part) > 1 {
159+
if attr := a.GetAttrs()[macropackage_name]; attr != nil {
160+
packageName = attr.GetS()
161+
} else if len(a.GetName().Part) > 1 {
156162
packageName = strings.Join(a.GetName().Part[:len(a.GetName().Part)-1], namespaceSeparator)
157163
} else if attr := a.GetAttrs()["package"]; attr != nil {
158164
packageName = attr.GetS()
@@ -170,12 +176,27 @@ func GetPackageName(m *sysl.Module, a Namer) string {
170176

171177
}
172178

179+
// SimpleName returns the last part of an app name.
180+
func SimpleName(app *sysl.Application) string {
181+
return app.Name.Part[len(app.Name.Part)-1]
182+
}
183+
184+
// ModuleNamespace returns the namespace associated with the module (if the module is grouped by a
185+
// namespace).
186+
func ModuleNamespace(m *sysl.Module) string {
187+
keys := SortedKeys(m.GetApps())
188+
key := keys[len(keys)-1]
189+
app := m.Apps[key]
190+
return strings.Join(app.Name.Part[:len(app.Name.Part)-1], namespaceSeparator)
191+
}
192+
193+
// ModulePackageName returns the package name associated with the module (that of one of its apps).
173194
func ModulePackageName(m *sysl.Module) string {
174-
for _, key := range SortedKeys(m.GetApps()) {
175-
app := m.Apps[key]
176-
return GetPackageName(m, app)
177-
}
178-
return ""
195+
keys := SortedKeys(m.GetApps())
196+
// A package app will be the first.
197+
key := keys[len(keys)-1]
198+
app := m.Apps[key]
199+
return GetPackageName(m, app)
179200
}
180201

181202
// Map applies a function to every element in a string slice

pkg/catalog/util_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,47 @@ ree:
7070
assert.Equal(t, exp, ServiceMetadata(m.GetApps()[app]))
7171
}
7272
}
73+
74+
func TestSimpleName_Simple(t *testing.T) {
75+
t.Parallel()
76+
77+
m, err := parse.NewParser().ParseString(`
78+
Foo:
79+
...`)
80+
require.NoError(t, err)
81+
assert.Equal(t, "Foo", SimpleName(m.Apps["Foo"]))
82+
}
83+
84+
func TestSimpleName_Namespace(t *testing.T) {
85+
t.Parallel()
86+
87+
m, err := parse.NewParser().ParseString(`
88+
Foo :: Bar:
89+
...`)
90+
require.NoError(t, err)
91+
assert.Equal(t, "Bar", SimpleName(m.Apps["Foo :: Bar"]))
92+
}
93+
94+
func TestModuleNamespace(t *testing.T) {
95+
t.Parallel()
96+
97+
m, err := parse.NewParser().ParseString(`
98+
Foo :: Bar :: Baz:
99+
...`)
100+
require.NoError(t, err)
101+
assert.Equal(t, "Foo :: Bar", ModuleNamespace(m))
102+
}
103+
104+
func TestModulePackage_Alias(t *testing.T) {
105+
t.Parallel()
106+
107+
m, err := parse.NewParser().ParseString(`
108+
Foo :: Bar :: Baz:
109+
...
110+
111+
Foo :: Bar:
112+
@package_alias = "Qux"
113+
`)
114+
require.NoError(t, err)
115+
assert.Equal(t, "Qux", ModulePackageName(m))
116+
}

0 commit comments

Comments
 (0)