Skip to content

Error retrieving context.P() #103

Closed
Closed
@benpate

Description

@benpate

Howdy,

I've just run into a bug. I'm not sure when it crept in, but my code was working fine before, and now I'm getting errors.

Here is some code from my application. It scans the context parameters to find the last sequential route parameter that's present in the current Context.

// lastParam scans the Context, returning the last parameter in the route, which is
// commonly used as the ID of the object being loaded.
func lastParam(ctx *echo.Context) string {

    var lastParam string
    var i int

    for {
        fmt.Println(i)
        if param := ctx.P(i); param != "" {
            lastParam = param
        } else {
            return lastParam
        }
        i++
    }
}

Now, I'd much rather be able to ask Echo what the last parameter is, but that's a whole other issue. For now, I'll leave that request alone :)

This function is now breaking when it tries to read past the number of parameters present in the route. For example, I have three parameters in the route /courses/:courseId/topics/:topicId/pages/:pageId -- but, the system is panicking when I try to read P(4) instead of returning an empty string, which it used to do.

I've tracked down the code to context.go around line 54. Here's the code I see:

// P returns path parameter by index.
func (c *Context) P(i int) (value string) {
    l := len(c.pnames)
    if i <= l {
        value = c.pvalues[i]
    }
    return
}

Since arrays are zero indexed, I believe that the if i <= l on line 54 should change to if i < l instead.

Does this make sense? I know the overall package is pretty complex, so I don't want to propose something without being sure that I understand the complete context.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions