-
Notifications
You must be signed in to change notification settings - Fork 124
/
tree.go
231 lines (190 loc) · 5.85 KB
/
tree.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package web
import (
"regexp"
"strings"
)
type pathNode struct {
// Given the next segment s, if edges[s] exists, then we'll look there first.
edges map[string]*pathNode
// If set, failure to match on edges will match on wildcard
wildcard *pathNode
// If set, and we have nothing left to match, then we match on this node
leaves []*pathLeaf
// If true, this pathNode has a pathparam that matches the rest of the path
matchesFullPath bool
}
// pathLeaf represents a leaf path segment that corresponds to a single route.
// For the route /admin/forums/:forum_id:\d.*/suggestions/:suggestion_id:\d.*
// We'd have wildcards = ["forum_id", "suggestion_id"]
// and regexps = [/\d.*/, /\d.*/]
// For the route /admin/forums/:forum_id/suggestions/:suggestion_id:\d.*
// We'd have wildcards = ["forum_id", "suggestion_id"]
// and regexps = [nil, /\d.*/]
// For the route /admin/forums/:forum_id/suggestions/:suggestion_id
// We'd have wildcards = ["forum_id", "suggestion_id"]
// and regexps = nil
type pathLeaf struct {
// names of wildcards that lead to this leaf. eg, ["category_id"] for the wildcard ":category_id"
wildcards []string
// regexps corresponding to wildcards. If a segment has regexp contraint, its entry will be nil.
// If the route has no regexp contraints on any segments, then regexps will be nil.
regexps []*regexp.Regexp
// Pointer back to the route
route *route
// If true, this leaf has a pathparam that matches the rest of the path
matchesFullPath bool
}
func newPathNode() *pathNode {
return &pathNode{edges: make(map[string]*pathNode)}
}
func (pn *pathNode) add(path string, route *route) {
pn.addInternal(splitPath(path), route, nil, nil)
}
func (pn *pathNode) addInternal(segments []string, route *route, wildcards []string, regexps []*regexp.Regexp) {
if len(segments) == 0 {
allNilRegexps := true
for _, r := range regexps {
if r != nil {
allNilRegexps = false
break
}
}
if allNilRegexps {
regexps = nil
}
matchesFullPath := false
if len(wildcards) > 0 {
matchesFullPath = wildcards[len(wildcards)-1] == "*"
}
pn.leaves = append(pn.leaves, &pathLeaf{route: route, wildcards: wildcards, regexps: regexps, matchesFullPath: matchesFullPath})
} else { // len(segments) >= 1
seg := segments[0]
wc, wcName, wcRegexpStr := isWildcard(seg)
if wc {
if pn.wildcard == nil {
pn.wildcard = newPathNode()
}
if !pn.wildcard.matchesFullPath {
pn.wildcard.matchesFullPath = wcName == "*"
}
pn.wildcard.addInternal(segments[1:], route, append(wildcards, wcName), append(regexps, compileRegexp(wcRegexpStr)))
} else {
subPn, ok := pn.edges[seg]
if !ok {
subPn = newPathNode()
pn.edges[seg] = subPn
}
subPn.addInternal(segments[1:], route, wildcards, regexps)
}
}
}
func (pn *pathNode) Match(path string) (leaf *pathLeaf, wildcards map[string]string) {
// Bail on invalid paths.
if len(path) == 0 || path[0] != '/' {
return nil, nil
}
return pn.match(splitPath(path), nil)
}
// Segments is like ["admin", "users"] representing "/admin/users"
// wildcardValues are the actual values accumulated when we match on a wildcard.
func (pn *pathNode) match(segments []string, wildcardValues []string) (leaf *pathLeaf, wildcardMap map[string]string) {
// Handle leaf nodes:
if len(segments) == 0 {
for _, leaf := range pn.leaves {
if leaf.match(wildcardValues) {
return leaf, makeWildcardMap(leaf, wildcardValues)
}
}
return nil, nil
}
var seg string
seg, segments = segments[0], segments[1:]
subPn, ok := pn.edges[seg]
if ok {
leaf, wildcardMap = subPn.match(segments, wildcardValues)
}
if leaf == nil && pn.wildcard != nil {
leaf, wildcardMap = pn.wildcard.match(segments, append(wildcardValues, seg))
}
if leaf == nil && pn.matchesFullPath {
for _, leaf := range pn.leaves {
if leaf.matchesFullPath && leaf.match(wildcardValues) {
if len(wildcardValues) > 0 {
wcVals := []string{wildcardValues[len(wildcardValues)-1], seg}
for _, s := range segments {
wcVals = append(wcVals, s)
}
wildcardValues[len(wildcardValues)-1] = strings.Join(wcVals, "/")
}
return leaf, makeWildcardMap(leaf, wildcardValues)
}
}
return nil, nil
}
return leaf, wildcardMap
}
func (leaf *pathLeaf) match(wildcardValues []string) bool {
if leaf.regexps == nil {
return true
}
// Invariant:
if len(leaf.regexps) != len(wildcardValues) {
panic("bug: invariant violated")
}
for i, r := range leaf.regexps {
if r != nil {
if !r.MatchString(wildcardValues[i]) {
return false
}
}
}
return true
}
// key is a non-empty path segment like "admin" or ":category_id" or ":category_id:\d+"
// Returns true if it's a wildcard, and if it is, also returns it's name / regexp.
// Eg, (true, "category_id", "\d+")
func isWildcard(key string) (bool, string, string) {
if key[0] == ':' {
substrs := strings.SplitN(key[1:], ":", 2)
if len(substrs) == 1 {
return true, substrs[0], ""
}
return true, substrs[0], substrs[1]
}
return false, "", ""
}
// "/" -> []
// "/admin" -> ["admin"]
// "/admin/" -> ["admin"]
// "/admin/users" -> ["admin", "users"]
func splitPath(key string) []string {
elements := strings.Split(key, "/")
if elements[0] == "" {
elements = elements[1:]
}
if elements[len(elements)-1] == "" {
elements = elements[:len(elements)-1]
}
return elements
}
func makeWildcardMap(leaf *pathLeaf, wildcards []string) map[string]string {
if leaf == nil {
return nil
}
leafWildcards := leaf.wildcards
if len(wildcards) == 0 || (len(leafWildcards) != len(wildcards)) {
return nil
}
// At this point, we know that wildcards and leaf.wildcards match in length.
assoc := make(map[string]string)
for i, w := range wildcards {
assoc[leafWildcards[i]] = w
}
return assoc
}
func compileRegexp(regStr string) *regexp.Regexp {
if regStr == "" {
return nil
}
return regexp.MustCompile("^" + regStr + "$")
}