feat: add urlEncode and urlDecode functions to template rendering
This commit is contained in:
parent
09fbe6dae8
commit
255579bd23
|
@ -28,6 +28,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
mathrand "math/rand"
|
||||
"net/url"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
@ -224,6 +225,17 @@ var advancedFuncs = []AdvancedFunc{{
|
|||
Func: func() float64 {
|
||||
return time.Since(uptime).Seconds()
|
||||
},
|
||||
}, {
|
||||
FuncName: "urlEncode",
|
||||
Func: func(text string) string {
|
||||
return url.QueryEscape(text)
|
||||
},
|
||||
}, {
|
||||
FuncName: "urlDecode",
|
||||
Func: func(text string) (result string) {
|
||||
result, _ = url.QueryUnescape(text)
|
||||
return
|
||||
},
|
||||
}}
|
||||
|
||||
// WeightEnum is a weight enum
|
||||
|
|
|
@ -127,6 +127,18 @@ func TestRender(t *testing.T) {
|
|||
verify: func(t *testing.T, s string) {
|
||||
assert.NotEmpty(t, s)
|
||||
},
|
||||
}, {
|
||||
name: "url encode",
|
||||
text: `{{urlEncode "hello world"}}`,
|
||||
verify: func(t *testing.T, s string) {
|
||||
assert.Equal(t, "hello+world", s)
|
||||
},
|
||||
}, {
|
||||
name: "url decode",
|
||||
text: `{{urlDecode "hello+world"}}`,
|
||||
verify: func(t *testing.T, s string) {
|
||||
assert.Equal(t, "hello world", s)
|
||||
},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue