refactor: refactor context handling and nil checks

- Refactor nil checks to improve readability in `context.go`
- Modify the control flow in `HandlerNames` and `Next` methods to continue on nil values before appending or invoking handlers in `context.go`

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2024-05-13 13:40:04 +08:00
parent 83036d4628
commit 4d3dfc8682
No known key found for this signature in database

View File

@ -152,9 +152,10 @@ func (c *Context) HandlerName() string {
func (c *Context) HandlerNames() []string { func (c *Context) HandlerNames() []string {
hn := make([]string, 0, len(c.handlers)) hn := make([]string, 0, len(c.handlers))
for _, val := range c.handlers { for _, val := range c.handlers {
if nil != val { if val == nil {
hn = append(hn, nameOfFunction(val)) continue
} }
hn = append(hn, nameOfFunction(val))
} }
return hn return hn
} }
@ -184,9 +185,10 @@ func (c *Context) FullPath() string {
func (c *Context) Next() { func (c *Context) Next() {
c.index++ c.index++
for c.index < int8(len(c.handlers)) { for c.index < int8(len(c.handlers)) {
if nil != c.handlers[c.index] { if c.handlers[c.index] == nil {
c.handlers[c.index](c) continue
} }
c.handlers[c.index](c)
c.index++ c.index++
} }
} }