Skip to content

Context binder proposal #2801

Open
Open
@tombuente

Description

@tombuente

Frequently, I attach a User object to my requests in an auth middleware.

user, ok := sessionManager.Get(c.Request().Context(), "user").(AuthUser)
if ok {
	c.Set(UserKey, user)
}
return next(c)

Currently, Echo provides binders for request URL path parameters, URL query parameters, headers, and bodies. I had the idea to add an additional Context binder. This would provide a more streamlined and type-safe way to access this data in handlers.

Usually, you are forced to access it with c.Get(key) and then type assert. I propose:

type Input struct {
    Message string `json:"message"`

    // Proposed: bind from context using a new tag
    User User `context:"user"`
}

A potential limitation of this approach is that the context keys used for binding must be strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions