echo icon indicating copy to clipboard operation
echo copied to clipboard

Context binder proposal

Open tombuente opened this issue 6 months ago • 0 comments

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.

tombuente avatar Jul 08 '25 02:07 tombuente