Allow whitespaces in front of key/value pairs in process placeholders

This commit is contained in:
Ingo Oppermann
2023-04-29 08:06:01 +02:00
parent 748bccc3d5
commit 024b5710b5
2 changed files with 8 additions and 4 deletions

View File

@@ -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 // 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 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 // the default value. Any variables in the values will be replaced by their value from the
// vars parameter. // vars parameter.
func (r *replacer) parseParametes(params string, vars map[string]string, defaults map[string]string) map[string]string { 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) p := make(map[string]string)
if len(params) == 0 && len(defaults) == 0 { if len(params) == 0 && len(defaults) == 0 {
@@ -125,6 +127,8 @@ func (r *replacer) parseParametes(params string, vars map[string]string, default
for params != "" { for params != "" {
var key string var key string
key, params, _ = cut([]rune(params), ',') key, params, _ = cut([]rune(params), ',')
key = reSpace.ReplaceAllString(key, "") // Remove all leading spaces
if key == "" { if key == "" {
continue continue
} }

View File

@@ -58,7 +58,7 @@ func TestReplace(t *testing.T) {
require.Equal(t, "", replaced) require.Equal(t, "", replaced)
} }
func TestReplaceInvalid(t *testing.T) { func TestReplaceSpaces(t *testing.T) {
r := New() r := New()
r.RegisterReplaceFunc( r.RegisterReplaceFunc(
"foo:bar", "foo:bar",
@@ -68,8 +68,8 @@ func TestReplaceInvalid(t *testing.T) {
nil, nil,
) )
replaced := r.Replace("{foo:bar, who=World}", "foo:bar", "", nil, nil, "") replaced := r.Replace("{foo:bar, who=World, what=What}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello ! ?", replaced) require.Equal(t, "Hello World! What?", replaced)
} }
func TestReplacerFunc(t *testing.T) { func TestReplacerFunc(t *testing.T) {