@@ -11,40 +11,28 @@ import (
11
11
"unicode/utf8"
12
12
)
13
13
14
- // ErrWrongSyntax represents a wrong syntax with a tempate
14
+ // ErrWrongSyntax represents a wrong syntax with a template
15
15
type ErrWrongSyntax struct {
16
16
Template string
17
17
}
18
18
19
19
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 )
21
21
}
22
22
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 {
31
25
Template string
32
26
Var string
33
27
}
34
28
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 )
43
31
}
44
32
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
46
34
// 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 ) {
48
36
// in the future, if necessary, we can introduce some escape-char,
49
37
// for example: it will use `#' as a reversed char, templates will use `{#{}` to do escape and output char '{'.
50
38
var buf strings.Builder
@@ -90,12 +78,12 @@ func Expand(template string, match map[string]string) (string, error) {
90
78
buf .WriteString (part )
91
79
} else {
92
80
// look up in the map
93
- if val , ok := match [key ]; ok {
81
+ if val , ok := vars [key ]; ok {
94
82
buf .WriteString (val )
95
83
} else {
96
84
// write the non-existing var as it is
97
85
buf .WriteString (part )
98
- err = ErrNoMatchedVar {Template : template , Var : key }
86
+ err = ErrVarMissing {Template : template , Var : key }
99
87
}
100
88
}
101
89
}
0 commit comments