Skip to content

Commit 67b1dfe

Browse files
committed
Fixed #32
Signed-off-by: Vishal Rana <[email protected]>
1 parent 0a075ce commit 67b1dfe

File tree

2 files changed

+65
-28
lines changed

2 files changed

+65
-28
lines changed

router.go

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ type (
1313
prefix string
1414
parent *node
1515
children children
16-
// pchild *node // Param child
17-
// cchild *node // Catch-all child
18-
handler HandlerFunc
19-
pnames []string
20-
echo *Echo
16+
pchild *node // Param child
17+
cchild *node // Catch-all child
18+
handler HandlerFunc
19+
pnames []string
20+
echo *Echo
2121
}
2222
ntype uint8
2323
children []*node
@@ -96,11 +96,18 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
9696
// Split node
9797
n := newNode(t, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo)
9898
cn.children = children{n} // Add to parent
99+
// if n.typ == ptype {
100+
// cn.pchild = n
101+
// } else if n.typ == ctype {
102+
// cn.cchild = n
103+
// }
99104

100105
// Reset parent node
101106
cn.typ = stype
102107
cn.label = cn.prefix[0]
103108
cn.prefix = cn.prefix[:l]
109+
// cn.pchild = nil
110+
// cn.cchild = nil
104111
cn.handler = nil
105112
cn.pnames = nil
106113
cn.echo = nil
@@ -115,6 +122,11 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
115122
// Create child node
116123
n = newNode(t, search[l:], cn, children{}, h, pnames, echo)
117124
cn.children = append(cn.children, n)
125+
// if n.typ == ptype {
126+
// cn.pchild = n
127+
// } else if n.typ == ctype {
128+
// cn.cchild = n
129+
// }
118130
}
119131
} else if l < sl {
120132
search = search[l:]
@@ -127,6 +139,11 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
127139
// Create child node
128140
n := newNode(t, search, cn, children{}, h, pnames, echo)
129141
cn.children = append(cn.children, n)
142+
// if n.typ == ptype {
143+
// cn.pchild = n
144+
// } else if n.typ == ctype {
145+
// cn.cchild = n
146+
// }
130147
} else {
131148
// Node already exists
132149
if h != nil {
@@ -139,8 +156,8 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
139156
}
140157
}
141158

142-
func newNode(t ntype, pfx string, p *node, c children, h HandlerFunc, pnames []string, echo *Echo) (n *node) {
143-
n = &node{
159+
func newNode(t ntype, pfx string, p *node, c children, h HandlerFunc, pnames []string, echo *Echo) *node {
160+
return &node{
144161
typ: t,
145162
label: pfx[0],
146163
prefix: pfx,
@@ -150,7 +167,9 @@ func newNode(t ntype, pfx string, p *node, c children, h HandlerFunc, pnames []s
150167
pnames: pnames,
151168
echo: echo,
152169
}
153-
return
170+
}
171+
172+
func (n *node) addChild(c *node) {
154173
}
155174

156175
func (n *node) findChild(l byte) *node {
@@ -201,20 +220,22 @@ func lcp(a, b string) (i int) {
201220
return
202221
}
203222

204-
func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Echo) {
223+
func (r *router) Find(method, path string, ctx *Context) (h HandlerFunc, echo *Echo) {
205224
cn := r.trees[method] // Current node as root
206225
search := path
207-
chn := new(node) // Child node
208-
n := 0 // Param counter
226+
c := new(node) // Child node
227+
n := 0 // Param counter
209228

210229
// Search order static > param > catch-all
211230
for {
212231
if search == "" || search == cn.prefix {
213-
// Found
214-
h = cn.handler
215-
c.pnames = cn.pnames
216-
echo = cn.echo
217-
return
232+
if cn.handler != nil {
233+
// Found
234+
h = cn.handler
235+
ctx.pnames = cn.pnames
236+
echo = cn.echo
237+
return
238+
}
218239
}
219240

220241
pl := len(cn.prefix)
@@ -226,32 +247,40 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
226247
goto Up
227248
}
228249

250+
// Check for catch-all with empty string
251+
if len(search) == 0 {
252+
goto CatchAll
253+
}
254+
229255
// Static node
230-
chn = cn.findSchild(search[0])
231-
if chn != nil {
232-
cn = chn
256+
c = cn.findSchild(search[0])
257+
if c != nil {
258+
cn = c
233259
continue
234260
}
235261

236262
// Param node
237263
Param:
238-
chn = cn.findPchild()
239-
if chn != nil {
240-
cn = chn
264+
c = cn.findPchild()
265+
// c = cn.pchild
266+
if c != nil {
267+
cn = c
241268
i, l := 0, len(search)
242269
for ; i < l && search[i] != '/'; i++ {
243270
}
244-
c.pvalues[n] = search[:i]
271+
ctx.pvalues[n] = search[:i]
245272
n++
246273
search = search[i:]
247274
continue
248275
}
249276

250277
// Catch-all node
251-
chn = cn.findCchild()
252-
if chn != nil {
253-
cn = chn
254-
c.pvalues[n] = search
278+
CatchAll:
279+
// c = cn.cchild
280+
c = cn.findCchild()
281+
if c != nil {
282+
cn = c
283+
ctx.pvalues[n] = search
255284
search = "" // End search
256285
continue
257286
}

router_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,15 @@ func TestRouterCatchAll(t *testing.T) {
344344
return nil
345345
}, nil)
346346

347-
h, _ := r.Find(GET, "/users/joe", context)
347+
h, _ := r.Find(GET, "/users/", context)
348+
if h == nil {
349+
t.Fatal("handler not found")
350+
}
351+
if context.pvalues[0] != "" {
352+
t.Error("value should be joe")
353+
}
354+
355+
h, _ = r.Find(GET, "/users/joe", context)
348356
if h == nil {
349357
t.Fatal("handler not found")
350358
}

0 commit comments

Comments
 (0)