Skip to content

Commit d57054e

Browse files
committed
fine tune
1 parent 554886d commit d57054e

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

modules/templates/vars/vars.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,28 @@ import (
1111
"unicode/utf8"
1212
)
1313

14-
// ErrWrongSyntax represents a wrong syntax with a tempate
14+
// ErrWrongSyntax represents a wrong syntax with a template
1515
type ErrWrongSyntax struct {
1616
Template string
1717
}
1818

1919
func (err ErrWrongSyntax) Error() string {
20-
return fmt.Sprintf("Wrong syntax found in %s", err.Template)
20+
return fmt.Sprintf("wrong syntax found in %s", err.Template)
2121
}
2222

23-
// IsErrWrongSyntax returns true if the error is ErrWrongSyntax
24-
func IsErrWrongSyntax(err error) bool {
25-
_, ok := err.(ErrWrongSyntax)
26-
return ok
27-
}
28-
29-
// ErrNoMatchedVar represents an error that no matched vars
30-
type ErrNoMatchedVar struct {
23+
// ErrVarMissing represents an error that no matched variable
24+
type ErrVarMissing struct {
3125
Template string
3226
Var string
3327
}
3428

35-
func (err ErrNoMatchedVar) Error() string {
36-
return fmt.Sprintf("No matched variable %s found for %s", err.Var, err.Template)
37-
}
38-
39-
// IsErrNoMatchedVar returns true if the error is ErrNoMatchedVar
40-
func IsErrNoMatchedVar(err error) bool {
41-
_, ok := err.(ErrNoMatchedVar)
42-
return ok
29+
func (err ErrVarMissing) Error() string {
30+
return fmt.Sprintf("the variable %s is missing for %s", err.Var, err.Template)
4331
}
4432

45-
// Expand replaces all variables like {var} to `match`, it always returns the expanded string regardless of errors
33+
// Expand replaces all variables like {var} by `vars` map, it always returns the expanded string regardless of errors
4634
// if error occurs, the error part doesn't change and is returned as it is.
47-
func Expand(template string, match map[string]string) (string, error) {
35+
func Expand(template string, vars map[string]string) (string, error) {
4836
// in the future, if necessary, we can introduce some escape-char,
4937
// for example: it will use `#' as a reversed char, templates will use `{#{}` to do escape and output char '{'.
5038
var buf strings.Builder
@@ -90,12 +78,12 @@ func Expand(template string, match map[string]string) (string, error) {
9078
buf.WriteString(part)
9179
} else {
9280
// look up in the map
93-
if val, ok := match[key]; ok {
81+
if val, ok := vars[key]; ok {
9482
buf.WriteString(val)
9583
} else {
9684
// write the non-existing var as it is
9785
buf.WriteString(part)
98-
err = ErrNoMatchedVar{Template: template, Var: key}
86+
err = ErrVarMissing{Template: template, Var: key}
9987
}
10088
}
10189
}

0 commit comments

Comments
 (0)