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

feat(sdk): ternary, urlencode, dirname, basename interpolate helper #6057

Merged
merged 5 commits into from
Jan 28, 2022
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
4 changes: 4 additions & 0 deletions sdk/interpolate/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func Do(input string, vars map[string]string) (string, error) {
if _, ok := usedHelpers["default"]; ok {
defaultIsUsed = true
}
// case: {{.assert | ternary "foo" "bar"}} when assert is not defined to avoid {{.assert | default false | ternary "foo" "bar"}}
if _, ok := usedHelpers["ternary"]; ok {
defaultIsUsed = true
}

unknownVariables := make([]string, 0, 1000)
for v := range usedVariables {
Expand Down
13 changes: 13 additions & 0 deletions sdk/interpolate/interpolate_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"path"
"reflect"
"strings"
"text/template"
Expand Down Expand Up @@ -71,6 +73,10 @@ func init() {
}
return a
},
"ternary": ternary,
"urlencode": func(s string) string { return url.QueryEscape(s) },
"dirname": func(s string) string { return path.Dir(s) },
"basename": func(s string) string { return path.Base(s) },
})
}

Expand Down Expand Up @@ -369,6 +375,13 @@ func escape(s string) string {
return s1
}

func ternary(v, v2, a interface{}) interface{} {
if cast.ToBool(a) {
return v
}
return v2
}

// toInt64 converts integer types to 64-bit integers
func toInt64(v interface{}) int64 {
return cast.ToInt64(v)
Expand Down
105 changes: 105 additions & 0 deletions sdk/interpolate/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,111 @@ func TestDo(t *testing.T) {
want: "my value 4 4",
enable: true,
},
{
name: "dirname",
args: args{
input: "{{.path | dirname}}",
vars: map[string]string{
"path": "/a/b/c",
},
},
want: "/a/b",
enable: true,
},
{
name: "basename",
args: args{
input: "{{.path | basename}}",
vars: map[string]string{
"path": "/ab/c",
},
},
want: "c",
enable: true,
},
{
name: "urlencode word",
args: args{
input: "{{.query | urlencode}}",
vars: map[string]string{
"query": "Trollhättan",
},
},
want: "Trollh%C3%A4ttan",
enable: true,
},
{
name: "urlencode query",
args: args{
input: "{{.query | urlencode}}",
vars: map[string]string{
"query": "zone:eq=Somewhere over the rainbow&name:like=%mydomain.localhost.local",
},
},
want: "zone%3Aeq%3DSomewhere+over+the+rainbow%26name%3Alike%3D%25mydomain.localhost.local",
enable: true,
},
{
name: "urlencode nothing to do",
args: args{
input: "{{.query | urlencode}}",
vars: map[string]string{
"query": "patrick",
},
},
want: "patrick",
enable: true,
},
{
name: "ternary truthy",
args: args{
input: "{{.assert | ternary .foo .bar}}",
vars: map[string]string{
"assert": "true",
"bar": "bar",
"foo": "foo",
},
},
want: "foo",
enable: true,
},
{
name: "ternary truthy integer",
args: args{
input: "{{ \"1\" | ternary .foo .bar}}",
vars: map[string]string{
"bar": "bar",
"foo": "foo",
},
},
want: "foo",
enable: true,
},
{
name: "ternary falsy",
args: args{
input: "{{.assert | ternary .foo .bar}}",
vars: map[string]string{
"assert": "false",
"bar": "bar",
"foo": "foo",
},
},
want: "bar",
enable: true,
},
{
name: "ternary undef assert",
args: args{
input: "{{.assert | ternary .foo .bar}}",
vars: map[string]string{
"bar": "bar",
"foo": "foo",
},
},
want: "bar",
enable: true,
},
}
for _, tt := range tests {
if !tt.enable {
Expand Down