Skip to content

Commit b9c1142

Browse files
committed
fine tune Expand behavior
1 parent d99b308 commit b9c1142

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

modules/templates/vars/vars.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package vars
77
import (
88
"fmt"
99
"strings"
10+
"unicode"
11+
"unicode/utf8"
1012
)
1113

1214
// ErrWrongSyntax represents a wrong syntax with a tempate
@@ -40,9 +42,11 @@ func IsErrNoMatchedVar(err error) bool {
4042
return ok
4143
}
4244

43-
// Expand replaces all variables like {var} to match, if error occurs, the error part doesn't change and is returned as it is.
44-
// `#' is a reversed char, templates can use `{#{}` to do escape and output char '{'.
45+
// Expand replaces all variables like {var} to match, if error occurs,
46+
// the error part doesn't change and is returned as it is.
4547
func Expand(template string, match map[string]string) (string, error) {
48+
// in the future, if necessary, we can introduce some escape-char,
49+
// for example: it will use `#' as a reversed char, templates will use `{#{}` to do escape and output char '{'.
4650
var buf strings.Builder
4751
var err error
4852

@@ -84,9 +88,10 @@ func Expand(template string, match map[string]string) (string, error) {
8488
} else {
8589
// now we get a valid key "{...}"
8690
key := part[1 : len(part)-1]
87-
if key[0] == '#' {
88-
// escaped char
89-
buf.WriteString(key[1:])
91+
keyFirst, _ := utf8.DecodeRuneInString(key)
92+
if unicode.IsSpace(keyFirst) || unicode.IsPunct(keyFirst) || unicode.IsControl(keyFirst) {
93+
// the if key doesn't start with a letter, then we do not treat it as a var now
94+
buf.WriteString(part)
9095
} else {
9196
// look up in the map
9297
if val, ok := match[key]; ok {

modules/templates/vars/vars_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,61 @@ import (
1212

1313
func TestExpandVars(t *testing.T) {
1414
kases := []struct {
15-
template string
16-
maps map[string]string
17-
expected string
18-
fail bool
15+
tmpl string
16+
data map[string]string
17+
out string
18+
error bool
1919
}{
2020
{
21-
template: "{a}",
22-
maps: map[string]string{
21+
tmpl: "{a}",
22+
data: map[string]string{
2323
"a": "1",
2424
},
25-
expected: "1",
25+
out: "1",
2626
},
2727
{
28-
template: "expand {a}, {b} and {c}, with escaped {#{}",
29-
maps: map[string]string{
28+
tmpl: "expand {a}, {b} and {c}, with non-var { } {#}",
29+
data: map[string]string{
3030
"a": "1",
3131
"b": "2",
3232
"c": "3",
3333
},
34-
expected: "expand 1, 2 and 3, with escaped {",
34+
out: "expand 1, 2 and 3, with non-var { } {#}",
3535
},
3636
{
37-
template: "中文内容 {一}, {二} 和 {三} 中文结尾",
38-
maps: map[string]string{
37+
tmpl: "中文内容 {一}, {二} 和 {三} 中文结尾",
38+
data: map[string]string{
3939
"一": "11",
4040
"二": "22",
4141
"三": "33",
4242
},
43-
expected: "中文内容 11, 22 和 33 中文结尾",
43+
out: "中文内容 11, 22 和 33 中文结尾",
4444
},
4545
{
46-
template: "expand {{a}, {b} and {c}",
47-
maps: map[string]string{
46+
tmpl: "expand {{a}, {b} and {c}",
47+
data: map[string]string{
4848
"a": "foo",
4949
"b": "bar",
5050
},
51-
expected: "expand {{a}, bar and {c}",
52-
fail: true,
51+
out: "expand {{a}, bar and {c}",
52+
error: true,
5353
},
5454
{
55-
template: "expand } {} and {",
56-
expected: "expand } {} and {",
57-
fail: true,
55+
tmpl: "expand } {} and {",
56+
out: "expand } {} and {",
57+
error: true,
5858
},
5959
}
6060

6161
for _, kase := range kases {
62-
t.Run(kase.template, func(t *testing.T) {
63-
res, err := Expand(kase.template, kase.maps)
64-
if kase.fail {
62+
t.Run(kase.tmpl, func(t *testing.T) {
63+
res, err := Expand(kase.tmpl, kase.data)
64+
if kase.error {
6565
assert.Error(t, err)
6666
} else {
6767
assert.NoError(t, err)
6868
}
69-
assert.EqualValues(t, kase.expected, res)
69+
assert.EqualValues(t, kase.out, res)
7070
})
7171
}
7272
}

0 commit comments

Comments
 (0)