Skip to content

Commit

Permalink
Merge pull request #37 from k1LoW/fix-stringify
Browse files Browse the repository at this point in the history
Fix ExprRepFn to stringify correctly.
  • Loading branch information
k1LoW authored Oct 9, 2023
2 parents 5e08d72 + c442a6f commit 3de5d0c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
7 changes: 3 additions & 4 deletions repfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func InterpolateRepFn(mapping func(string) (string, bool)) repFn {
}
}

var numberRe = regexp.MustCompile(`^[+-]?\d+(?:\.\d+)?$`)

func ExprRepFn(delimStart, delimEnd string, env any) repFn {
const strDQuote = `"`
return func(in string) (string, error) {
Expand Down Expand Up @@ -65,8 +63,9 @@ func ExprRepFn(delimStart, delimEnd string, env any) repFn {
switch v := o.(type) {
case string:
// Stringify only one expression.
if strings.TrimSpace(in) == m[0] && numberRe.MatchString(v) {
s = fmt.Sprintf("'%s'", v)
stat := getNumberStat(v)
if strings.TrimSpace(in) == m[0] && stat.isNum {
s = fmt.Sprintf("%q", v)
} else {
s = v
}
Expand Down
47 changes: 46 additions & 1 deletion repfn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,52 @@ world`,
"hello": "-3",
},
`"{{ hello }}"`,
`'-3'`,
`"-3"`,
},
{
"{{",
"}}",
map[string]any{
"hello": "0o777",
},
`"{{ hello }}"`,
`"0o777"`,
},
{
"{{",
"}}",
map[string]any{
"hello": 0o777,
},
`"{{ hello }}"`,
`511`,
},
{
"{{",
"}}",
map[string]any{
"hello": -3.4,
},
`"{{ hello }}"`,
`-3.4`,
},
{
"{{",
"}}",
map[string]any{
"hello": false,
},
`"{{ hello }}"`,
`false`,
},
{
"{{",
"}}",
map[string]any{
"hello": "false",
},
`"{{ hello }}"`,
`false`,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 3de5d0c

Please sign in to comment.