From df7bcf90fbb55b6d91c7ad283b7538520458908e Mon Sep 17 00:00:00 2001 From: Quentin Joucla Date: Fri, 28 Jan 2022 15:21:21 +0100 Subject: [PATCH] feat(sdk): ternary, urlencode, dirname, basename interpolate helper (#6057) Signed-off-by: Louis-Quentin --- sdk/interpolate/interpolate.go | 4 + sdk/interpolate/interpolate_helper.go | 13 ++++ sdk/interpolate/interpolate_test.go | 105 ++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) diff --git a/sdk/interpolate/interpolate.go b/sdk/interpolate/interpolate.go index 49b785ea96..5c34c4776c 100644 --- a/sdk/interpolate/interpolate.go +++ b/sdk/interpolate/interpolate.go @@ -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 { diff --git a/sdk/interpolate/interpolate_helper.go b/sdk/interpolate/interpolate_helper.go index 38c09a57e2..46572c9ef2 100644 --- a/sdk/interpolate/interpolate_helper.go +++ b/sdk/interpolate/interpolate_helper.go @@ -8,6 +8,8 @@ import ( "encoding/base64" "encoding/json" "fmt" + "net/url" + "path" "reflect" "strings" "text/template" @@ -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) }, }) } @@ -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) diff --git a/sdk/interpolate/interpolate_test.go b/sdk/interpolate/interpolate_test.go index 6f42f15d97..635e409007 100644 --- a/sdk/interpolate/interpolate_test.go +++ b/sdk/interpolate/interpolate_test.go @@ -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 {