Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Mar 8, 2024
1 parent b8e4361 commit 2a9417d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
35 changes: 34 additions & 1 deletion expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func TestReplaceYAMLWithExprRepFn(t *testing.T) {
{
map[string]any{
"hello": map[string]any{
"foo": "ba\nr",
"foo": "bar",
},
},
false,
Expand All @@ -488,6 +488,39 @@ func TestReplaceYAMLWithExprRepFn(t *testing.T) {
`v: '\{\{ {{ hello }} \}\}'`,
`v: '{{ {"foo":"bar"} }}'`,
},
{
map[string]any{
"hello": map[string]any{
"foo": "bar",
},
},
false,
true,
`v: '\\{\{ hello \\}\}'`,
`v: '\{\{ hello \}\}'`,
},
{
map[string]any{
"hello": map[string]any{
"foo": "bar",
},
},
false,
true,
`v: '\\\{\{ hello \\\}\}'`,
`v: '\\{\{ hello \\}\}'`,
},
{
map[string]any{
"hello": map[string]any{
"foo": "bar",
},
},
false,
true,
`v: '\\{\\{ hello \\}\\}'`,
`v: '\\{\\{ hello \\}\\}'`,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions repfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,23 @@ func trySubstr(delimStart, delimEnd, in string) ([]string, int) {
}

func unescapeDelims(delimStart, delimEnd, in string) string {
const (
eeStartToken = "__E_E_DELIM_START__"

Check failure on line 141 in repfn.go

View workflow job for this annotation

GitHub Actions / Test

G101: Potential hardcoded credentials (gosec)
eeEndToken = "__E_E_DELIM_END__"

Check failure on line 142 in repfn.go

View workflow job for this annotation

GitHub Actions / Test

G101: Potential hardcoded credentials (gosec)
)
var (
escapedDelimStart string
escapedDelimEnd string
)
for _, r := range delimStart {
escapedDelimStart += fmt.Sprintf("\\%s", string(r))
}
escapedescapedDelimStart := fmt.Sprintf("\\%s", escapedDelimStart)
for _, r := range delimEnd {
escapedDelimEnd += fmt.Sprintf("\\%s", string(r))
}
rep := strings.NewReplacer(escapedDelimStart, delimStart, escapedDelimEnd, delimEnd)
return rep.Replace(in)
escapedescapedDelimEnd := fmt.Sprintf("\\%s", escapedDelimEnd)
rep := strings.NewReplacer(escapedescapedDelimStart, eeStartToken, escapedescapedDelimEnd, eeEndToken, escapedDelimStart, delimStart, escapedDelimEnd, delimEnd)
rep2 := strings.NewReplacer(eeStartToken, escapedDelimStart, eeEndToken, escapedDelimEnd)
return rep2.Replace(rep.Replace(in))
}

0 comments on commit 2a9417d

Please sign in to comment.