-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: SetUserValue key changed to interface{} #1385
Comments
I'm not sure what to think of this. It will cause extra allocations. But I guess anyone who cares about that wouldn't be using I don't have time to add support for this at the moment, so a pull request would be very nice. |
In the context.WithValue documentation it states:
With regards to your concern about extra allocations, can you help me understand what they are saying? Then I will issue a PR. |
They are saying that with context if you do:
it won't allocate because an empty struct has no size so it fits within the While doing:
would cause an extra allocation since |
@erikdubbelboer Thanks for explaining |
@erikdubbelboer Any idea when new version will be tagged? |
I'll tag one on Monday. |
Currently
SetUserValue
accepts only a string for a key.This is a limitation.
It would be great if the key accepted an
interface{}
similar tocontext.Context
: https://pkg.go.dev/context#WithValueThe 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
.The text was updated successfully, but these errors were encountered: