Skip to content

Commit

Permalink
feat: add urlencode & urldecode
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Oct 4, 2023
1 parent dd952f5 commit c89876c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func CreateFuncs(ctx context.Context) template.FuncMap {
addToMap(f, funcs.CreateCollFuncs(ctx))
addToMap(f, funcs.CreateUUIDFuncs(ctx))
addToMap(f, funcs.CreateRandomFuncs(ctx))
addToMap(f, funcs.CreateEncodeFuncs(ctx))
return f
}

Expand Down
2 changes: 2 additions & 0 deletions funcs/cel_gen_exports.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions funcs/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package funcs

import (
"context"
"net/url"
)

type EncodeFuncs struct {
}

func CreateEncodeFuncs(ctx context.Context) map[string]interface{} {
f := map[string]interface{}{}

ns := &EncodeFuncs{}
f["urlencode"] = ns.URLEncode
f["urldecode"] = ns.URLDecode
return f
}

func (t EncodeFuncs) URLEncode(input string) string {
return url.QueryEscape(input)
}

func (t EncodeFuncs) URLDecode(input string) (string, error) {
return url.QueryUnescape(input)
}
35 changes: 35 additions & 0 deletions funcs/encode_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package funcs

import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
)

var urlEncodeGen = cel.Function("urlencode",
cel.Overload("urlencode.string",
[]*cel.Type{
cel.StringType,
},
cel.StringType,
cel.UnaryBinding(func(arg ref.Val) ref.Val {
var x EncodeFuncs
result := x.URLEncode(arg.Value().(string))
return types.String(result)
}),
),
)

var urlDecodeGen = cel.Function("urldecode",
cel.Overload("urldecode.string",
[]*cel.Type{
cel.StringType,
},
cel.StringType,
cel.UnaryBinding(func(arg ref.Val) ref.Val {
var x EncodeFuncs
result, _ := x.URLDecode(arg.Value().(string))
return types.String(result)
}),
),
)
4 changes: 4 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func TestGomplate(t *testing.T) {
out string
}{
{map[string]interface{}{"hello": "world"}, "{{ .hello }}", "world"},
{map[string]interface{}{"hello": "hello world ?"}, "{{ .hello | urlencode }}", `hello+world+%3F`},
{map[string]interface{}{"hello": "hello+world+%3F"}, "{{ .hello | urldecode }}", `hello world ?`},
{map[string]interface{}{"age": 75 * time.Second}, "{{ .age | humanDuration }}", "1m15s"},
{map[string]interface{}{"healthySvc": k8s.GetUnstructured(k8s.TestHealthy)}, "{{ (.healthySvc | isHealthy) }}", "true"},
{map[string]interface{}{"healthySvc": k8s.GetUnstructured(k8s.TestLuaStatus)}, "{{ (.healthySvc | getStatus) }}", "Degraded: found less than two generators, Merge requires two or more"},
Expand Down Expand Up @@ -153,6 +155,8 @@ func TestCel(t *testing.T) {
}{
{nil, `math.Add([1,2,3,4,5])`, "15"},
{map[string]interface{}{"hello": "world"}, "hello", "world"},
{map[string]interface{}{"hello": "hello world ?"}, "urlencode(hello)", `hello+world+%3F`},
{map[string]interface{}{"hello": "hello+world+%3F"}, "urldecode(hello)", `hello world ?`},
{map[string]interface{}{"age": 75 * time.Second}, "age", "1m15s"},
{map[string]interface{}{"healthySvc": k8s.GetUnstructuredMap(k8s.TestHealthy)}, "IsHealthy(healthySvc)", "true"},
{map[string]interface{}{"healthySvc": k8s.GetUnstructuredMap(k8s.TestLuaStatus)}, "GetStatus(healthySvc)", "Degraded: found less than two generators, Merge requires two or more"},
Expand Down

0 comments on commit c89876c

Please sign in to comment.