Skip to content

Commit

Permalink
fix: fix ReplacePlaceholderString
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryooooooga committed Jan 18, 2023
1 parent 48df9b7 commit 7149cfe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pkg/utils/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 7 additions & 0 deletions pkg/utils/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7149cfe

Please sign in to comment.