Skip to content

Commit 4f0d806

Browse files
committed
tree: reduce nesting
1 parent 574f375 commit 4f0d806

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

tree.go

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ func (n *node) insertChild(path, fullPath string, handle Handle) {
240240
"' conflicts with existing children in path '" + fullPath + "'")
241241
}
242242

243-
if wildcard[0] == ':' { // param
243+
// param
244+
if wildcard[0] == ':' {
244245
if i > 0 {
245246
// Insert prefix before the current wildcard
246247
n.path = path[:i]
@@ -271,45 +272,45 @@ func (n *node) insertChild(path, fullPath string, handle Handle) {
271272
// Otherwise we're done. Insert the handle in the new leaf
272273
n.handle = handle
273274
return
275+
}
274276

275-
} else { // catchAll
276-
if i+len(wildcard) != len(path) {
277-
panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'")
278-
}
279-
280-
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
281-
panic("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "'")
282-
}
283-
284-
// Currently fixed width 1 for '/'
285-
i--
286-
if path[i] != '/' {
287-
panic("no / before catch-all in path '" + fullPath + "'")
288-
}
277+
// catchAll
278+
if i+len(wildcard) != len(path) {
279+
panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'")
280+
}
289281

290-
n.path = path[:i]
282+
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
283+
panic("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "'")
284+
}
291285

292-
// First node: catchAll node with empty path
293-
child := &node{
294-
wildChild: true,
295-
nType: catchAll,
296-
}
297-
n.children = []*node{child}
298-
n.indices = string('/')
299-
n = child
300-
n.priority++
286+
// Currently fixed width 1 for '/'
287+
i--
288+
if path[i] != '/' {
289+
panic("no / before catch-all in path '" + fullPath + "'")
290+
}
301291

302-
// Second node: node holding the variable
303-
child = &node{
304-
path: path[i:],
305-
nType: catchAll,
306-
handle: handle,
307-
priority: 1,
308-
}
309-
n.children = []*node{child}
292+
n.path = path[:i]
310293

311-
return
294+
// First node: catchAll node with empty path
295+
child := &node{
296+
wildChild: true,
297+
nType: catchAll,
298+
}
299+
n.children = []*node{child}
300+
n.indices = string('/')
301+
n = child
302+
n.priority++
303+
304+
// Second node: node holding the variable
305+
child = &node{
306+
path: path[i:],
307+
nType: catchAll,
308+
handle: handle,
309+
priority: 1,
312310
}
311+
n.children = []*node{child}
312+
313+
return
313314
}
314315

315316
// If no wildcard was found, simply insert the path and handle

0 commit comments

Comments
 (0)