8
8
echo * Echo
9
9
}
10
10
node struct {
11
- label byte
12
- prefix string
13
- parent * node
14
- edges edges
15
- handler HandlerFunc
16
- echo * Echo
11
+ label byte
12
+ prefix string
13
+ parent * node
14
+ children children
15
+ handler HandlerFunc
16
+ echo * Echo
17
17
}
18
- edges []* node
19
- param struct {
18
+ children []* node
19
+ param struct {
20
20
Name string
21
21
Value string
22
22
}
@@ -30,8 +30,8 @@ func NewRouter(e *Echo) (r *router) {
30
30
}
31
31
for _ , m := range methods {
32
32
r .trees [m ] = & node {
33
- prefix : "" ,
34
- edges : edges {},
33
+ prefix : "" ,
34
+ children : children {},
35
35
}
36
36
}
37
37
return
@@ -75,8 +75,8 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
75
75
}
76
76
} else if l < pl {
77
77
// Split node
78
- n := newNode (cn .prefix [l :], cn , cn .edges , cn .handler , cn .echo )
79
- cn .edges = edges {n } // Add to parent
78
+ n := newNode (cn .prefix [l :], cn , cn .children , cn .handler , cn .echo )
79
+ cn .children = children {n } // Add to parent
80
80
81
81
// Reset parent node
82
82
cn .label = cn .prefix [0 ]
@@ -90,20 +90,20 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
90
90
cn .echo = echo
91
91
} else {
92
92
// Create child node
93
- n = newNode (search [l :], cn , edges {}, h , echo )
94
- cn .edges = append (cn .edges , n )
93
+ n = newNode (search [l :], cn , children {}, h , echo )
94
+ cn .children = append (cn .children , n )
95
95
}
96
96
} else if l < sl {
97
97
search = search [l :]
98
- e := cn .findEdge (search [0 ])
99
- if e != nil {
98
+ c := cn .findChild (search [0 ])
99
+ if c != nil {
100
100
// Go deeper
101
- cn = e
101
+ cn = c
102
102
continue
103
103
}
104
104
// Create child node
105
- n := newNode (search , cn , edges {}, h , echo )
106
- cn .edges = append (cn .edges , n )
105
+ n := newNode (search , cn , children {}, h , echo )
106
+ cn .children = append (cn .children , n )
107
107
} else {
108
108
// Node already exists
109
109
if h != nil {
@@ -115,22 +115,22 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
115
115
}
116
116
}
117
117
118
- func newNode (pfx string , p * node , e edges , h HandlerFunc , echo * Echo ) (n * node ) {
118
+ func newNode (pfx string , p * node , c children , h HandlerFunc , echo * Echo ) (n * node ) {
119
119
n = & node {
120
- label : pfx [0 ],
121
- prefix : pfx ,
122
- parent : p ,
123
- edges : e ,
124
- handler : h ,
125
- echo : echo ,
120
+ label : pfx [0 ],
121
+ prefix : pfx ,
122
+ parent : p ,
123
+ children : c ,
124
+ handler : h ,
125
+ echo : echo ,
126
126
}
127
127
return
128
128
}
129
129
130
- func (n * node ) findEdge (l byte ) * node {
131
- for _ , e := range n .edges {
132
- if e .label == l {
133
- return e
130
+ func (n * node ) findChild (l byte ) * node {
131
+ for _ , c := range n .children {
132
+ if c .label == l {
133
+ return c
134
134
}
135
135
}
136
136
return nil
@@ -151,7 +151,8 @@ func lcp(a, b string) (i int) {
151
151
func (r * router ) Find (method , path string , params Params ) (h HandlerFunc , echo * Echo ) {
152
152
cn := r .trees [method ] // Current node as root
153
153
search := path
154
- n := 0 // Param count
154
+ n := 0 // Param count
155
+ c := new (node ) // Child node
155
156
156
157
// Search order static > param > catch-all
157
158
for {
@@ -162,30 +163,27 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
162
163
return
163
164
}
164
165
165
- var e * node
166
166
pl := len (cn .prefix )
167
167
l := lcp (search , cn .prefix )
168
168
169
169
if l == pl {
170
170
search = search [l :]
171
- } else if l < pl {
172
- if cn .label != ':' {
173
- goto Up
174
- }
171
+ } else if l < pl && cn .label != ':' {
172
+ goto Up
175
173
}
176
174
177
175
// Static node
178
- e = cn .findEdge (search [0 ])
179
- if e != nil {
180
- cn = e
176
+ c = cn .findChild (search [0 ])
177
+ if c != nil {
178
+ cn = c
181
179
continue
182
180
}
183
181
184
182
// Param node
185
183
Param:
186
- e = cn .findEdge (':' )
187
- if e != nil {
188
- cn = e
184
+ c = cn .findChild (':' )
185
+ if c != nil {
186
+ cn = c
189
187
i , l := 0 , len (search )
190
188
for ; i < l && search [i ] != '/' ; i ++ {
191
189
}
@@ -197,9 +195,9 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
197
195
}
198
196
199
197
// Catch-all node
200
- e = cn .findEdge ('*' )
201
- if e != nil {
202
- cn = e
198
+ c = cn .findChild ('*' )
199
+ if c != nil {
200
+ cn = c
203
201
p := params [:n + 1 ]
204
202
p [n ].Name = "_name"
205
203
p [n ].Value = search
0 commit comments