Skip to content

Commit 90ff3c1

Browse files
committed
clean up
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 604d941 commit 90ff3c1

File tree

2 files changed

+47
-49
lines changed

2 files changed

+47
-49
lines changed

router.go

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ type (
88
echo *Echo
99
}
1010
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
1717
}
18-
edges []*node
19-
param struct {
18+
children []*node
19+
param struct {
2020
Name string
2121
Value string
2222
}
@@ -30,8 +30,8 @@ func NewRouter(e *Echo) (r *router) {
3030
}
3131
for _, m := range methods {
3232
r.trees[m] = &node{
33-
prefix: "",
34-
edges: edges{},
33+
prefix: "",
34+
children: children{},
3535
}
3636
}
3737
return
@@ -75,8 +75,8 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
7575
}
7676
} else if l < pl {
7777
// 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
8080

8181
// Reset parent node
8282
cn.label = cn.prefix[0]
@@ -90,20 +90,20 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
9090
cn.echo = echo
9191
} else {
9292
// 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)
9595
}
9696
} else if l < sl {
9797
search = search[l:]
98-
e := cn.findEdge(search[0])
99-
if e != nil {
98+
c := cn.findChild(search[0])
99+
if c != nil {
100100
// Go deeper
101-
cn = e
101+
cn = c
102102
continue
103103
}
104104
// 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)
107107
} else {
108108
// Node already exists
109109
if h != nil {
@@ -115,22 +115,22 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
115115
}
116116
}
117117

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) {
119119
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,
126126
}
127127
return
128128
}
129129

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
134134
}
135135
}
136136
return nil
@@ -151,7 +151,8 @@ func lcp(a, b string) (i int) {
151151
func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *Echo) {
152152
cn := r.trees[method] // Current node as root
153153
search := path
154-
n := 0 // Param count
154+
n := 0 // Param count
155+
c := new(node) // Child node
155156

156157
// Search order static > param > catch-all
157158
for {
@@ -162,30 +163,27 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
162163
return
163164
}
164165

165-
var e *node
166166
pl := len(cn.prefix)
167167
l := lcp(search, cn.prefix)
168168

169169
if l == pl {
170170
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
175173
}
176174

177175
// 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
181179
continue
182180
}
183181

184182
// Param node
185183
Param:
186-
e = cn.findEdge(':')
187-
if e != nil {
188-
cn = e
184+
c = cn.findChild(':')
185+
if c != nil {
186+
cn = c
189187
i, l := 0, len(search)
190188
for ; i < l && search[i] != '/'; i++ {
191189
}
@@ -197,9 +195,9 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
197195
}
198196

199197
// 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
203201
p := params[:n+1]
204202
p[n].Name = "_name"
205203
p[n].Value = search

router_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,14 +531,14 @@ func (n *node) printTree(pfx string, tail bool) {
531531
p := prefix(tail, pfx, "└── ", "├── ")
532532
fmt.Printf("%s%s, %p: parent=%p, handler=%v, echo=%v\n", p, n.prefix, n, n.parent, n.handler, n.echo)
533533

534-
nodes := n.edges
535-
l := len(nodes)
534+
children := n.children
535+
l := len(children)
536536
p = prefix(tail, pfx, " ", "│ ")
537537
for i := 0; i < l-1; i++ {
538-
nodes[i].printTree(p, false)
538+
children[i].printTree(p, false)
539539
}
540540
if l > 0 {
541-
nodes[l-1].printTree(p, true)
541+
children[l-1].printTree(p, true)
542542
}
543543
}
544544

0 commit comments

Comments
 (0)