Skip to content

Commit 32b2f74

Browse files
committed
Support $$ for escaping $ in interpolated values.
Fixes #248
1 parent c5e464a commit 32b2f74

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

interpolate.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"regexp"
66
)
77

8-
var interpolationRegex = regexp.MustCompile(`((?:\${([[:alpha:]_][[:word:]]*))(?:=([^}]+))?})|(\$)|([^$]+)`)
8+
var interpolationRegex = regexp.MustCompile(`(\$\$)|((?:\${([[:alpha:]_][[:word:]]*))(?:=([^}]+))?})|(\$)|([^$]+)`)
99

1010
// Interpolate variables from vars into s for substrings in the form ${var} or ${var=default}.
1111
func interpolate(s string, vars Vars, updatedVars map[string]string) (string, error) {
@@ -21,14 +21,16 @@ func interpolate(s string, vars Vars, updatedVars map[string]string) (string, er
2121
}
2222
}
2323
for _, match := range matches {
24-
if name := match[2]; name != "" {
24+
if dollar := match[1]; dollar != "" {
25+
out += "$"
26+
} else if name := match[3]; name != "" {
2527
value, ok := vars[name]
2628
if !ok {
2729
// No default value.
28-
if match[3] == "" {
30+
if match[4] == "" {
2931
return "", fmt.Errorf("undefined variable ${%s}", name)
3032
}
31-
value = match[3]
33+
value = match[4]
3234
}
3335
out += value
3436
} else {

interpolate_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestInterpolate(t *testing.T) {
1313
updatedVars := map[string]string{
1414
"height": "180",
1515
}
16-
actual, err := interpolate("${name=Bobby Brown} is ${age} years old and ${height} cm tall", vars, updatedVars)
16+
actual, err := interpolate("${name=Bobby Brown} is ${age} years old and ${height} cm tall and likes $${AUD}", vars, updatedVars)
1717
require.NoError(t, err)
18-
require.Equal(t, `Bobby Brown is 35 years old and 180 cm tall`, actual)
18+
require.Equal(t, `Bobby Brown is 35 years old and 180 cm tall and likes ${AUD}`, actual)
1919
}

0 commit comments

Comments
 (0)