Skip to content

Feature Request: SetUserValue key changed to interface{} #1385

@pjebs

Description

@pjebs

Currently SetUserValue accepts only a string for a key.
This is a limitation.

It would be great if the key accepted an interface{} similar to context.Context: https://pkg.go.dev/context#WithValue

The rationale for the (arguably backward compatible change) is that if it accepts interface{}, I can make the key into an unexported internal type.

That way, outside packages can't tamper with my user value.

In the fiber framework, the fiber context (which gives access to fasthttp.uservalue) is passed down middlewares. Some of these middleware are external packages I have no control over.

I need to protect some of my user values so they don't get tampered with. If the type of key is an unexported internal type, they can't modify it.

This approach is heavily used by context.Context.

type internalKey string

ctx.SetUserValue( internalKey("my key"), 22)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions