Skip to content

Commit df7bcf9

Browse files
feat(sdk): ternary, urlencode, dirname, basename interpolate helper (#6057)
Signed-off-by: Louis-Quentin <[email protected]>
1 parent 59f84be commit df7bcf9

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

sdk/interpolate/interpolate.go

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ func Do(input string, vars map[string]string) (string, error) {
129129
if _, ok := usedHelpers["default"]; ok {
130130
defaultIsUsed = true
131131
}
132+
// case: {{.assert | ternary "foo" "bar"}} when assert is not defined to avoid {{.assert | default false | ternary "foo" "bar"}}
133+
if _, ok := usedHelpers["ternary"]; ok {
134+
defaultIsUsed = true
135+
}
132136

133137
unknownVariables := make([]string, 0, 1000)
134138
for v := range usedVariables {

sdk/interpolate/interpolate_helper.go

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"encoding/base64"
99
"encoding/json"
1010
"fmt"
11+
"net/url"
12+
"path"
1113
"reflect"
1214
"strings"
1315
"text/template"
@@ -71,6 +73,10 @@ func init() {
7173
}
7274
return a
7375
},
76+
"ternary": ternary,
77+
"urlencode": func(s string) string { return url.QueryEscape(s) },
78+
"dirname": func(s string) string { return path.Dir(s) },
79+
"basename": func(s string) string { return path.Base(s) },
7480
})
7581
}
7682

@@ -369,6 +375,13 @@ func escape(s string) string {
369375
return s1
370376
}
371377

378+
func ternary(v, v2, a interface{}) interface{} {
379+
if cast.ToBool(a) {
380+
return v
381+
}
382+
return v2
383+
}
384+
372385
// toInt64 converts integer types to 64-bit integers
373386
func toInt64(v interface{}) int64 {
374387
return cast.ToInt64(v)

sdk/interpolate/interpolate_test.go

+105
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,111 @@ func TestDo(t *testing.T) {
446446
want: "my value 4 4",
447447
enable: true,
448448
},
449+
{
450+
name: "dirname",
451+
args: args{
452+
input: "{{.path | dirname}}",
453+
vars: map[string]string{
454+
"path": "/a/b/c",
455+
},
456+
},
457+
want: "/a/b",
458+
enable: true,
459+
},
460+
{
461+
name: "basename",
462+
args: args{
463+
input: "{{.path | basename}}",
464+
vars: map[string]string{
465+
"path": "/ab/c",
466+
},
467+
},
468+
want: "c",
469+
enable: true,
470+
},
471+
{
472+
name: "urlencode word",
473+
args: args{
474+
input: "{{.query | urlencode}}",
475+
vars: map[string]string{
476+
"query": "Trollhättan",
477+
},
478+
},
479+
want: "Trollh%C3%A4ttan",
480+
enable: true,
481+
},
482+
{
483+
name: "urlencode query",
484+
args: args{
485+
input: "{{.query | urlencode}}",
486+
vars: map[string]string{
487+
"query": "zone:eq=Somewhere over the rainbow&name:like=%mydomain.localhost.local",
488+
},
489+
},
490+
want: "zone%3Aeq%3DSomewhere+over+the+rainbow%26name%3Alike%3D%25mydomain.localhost.local",
491+
enable: true,
492+
},
493+
{
494+
name: "urlencode nothing to do",
495+
args: args{
496+
input: "{{.query | urlencode}}",
497+
vars: map[string]string{
498+
"query": "patrick",
499+
},
500+
},
501+
want: "patrick",
502+
enable: true,
503+
},
504+
{
505+
name: "ternary truthy",
506+
args: args{
507+
input: "{{.assert | ternary .foo .bar}}",
508+
vars: map[string]string{
509+
"assert": "true",
510+
"bar": "bar",
511+
"foo": "foo",
512+
},
513+
},
514+
want: "foo",
515+
enable: true,
516+
},
517+
{
518+
name: "ternary truthy integer",
519+
args: args{
520+
input: "{{ \"1\" | ternary .foo .bar}}",
521+
vars: map[string]string{
522+
"bar": "bar",
523+
"foo": "foo",
524+
},
525+
},
526+
want: "foo",
527+
enable: true,
528+
},
529+
{
530+
name: "ternary falsy",
531+
args: args{
532+
input: "{{.assert | ternary .foo .bar}}",
533+
vars: map[string]string{
534+
"assert": "false",
535+
"bar": "bar",
536+
"foo": "foo",
537+
},
538+
},
539+
want: "bar",
540+
enable: true,
541+
},
542+
{
543+
name: "ternary undef assert",
544+
args: args{
545+
input: "{{.assert | ternary .foo .bar}}",
546+
vars: map[string]string{
547+
"bar": "bar",
548+
"foo": "foo",
549+
},
550+
},
551+
want: "bar",
552+
enable: true,
553+
},
449554
}
450555
for _, tt := range tests {
451556
if !tt.enable {

0 commit comments

Comments
 (0)