From bac0507c6e84513e0104618624f98b6be252c194 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 28 Oct 2020 19:37:09 +1100 Subject: [PATCH] Add more template functions (#78) --- pkg/catalog/create_diagram.go | 7 ++++++ pkg/catalog/generator.go | 2 ++ pkg/catalog/util.go | 33 +++++++++++++++++++++----- pkg/catalog/util_test.go | 44 +++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/pkg/catalog/create_diagram.go b/pkg/catalog/create_diagram.go index b4dccfe..03e2f19 100644 --- a/pkg/catalog/create_diagram.go +++ b/pkg/catalog/create_diagram.go @@ -232,6 +232,13 @@ func (p *Generator) Packages(m *sysl.Module) []string { 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) } diff --git a/pkg/catalog/generator.go b/pkg/catalog/generator.go index 9306b99..06d4331 100644 --- a/pkg/catalog/generator.go +++ b/pkg/catalog/generator.go @@ -256,12 +256,14 @@ func (p *Generator) GetFuncMap() template.FuncMap { "hasPattern": syslutil.HasPattern, "ModuleAsPackages": p.ModuleAsPackages, "ModulePackageName": ModulePackageName, + "ModuleNamespace": ModuleNamespace, "SortedKeys": SortedKeys, "Attribute": Attribute, "ServiceMetadata": ServiceMetadata, "Fields": Fields, "FieldType": FieldType, "SanitiseOutputName": SanitiseOutputName, + "SimpleName": SimpleName, "ToLower": strings.ToLower, "ToCamel": strcase.ToCamel, "Remove": Remove, diff --git a/pkg/catalog/util.go b/pkg/catalog/util.go index cf9a32a..04f250c 100644 --- a/pkg/catalog/util.go +++ b/pkg/catalog/util.go @@ -21,6 +21,10 @@ import ( 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, " ", ""), "/", "") @@ -152,7 +156,9 @@ func JoinAppNameString(an *sysl.AppName) string { func GetAppPackageName(a Namer) (string, string) { appName := GetAppNameString(a) packageName := appName - if len(a.GetName().Part) > 1 { + 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) } else if attr := a.GetAttrs()["package"]; attr != nil { packageName = attr.GetS() @@ -170,12 +176,27 @@ func GetPackageName(m *sysl.Module, a Namer) string { } +// 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 { - for _, key := range SortedKeys(m.GetApps()) { - app := m.Apps[key] - return GetPackageName(m, app) - } - return "" + 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 diff --git a/pkg/catalog/util_test.go b/pkg/catalog/util_test.go index 83ff87b..7f66023 100644 --- a/pkg/catalog/util_test.go +++ b/pkg/catalog/util_test.go @@ -70,3 +70,47 @@ ree: assert.Equal(t, exp, ServiceMetadata(m.GetApps()[app])) } } + +func TestSimpleName_Simple(t *testing.T) { + t.Parallel() + + m, err := parse.NewParser().ParseString(` +Foo: + ...`) + require.NoError(t, err) + assert.Equal(t, "Foo", SimpleName(m.Apps["Foo"])) +} + +func TestSimpleName_Namespace(t *testing.T) { + t.Parallel() + + m, err := parse.NewParser().ParseString(` +Foo :: Bar: + ...`) + require.NoError(t, err) + assert.Equal(t, "Bar", SimpleName(m.Apps["Foo :: Bar"])) +} + +func TestModuleNamespace(t *testing.T) { + t.Parallel() + + m, err := parse.NewParser().ParseString(` +Foo :: Bar :: Baz: + ...`) + require.NoError(t, err) + assert.Equal(t, "Foo :: Bar", ModuleNamespace(m)) +} + +func TestModulePackage_Alias(t *testing.T) { + t.Parallel() + + m, err := parse.NewParser().ParseString(` +Foo :: Bar :: Baz: + ... + +Foo :: Bar: + @package_alias = "Qux" +`) + require.NoError(t, err) + assert.Equal(t, "Qux", ModulePackageName(m)) +}