Skip to content

Commit 554886d

Browse files
committed
fine tune
1 parent 6ded737 commit 554886d

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

modules/templates/vars/vars.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func IsErrNoMatchedVar(err error) bool {
4242
return ok
4343
}
4444

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.
45+
// Expand replaces all variables like {var} to `match`, it always returns the expanded string regardless of errors
46+
// if error occurs, the error part doesn't change and is returned as it is.
4747
func Expand(template string, match map[string]string) (string, error) {
4848
// in the future, if necessary, we can introduce some escape-char,
4949
// for example: it will use `#' as a reversed char, templates will use `{#{}` to do escape and output char '{'.
@@ -67,14 +67,10 @@ func Expand(template string, match map[string]string) (string, error) {
6767
posBegin += pos
6868
posEnd := posBegin + 1
6969
for posEnd < strLen {
70-
if template[posEnd] == '#' {
71-
// escape char, skip next
72-
posEnd += 2
73-
continue
74-
} else if template[posEnd] == '}' {
70+
if template[posEnd] == '}' {
7571
posEnd++
7672
break
77-
}
73+
} // in the future, if we need to support escape chars, we can do: if (isEscapeChar) { posEnd+=2 }
7874
posEnd++
7975
}
8076

modules/templates/vars/vars_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ func TestExpandVars(t *testing.T) {
6161
for _, kase := range kases {
6262
t.Run(kase.tmpl, func(t *testing.T) {
6363
res, err := Expand(kase.tmpl, kase.data)
64+
assert.EqualValues(t, kase.out, res)
6465
if kase.error {
6566
assert.Error(t, err)
6667
} else {
6768
assert.NoError(t, err)
6869
}
69-
assert.EqualValues(t, kase.out, res)
7070
})
7171
}
7272
}

0 commit comments

Comments
 (0)