mirror of
https://github.com/datarhei/core.git
synced 2025-10-17 13:31:51 +08:00
Allow whitespaces in front of key/value pairs in process placeholders
This commit is contained in:
@@ -101,11 +101,13 @@ func (r *replacer) Replace(str, placeholder, value string, vars map[string]strin
|
||||
}
|
||||
|
||||
// parseParametes parses the parameters of a placeholder. The params string is a comma-separated
|
||||
// string of key=value pairs. The key and values can be escaped as in net/url.QueryEscape.
|
||||
// string of key=value pairs. The key and values can be escaped with an \.
|
||||
// The provided defaults will be used as basis. Any parsed key/value from the params might overwrite
|
||||
// the default value. Any variables in the values will be replaced by their value from the
|
||||
// vars parameter.
|
||||
func (r *replacer) parseParametes(params string, vars map[string]string, defaults map[string]string) map[string]string {
|
||||
reSpace := regexp.MustCompile(`^\s+`)
|
||||
|
||||
p := make(map[string]string)
|
||||
|
||||
if len(params) == 0 && len(defaults) == 0 {
|
||||
@@ -125,6 +127,8 @@ func (r *replacer) parseParametes(params string, vars map[string]string, default
|
||||
for params != "" {
|
||||
var key string
|
||||
key, params, _ = cut([]rune(params), ',')
|
||||
key = reSpace.ReplaceAllString(key, "") // Remove all leading spaces
|
||||
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
|
@@ -58,7 +58,7 @@ func TestReplace(t *testing.T) {
|
||||
require.Equal(t, "", replaced)
|
||||
}
|
||||
|
||||
func TestReplaceInvalid(t *testing.T) {
|
||||
func TestReplaceSpaces(t *testing.T) {
|
||||
r := New()
|
||||
r.RegisterReplaceFunc(
|
||||
"foo:bar",
|
||||
@@ -68,8 +68,8 @@ func TestReplaceInvalid(t *testing.T) {
|
||||
nil,
|
||||
)
|
||||
|
||||
replaced := r.Replace("{foo:bar, who=World}", "foo:bar", "", nil, nil, "")
|
||||
require.Equal(t, "Hello ! ?", replaced)
|
||||
replaced := r.Replace("{foo:bar, who=World, what=What}", "foo:bar", "", nil, nil, "")
|
||||
require.Equal(t, "Hello World! What?", replaced)
|
||||
}
|
||||
|
||||
func TestReplacerFunc(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user