Skip to content

Commit 3e45d2a

Browse files
committed
Change methodHandler element type to methodContext
Signed-off-by: ortyomka <iurin.art@gmail.com>
1 parent 0644cd6 commit 3e45d2a

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

router.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ type (
1313
routes map[string]*Route
1414
echo *Echo
1515
}
16+
methodContext struct {
17+
handler HandlerFunc
18+
}
1619
node struct {
1720
kind kind
1821
label byte
@@ -32,17 +35,17 @@ type (
3235
kind uint8
3336
children []*node
3437
methodHandler struct {
35-
connect HandlerFunc
36-
delete HandlerFunc
37-
get HandlerFunc
38-
head HandlerFunc
39-
options HandlerFunc
40-
patch HandlerFunc
41-
post HandlerFunc
42-
propfind HandlerFunc
43-
put HandlerFunc
44-
trace HandlerFunc
45-
report HandlerFunc
38+
connect *methodContext
39+
delete *methodContext
40+
get *methodContext
41+
head *methodContext
42+
options *methodContext
43+
patch *methodContext
44+
post *methodContext
45+
propfind *methodContext
46+
put *methodContext
47+
trace *methodContext
48+
report *methodContext
4649
allowHeader string
4750
}
4851
)
@@ -209,7 +212,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
209212
currentNode.prefix = search
210213
if h != nil {
211214
currentNode.kind = t
212-
currentNode.addHandler(method, h)
215+
currentNode.addHandler(method, &methodContext{
216+
handler: h,
217+
})
213218
currentNode.ppath = ppath
214219
currentNode.pnames = pnames
215220
}
@@ -257,13 +262,17 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
257262
if lcpLen == searchLen {
258263
// At parent node
259264
currentNode.kind = t
260-
currentNode.addHandler(method, h)
265+
currentNode.addHandler(method, &methodContext{
266+
handler: h,
267+
})
261268
currentNode.ppath = ppath
262269
currentNode.pnames = pnames
263270
} else {
264271
// Create child node
265272
n = newNode(t, search[lcpLen:], currentNode, nil, new(methodHandler), ppath, pnames, nil, nil)
266-
n.addHandler(method, h)
273+
n.addHandler(method, &methodContext{
274+
handler: h,
275+
})
267276
// Only Static children could reach here
268277
currentNode.addStaticChild(n)
269278
}
@@ -278,7 +287,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
278287
}
279288
// Create child node
280289
n := newNode(t, search, currentNode, nil, new(methodHandler), ppath, pnames, nil, nil)
281-
n.addHandler(method, h)
290+
n.addHandler(method, &methodContext{
291+
handler: h,
292+
})
282293
switch t {
283294
case staticKind:
284295
currentNode.addStaticChild(n)
@@ -291,7 +302,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
291302
} else {
292303
// Node already exists
293304
if h != nil {
294-
currentNode.addHandler(method, h)
305+
currentNode.addHandler(method, &methodContext{
306+
handler: h,
307+
})
295308
currentNode.ppath = ppath
296309
if len(currentNode.pnames) == 0 { // Issue #729
297310
currentNode.pnames = pnames
@@ -345,7 +358,12 @@ func (n *node) findChildWithLabel(l byte) *node {
345358
return nil
346359
}
347360

348-
func (n *node) addHandler(method string, h HandlerFunc) {
361+
func (n *node) addHandler(method string, h *methodContext) {
362+
if h.handler == nil {
363+
n.isHandler = n.methodHandler.isHandler()
364+
return
365+
}
366+
349367
switch method {
350368
case http.MethodConnect:
351369
n.methodHandler.connect = h
@@ -372,14 +390,10 @@ func (n *node) addHandler(method string, h HandlerFunc) {
372390
}
373391

374392
n.methodHandler.updateAllowHeader()
375-
if h != nil {
376-
n.isHandler = true
377-
} else {
378-
n.isHandler = n.methodHandler.isHandler()
379-
}
393+
n.isHandler = true
380394
}
381395

382-
func (n *node) findHandler(method string) HandlerFunc {
396+
func (n *node) findHandler(method string) *methodContext {
383397
switch method {
384398
case http.MethodConnect:
385399
return n.methodHandler.connect
@@ -530,7 +544,7 @@ func (r *Router) Find(method, path string, c Context) {
530544
previousBestMatchNode = currentNode
531545
}
532546
if h := currentNode.findHandler(method); h != nil {
533-
matchedHandler = h
547+
matchedHandler = h.handler
534548
break
535549
}
536550
}
@@ -581,7 +595,7 @@ func (r *Router) Find(method, path string, c Context) {
581595
previousBestMatchNode = currentNode
582596
}
583597
if h := currentNode.findHandler(method); h != nil {
584-
matchedHandler = h
598+
matchedHandler = h.handler
585599
break
586600
}
587601
}

router_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,7 @@ func TestRouterHandleMethodOptions(t *testing.T) {
23802380
assert.NoError(t, err)
23812381
assert.Equal(t, tc.expectStatus, rec.Code)
23822382
}
2383-
assert.Equal(t, tc.expectAllowHeader, c.Response().Header().Get("Allow"))
2383+
assert.Equal(t, tc.expectAllowHeader, c.Response().Header().Get(HeaderAllow))
23842384
})
23852385
}
23862386
}

0 commit comments

Comments
 (0)