Skip to content

Commit

Permalink
Add more template functions (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
orlade-anz committed Oct 28, 2020
1 parent 15593f0 commit bac0507
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
7 changes: 7 additions & 0 deletions pkg/catalog/create_diagram.go
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/catalog/generator.go
Expand Up @@ -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,
Expand Down
33 changes: 27 additions & 6 deletions pkg/catalog/util.go
Expand Up @@ -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, " ", ""), "/", "")
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions pkg/catalog/util_test.go
Expand Up @@ -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))
}

0 comments on commit bac0507

Please sign in to comment.