/
route_node.go
237 lines (200 loc) · 4.61 KB
/
route_node.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
232
233
234
235
236
237
package gen
import (
"sort"
"strings"
"github.com/go-faster/errors"
"github.com/ogen-go/ogen/gen/ir"
)
type nodes []*RouteNode
func (e nodes) Len() int {
return len(e)
}
func (e nodes) Less(i, j int) bool {
return e[i].head < e[j].head
}
func (e nodes) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
func (e nodes) Sort() {
sort.Sort(e)
}
// RouteNode is node of Radix tree of routes.
type RouteNode struct {
prefix string
head byte
child nodes
paramName string
param *ir.Parameter // nil-able
routes Routes
}
// AddRoute adds new method route to node.
func (n *RouteNode) AddRoute(nr Route) error {
return n.routes.AddRoute(nr)
}
// Prefix returns common prefix.
func (n *RouteNode) Prefix() string {
return n.prefix
}
// Head returns first byte of prefix.
func (n *RouteNode) Head() byte {
return n.head
}
// IsStatic whether node is not a parameter node.
func (n *RouteNode) IsStatic() bool {
return n.param == nil
}
// IsLeaf whether node has no children.
func (n *RouteNode) IsLeaf() bool {
return n.child.Len() == 0
}
// IsParam whether node is a parameter node.
func (n *RouteNode) IsParam() bool {
return n.param != nil
}
// Children returns child nodes.
func (n *RouteNode) Children() []*RouteNode {
return n.child
}
// StaticChildren returns slice of child static nodes.
func (n *RouteNode) StaticChildren() (r []*RouteNode) {
for _, c := range n.child {
if c.IsStatic() {
r = append(r, c)
}
}
return r
}
// ParamChildren returns slice of child parameter nodes.
func (n *RouteNode) ParamChildren() (r []*RouteNode) {
for _, c := range n.child {
if c.IsParam() {
r = append(r, c)
}
}
return r
}
// Tails returns heads of child nodes.
//
// Used for matching end of parameter node between two static.
func (n *RouteNode) Tails() (r []byte) {
for _, c := range n.child {
if !c.IsParam() {
r = append(r, c.head)
}
}
return r
}
// ParamName returns parameter name, if any.
func (n *RouteNode) ParamName() string {
return n.paramName
}
// Param returns associated parameter, if any.
//
// May be nil.
func (n *RouteNode) Param() *ir.Parameter {
return n.param
}
// AllowedMethods returns list of allowed methods.
func (n *RouteNode) AllowedMethods() string {
var s strings.Builder
for i, route := range n.routes {
if i != 0 {
s.WriteByte(',')
}
s.WriteString(route.Method)
}
return s.String()
}
// Routes returns list of associated MethodRoute.
func (n *RouteNode) Routes() []Route {
return n.routes
}
func nextPathPart(s string) (hasParam bool, paramStart, paramEnd int, _ error) {
paramStart = strings.IndexByte(s, '{')
if paramStart < 0 {
return false, 0, 0, nil
}
paramEnd = strings.IndexByte(s, '}')
if paramEnd < 0 || paramEnd < paramStart {
return false, paramStart, paramEnd, errors.Errorf("unclosed '{' at %d", paramStart)
}
// Need to match parameter part including both brackets.
paramEnd++
return true, paramStart, paramEnd, nil
}
func (n *RouteNode) addChild(path string, op *ir.Operation, ch *RouteNode) (r *RouteNode, _ error) {
r = ch
hasParam, start, end, err := nextPathPart(path)
if err != nil {
return nil, errors.Errorf("parse %q", path)
}
if hasParam {
paramName := path[start+1 : end-1]
p, ok := findParam(op, paramName)
if !ok {
return nil, errors.Errorf("unknown parameter %q", paramName)
}
if start == 0 { // Route starts with a param.
ch.paramName = paramName
ch.param = p
// Handle tail of path.
if len(path[end:]) > 0 {
path = path[end:]
n, err := ch.addChild(path, op, &RouteNode{
prefix: path,
head: path[0],
})
if err != nil {
return nil, err
}
r = n
}
} else { // Route contains param.
// Set prefix to static part of path.
ch.prefix = path[:start]
// Get parameterized part.
path = path[start:]
// Add parameterized child node.
n, err := ch.addChild(path, op, &RouteNode{
head: path[0],
paramName: paramName,
param: p,
})
if err != nil {
return nil, err
}
r = n
}
}
n.child = append(n.child, ch)
n.child.Sort()
return r, nil
}
func (n *RouteNode) childIdx(head byte) (int, bool) {
for i := range n.child {
if n.child[i].head == head {
return i, true
}
}
return 0, false
}
func (n *RouteNode) replaceChild(head byte, child *RouteNode) {
idx, _ := n.childIdx(head)
n.child[idx] = child
}
func (n *RouteNode) getChild(head byte) *RouteNode {
idx, ok := n.childIdx(head)
if ok && n.child[idx].head == head {
return n.child[idx]
}
return nil
}
func (n *RouteNode) walk(level int, cb func(level int, n *RouteNode)) {
if n == nil {
return
}
cb(level, n)
for _, ch := range n.child {
ch.walk(level+1, cb)
}
}