refactor: refactor return logic in tree operations

- Modify multiple return statements in `tree.go` to return a specific value instead of nothing

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2024-02-01 09:19:44 +08:00
parent befcf6fa3a
commit 0603e23fdc
No known key found for this signature in database

22
tree.go
View File

@ -478,7 +478,7 @@ walk: // Outer loop for walking the tree
// We can recommend to redirect to the same URL without a // We can recommend to redirect to the same URL without a
// trailing slash if a leaf exists for that path. // trailing slash if a leaf exists for that path.
value.tsr = path == "/" && n.handlers != nil value.tsr = path == "/" && n.handlers != nil
return return value
} }
// Handle wildcard child, which is always at the end of the array // Handle wildcard child, which is always at the end of the array
@ -533,12 +533,12 @@ walk: // Outer loop for walking the tree
// ... but we can't // ... but we can't
value.tsr = len(path) == end+1 value.tsr = len(path) == end+1
return return value
} }
if value.handlers = n.handlers; value.handlers != nil { if value.handlers = n.handlers; value.handlers != nil {
value.fullPath = n.fullPath value.fullPath = n.fullPath
return return value
} }
if len(n.children) == 1 { if len(n.children) == 1 {
// No handle found. Check if a handle for this path + a // No handle found. Check if a handle for this path + a
@ -546,7 +546,7 @@ walk: // Outer loop for walking the tree
n = n.children[0] n = n.children[0]
value.tsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/") value.tsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/")
} }
return return value
case catchAll: case catchAll:
// Save param value // Save param value
@ -578,7 +578,7 @@ walk: // Outer loop for walking the tree
value.handlers = n.handlers value.handlers = n.handlers
value.fullPath = n.fullPath value.fullPath = n.fullPath
return return value
default: default:
panic("invalid node type") panic("invalid node type")
@ -609,7 +609,7 @@ walk: // Outer loop for walking the tree
// Check if this node has a handle registered. // Check if this node has a handle registered.
if value.handlers = n.handlers; value.handlers != nil { if value.handlers = n.handlers; value.handlers != nil {
value.fullPath = n.fullPath value.fullPath = n.fullPath
return return value
} }
// If there is no handle for this route, but this route has a // If there is no handle for this route, but this route has a
@ -617,12 +617,12 @@ walk: // Outer loop for walking the tree
// additional trailing slash // additional trailing slash
if path == "/" && n.wildChild && n.nType != root { if path == "/" && n.wildChild && n.nType != root {
value.tsr = true value.tsr = true
return return value
} }
if path == "/" && n.nType == static { if path == "/" && n.nType == static {
value.tsr = true value.tsr = true
return return value
} }
// No handle found. Check if a handle for this path + a // No handle found. Check if a handle for this path + a
@ -632,11 +632,11 @@ walk: // Outer loop for walking the tree
n = n.children[i] n = n.children[i]
value.tsr = (len(n.path) == 1 && n.handlers != nil) || value.tsr = (len(n.path) == 1 && n.handlers != nil) ||
(n.nType == catchAll && n.children[0].handlers != nil) (n.nType == catchAll && n.children[0].handlers != nil)
return return value
} }
} }
return return value
} }
// Nothing found. We can recommend to redirect to the same URL with an // Nothing found. We can recommend to redirect to the same URL with an
@ -662,7 +662,7 @@ walk: // Outer loop for walking the tree
} }
} }
return return value
} }
} }