Skip to content

Commit

Permalink
Add append template function (#841)
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
  • Loading branch information
mikkeloscar authored Jan 20, 2025
1 parent a97d1b6 commit feb4c7a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func renderTemplate(context *templateContext, file string) (string, error) {
"indent": sprig.GenericFuncMap()["indent"],
"dict": dict,
"list": list,
"append": strAppend,
"scaleQuantity": scaleQuantity,
"instanceTypeCPUQuantity": func(instanceType string) (string, error) {
return instanceTypeCPUQuantity(context, instanceType)
Expand Down Expand Up @@ -283,6 +284,11 @@ func list(args ...interface{}) []interface{} {
return args
}

func strAppend(list []string, item string, moreItems ...string) []string {
items := append(list, item)
return append(items, moreItems...)
}

func eksID(id string) string {
parts := strings.Split(id, ":")
return parts[len(parts)-1]
Expand Down
34 changes: 34 additions & 0 deletions provisioner/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,40 @@ func TestJoin(t *testing.T) {
}
}

func TestStrAppend(t *testing.T) {
for _, tc := range []struct {
name string
template string
data interface{}
expected string
}{
{
name: "append to list",
template: `{{ append .Values.data.items .Values.data.item }}`,
data: map[string]interface{}{
"items": []string{"a", "b"},
"item": "c",
},
expected: "[a b c]",
}, {
name: "append multiple items to list",
template: `{{ append .Values.data.items .Values.data.item .Values.data.item2 }}`,
data: map[string]interface{}{
"items": []string{"a", "b"},
"item": "c",
"item2": "d",
},
expected: "[a b c d]",
},
} {
t.Run(tc.name, func(t *testing.T) {
res, err := renderSingle(t, tc.template, tc.data)
require.NoError(t, err)
require.Equal(t, tc.expected, res)
})
}
}

func TestScaleQuantity(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down

0 comments on commit feb4c7a

Please sign in to comment.