Skip to content

Commit 255579b

Browse files
committed
feat: add urlEncode and urlDecode functions to template rendering
1 parent 09fbe6d commit 255579b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pkg/render/template.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"fmt"
2929
"io"
3030
mathrand "math/rand"
31+
"net/url"
3132
"strings"
3233
"text/template"
3334
"time"
@@ -224,6 +225,17 @@ var advancedFuncs = []AdvancedFunc{{
224225
Func: func() float64 {
225226
return time.Since(uptime).Seconds()
226227
},
228+
}, {
229+
FuncName: "urlEncode",
230+
Func: func(text string) string {
231+
return url.QueryEscape(text)
232+
},
233+
}, {
234+
FuncName: "urlDecode",
235+
Func: func(text string) (result string) {
236+
result, _ = url.QueryUnescape(text)
237+
return
238+
},
227239
}}
228240

229241
// WeightEnum is a weight enum

pkg/render/template_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ func TestRender(t *testing.T) {
127127
verify: func(t *testing.T, s string) {
128128
assert.NotEmpty(t, s)
129129
},
130+
}, {
131+
name: "url encode",
132+
text: `{{urlEncode "hello world"}}`,
133+
verify: func(t *testing.T, s string) {
134+
assert.Equal(t, "hello+world", s)
135+
},
136+
}, {
137+
name: "url decode",
138+
text: `{{urlDecode "hello+world"}}`,
139+
verify: func(t *testing.T, s string) {
140+
assert.Equal(t, "hello world", s)
141+
},
130142
}}
131143
for _, tt := range tests {
132144
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)