From 024b5710b5dacefb857a648c98a42be0187b5af5 Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Sat, 29 Apr 2023 08:06:01 +0200 Subject: [PATCH] Allow whitespaces in front of key/value pairs in process placeholders --- restream/replace/replace.go | 6 +++++- restream/replace/replace_test.go | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/restream/replace/replace.go b/restream/replace/replace.go index 5babd0bc..f7771319 100644 --- a/restream/replace/replace.go +++ b/restream/replace/replace.go @@ -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 } diff --git a/restream/replace/replace_test.go b/restream/replace/replace_test.go index 55578846..f69a96f8 100644 --- a/restream/replace/replace_test.go +++ b/restream/replace/replace_test.go @@ -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) {