From 7149cfeb11de63aac06ca96bc957c70c7e48321c Mon Sep 17 00:00:00 2001 From: Ryooooooga Date: Wed, 18 Jan 2023 20:56:22 +0900 Subject: [PATCH] fix: fix `ReplacePlaceholderString` --- pkg/utils/template.go | 9 ++++++--- pkg/utils/template_test.go | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/utils/template.go b/pkg/utils/template.go index d98a68b99..9b7f544d1 100644 --- a/pkg/utils/template.go +++ b/pkg/utils/template.go @@ -22,9 +22,12 @@ func ResolveTemplate(templateStr string, object interface{}, funcs template.Func // ResolvePlaceholderString populates a template with values func ResolvePlaceholderString(str string, arguments map[string]string) string { + oldnews := make([]string, 0, len(arguments)*4) for key, value := range arguments { - str = strings.Replace(str, "{{"+key+"}}", value, -1) - str = strings.Replace(str, "{{."+key+"}}", value, -1) + oldnews = append(oldnews, + "{{"+key+"}}", value, + "{{."+key+"}}", value, + ) } - return str + return strings.NewReplacer(oldnews...).Replace(str) } diff --git a/pkg/utils/template_test.go b/pkg/utils/template_test.go index f294d115d..236c23278 100644 --- a/pkg/utils/template_test.go +++ b/pkg/utils/template_test.go @@ -53,6 +53,13 @@ func TestResolvePlaceholderString(t *testing.T) { }, "{{}} {{ this }} { should not throw}} an {{{{}}}} error", }, + { + "{{a}}", + map[string]string{ + "a": "X{{.a}}X", + }, + "X{{.a}}X", + }, } for _, s := range scenarios {