mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 04:16:25 +08:00
Fix backslash escaping in placeholder value escaping
This commit is contained in:
@@ -650,7 +650,8 @@ Currently supported placeholders are:
|
||||
Before replacing the placeholders in the process config, all references (see below) will be resolved.
|
||||
|
||||
If the value that gets filled in on the place of the placeholder needs escaping, you can define the character to be escaped in the placeholder by adding it to the placeholder name and prefix it with a `^`.
|
||||
E.g. escape all `:` in the value (`http://example.com:8080`) for `{memfs}` placeholder, write `{memfs^:}`. It will then be replaced by `http\://example.com\:8080`. The escape character is always `\`.
|
||||
E.g. escape all `:` in the value (`http://example.com:8080`) for `{memfs}` placeholder, write `{memfs^:}`. It will then be replaced by `http\://example.com\:8080`. The escape character is always `\`. In
|
||||
case there are `\` in the value, they will also get escaped. If the placeholder doesn't imply escaping, the value will be uses as-is.
|
||||
|
||||
### References
|
||||
|
||||
|
@@ -88,12 +88,13 @@ func replace(what, placeholder, value string) string {
|
||||
|
||||
what = re.ReplaceAllStringFunc(what, func(match string) string {
|
||||
matches := re.FindStringSubmatch(match)
|
||||
var v string
|
||||
v := value
|
||||
|
||||
if matches[2] != "" {
|
||||
v = strings.ReplaceAll(value, matches[2], `\`+matches[2])
|
||||
} else {
|
||||
v = value
|
||||
if matches[2] != `\` {
|
||||
v = strings.ReplaceAll(v, `\`, `\\`)
|
||||
}
|
||||
v = strings.ReplaceAll(v, matches[2], `\`+matches[2])
|
||||
}
|
||||
|
||||
return strings.Replace(match, match, v, 1)
|
||||
|
@@ -7,21 +7,22 @@ import (
|
||||
)
|
||||
|
||||
func TestReplace(t *testing.T) {
|
||||
foobar := `;:.,-_$£!^`
|
||||
foobar := `;:.,-_$\£!^`
|
||||
|
||||
samples := [][2]string{
|
||||
{"{foobar}", foobar},
|
||||
{"{foobar^:}", `;\:.,-_$£!^`},
|
||||
{"{foobar^:}barfoo{foobar^:}", `;\:.,-_$£!^barfoo;\:.,-_$£!^`},
|
||||
{"{foobar^:}", `;\:.,-_$\\£!^`},
|
||||
{"{foobar^:}barfoo{foobar^:}", `;\:.,-_$\\£!^barfoo;\:.,-_$\\£!^`},
|
||||
{"{foobar^:.}", "{foobar^:.}"},
|
||||
{"{foobar^}", "{foobar^}"},
|
||||
{"{barfoo^:}", "{barfoo^:}"},
|
||||
{"{foobar^^}", `;:.,-_$£!\^`},
|
||||
{"{foobar^^}", `;:.,-_$\\£!\^`},
|
||||
{`{foobar^\}`, `;:.,-_$\\£!^`},
|
||||
}
|
||||
|
||||
for _, e := range samples {
|
||||
replaced := replace(e[0], "foobar", foobar)
|
||||
require.Equal(t, e[1], replaced)
|
||||
require.Equal(t, e[1], replaced, e[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user