-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
166 lines (137 loc) · 3.97 KB
/
template.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2021 Xelaj Software
//
// This file is a part of path-template package.
// See https://github.com/quenbyako/path-template/blob/master/LICENSE.md for details
package tpl
import (
"fmt"
"regexp"
"strings"
)
// MatchPath extracting path variables with template.
// it returns nil, false, if path doesn't match template
// got example: matchPath("/joinchat/{chat_id}", "/joinchat/abcdefg") returns {"chat_id":"abcdefg"}, false
// spiced up implementaition from https://git.io/Jtcv0 (cuz why not?)
func MatchPath(tpl, path string) (map[string]string, bool) {
// if template doesn't have pattern
if !strings.ContainsAny(tpl, "{}") {
if tpl == path {
return map[string]string{}, true
}
return nil, false
}
tplIsGlobal := strings.HasPrefix(tpl, "/")
pathIsGlobal := strings.HasPrefix(path, "/")
if tplIsGlobal {
if !pathIsGlobal {
return nil, false
}
tpl = strings.TrimLeft(tpl, "/")
}
if pathIsGlobal {
if !tplIsGlobal {
return nil, false
}
path = strings.TrimLeft(path, "/")
}
tplPathItems := strings.Split(tpl, "/")
pathItems := strings.Split(path, "/")
if len(tplPathItems) != len(pathItems) {
return nil, false
}
res := make(map[string]string)
for i, tplPathItem := range tplPathItems {
// if this item not a variable, we just need to check it but don't extract
if !strings.HasPrefix(tplPathItem, "{") || !strings.HasSuffix(tplPathItem, "}") {
if tplPathItem != pathItems[i] {
return nil, false
}
continue
}
// {chat_id} -> chat_id
templateObject := stringsTrimSuffixPrefix("{", tplPathItem, "}")
name := templateObject
result := pathItems[i]
if strings.Contains(templateObject, ":") {
i := strings.Index(templateObject, ":")
name = templateObject[:i]
regexpr := templateObject[i+1:]
if regexpr == "" {
res[name] = result
continue
}
// здесь мы просто точно уверены, что регекс не пустой
// добавляем ^ и $ при необходимости, т.к. матчимся целиком по строчке
if []rune(regexpr)[0] != '^' {
regexpr = "^" + regexpr
}
if []rune(regexpr)[len(regexpr)-1] != '$' {
regexpr += "$"
}
if !regexp.MustCompilePOSIX(regexpr).MatchString(result) {
return nil, false
}
}
res[name] = result
}
return res, true
}
func FillTemplate(tpl string, data map[string]string) (string, error) {
// if template doesn't have pattern
if !strings.ContainsAny(tpl, "{}") {
if len(data) > 0 {
return "", fmt.Errorf("unused keys: [%v]", strings.Join(stringStringMapKeys(data), ", "))
}
return tpl, nil
}
d := make(map[string]string)
for k, v := range data {
d[k] = v
}
var isAbstract bool
// if template or path are not global filepath
if !strings.HasPrefix(tpl, "/") {
isAbstract = true
}
tplPathItems := strings.Split(tpl, "/")
for i, tplPathItem := range tplPathItems {
if !strings.HasPrefix(tplPathItem, "{") || !strings.HasSuffix(tplPathItem, "}") {
continue
}
// {chat_id} -> chat_id
dataKey := stringsTrimSuffixPrefix("{", tplPathItem, "}")
v, ok := d[dataKey]
if !ok {
return "", fmt.Errorf("key '%v' not found", dataKey)
}
tplPathItems[i] = v
delete(d, dataKey)
}
res := strings.Join(tplPathItems, "/")
if !isAbstract {
res = "/" + res
}
if len(d) > 0 {
return "", fmt.Errorf("unused keys: [%v]", strings.Join(stringStringMapKeys(d), ", "))
}
return res, nil
}
func GetTemplateVariables(tpl string) []string {
if !strings.ContainsAny(tpl, "{}") {
return []string{}
}
if !strings.HasPrefix(tpl, "/") {
tpl = strings.TrimLeft(tpl, "/")
}
parts := strings.Split(tpl, "/")
// we divide capacity by 2 cuz most likely wildcards count is less than half of all path parts
res := make([]string, 0, len(parts)/2) //nolint:gomnd // see note above
for _, part := range parts {
if !strings.HasPrefix(part, "{") || !strings.HasSuffix(part, "}") {
continue
}
part = stringsTrimSuffixPrefix("{", part, "}")
res = append(res, part)
}
return res
}