Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map and pair helper functions for bundle templates #665

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions libs/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func (err ErrFail) Error() string {
return err.msg
}

type pair struct {
k string
v any
}

var helperFuncs = template.FuncMap{
"fail": func(format string, args ...any) (any, error) {
return nil, ErrFail{fmt.Sprintf(format, args...)}
Expand All @@ -27,4 +32,23 @@ var helperFuncs = template.FuncMap{
"regexp": func(expr string) (*regexp.Regexp, error) {
return regexp.Compile(expr)
},
// A key value pair. This is used with the map function to generate maps
// to use inside a template
"pair": func(k string, v any) pair {
return pair{k, v}
},
// map converts a list of pairs to a map object. This is useful to pass multiple
// objects to templates defined in the library directory. Go text template
// syntax for invoking a template only allows specifying a single argument,
// this function can be used to workaround that limitation.
//
// For example: {{template "my_template" (map (pair "foo" $arg1) (pair "bar" $arg2))}}
// $arg1 and $arg2 can be referred from inside "my_template" as ".foo" and ".bar"
"map": func(pairs ...pair) map[string]any {
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
result := make(map[string]any, 0)
for _, p := range pairs {
result[p.k] = p.v
}
return result
},
}
15 changes: 15 additions & 0 deletions libs/template/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,18 @@ func TestTemplateUrlFunction(t *testing.T) {
assert.Len(t, r.files, 1)
assert.Equal(t, "https://www.databricks.com", string(r.files[0].(*inMemoryFile).content))
}

func TestTemplateMapPairFunction(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()

r, err := newRenderer(ctx, nil, "./testdata/map-pair/template", "./testdata/map-pair/library", tmpDir)

require.NoError(t, err)

err = r.walk()
assert.NoError(t, err)

assert.Len(t, r.files, 1)
assert.Equal(t, "false 123 hello 12.3", string(r.files[0].(*inMemoryFile).content))
}
3 changes: 3 additions & 0 deletions libs/template/testdata/map-pair/library/abc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- define "my_template" -}}
{{- .foo}} {{.bar}} {{.abc}} {{.def -}}
{{- end -}}
1 change: 1 addition & 0 deletions libs/template/testdata/map-pair/template/hello.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{template "my_template" (map (pair "foo" false) (pair "bar" 123) (pair "abc" "hello") (pair "def" 12.3)) -}}