-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
66 lines (62 loc) · 1.28 KB
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package jsonhttpc
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
// Params is a parameters for path templating.
type Params map[string]interface{}
// Path provides path templating.
//
// "{foo}" in `s` are replaced with `fmt.Sprint(p["foo"])`.
// '\' escapes a rune succeeding.
//
// This returns a string with prefix "jsonhttpc.Parse error: " if detects some
// errors.
func Path(s string, p Params) string {
s2, err := path(s, p)
if err != nil {
return fmt.Sprintf("jsonhttpc.Parse error: %s", err)
}
return s2
}
func path(s string, p Params) (string, error) {
b := &strings.Builder{}
for s != "" {
n := strings.IndexAny(s, "\\{")
if n < 0 {
b.WriteString(s)
break
}
if n > 0 {
b.WriteString(s[:n])
s = s[n:]
}
switch s[0] {
case '\\':
if len(s) <= 1 {
return "", errors.New("no chars to escape")
}
r, m := utf8.DecodeRuneInString(s[1:])
if r == utf8.RuneError {
return "", errors.New("rune error to escape")
}
b.WriteRune(r)
s = s[m+1:]
case '{':
m := strings.IndexRune(s, '}')
if m < 0 {
return "", errors.New("not found '}'")
}
k := s[1:m]
v, ok := p[k]
if !ok {
return "", fmt.Errorf("not found key: key=%q", k)
}
b.WriteString(fmt.Sprint(v))
s = s[m+1:]
}
}
return b.String(), nil
}