aboutsummaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authorGravatar pj <pjebs@users.noreply.github.com> 2022-10-07 01:25:32 +1100
committerGravatar GitHub <noreply@github.com> 2022-10-06 16:25:32 +0200
commitd404f2db91fcf59244f75272ceaa1d28d9ff352c (patch)
treef8bcc7aedfe22e841a192a95aca93462634faf74 /server.go
parenttest: merge test in adaptor_test.go (#1381) (diff)
downloadfasthttp-d404f2db91fcf59244f75272ceaa1d28d9ff352c.tar.gz
fasthttp-d404f2db91fcf59244f75272ceaa1d28d9ff352c.tar.bz2
fasthttp-d404f2db91fcf59244f75272ceaa1d28d9ff352c.zip
make RequestCtx's userdata accept keys that are of type: interface{} (#1387)
Co-authored-by: rocketlaunchr-cto <rocketlaunchr.cloud@gmail.com>
Diffstat (limited to 'server.go')
-rw-r--r--server.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/server.go b/server.go
index a7e93b1..3f68eae 100644
--- a/server.go
+++ b/server.go
@@ -670,7 +670,7 @@ func (ctx *RequestCtx) Hijacked() bool {
// All the values are removed from ctx after returning from the top
// RequestHandler. Additionally, Close method is called on each value
// implementing io.Closer before removing the value from ctx.
-func (ctx *RequestCtx) SetUserValue(key string, value interface{}) {
+func (ctx *RequestCtx) SetUserValue(key interface{}, value interface{}) {
ctx.userValues.Set(key, value)
}
@@ -688,7 +688,7 @@ func (ctx *RequestCtx) SetUserValueBytes(key []byte, value interface{}) {
}
// UserValue returns the value stored via SetUserValue* under the given key.
-func (ctx *RequestCtx) UserValue(key string) interface{} {
+func (ctx *RequestCtx) UserValue(key interface{}) interface{} {
return ctx.userValues.Get(key)
}
@@ -698,13 +698,26 @@ func (ctx *RequestCtx) UserValueBytes(key []byte) interface{} {
return ctx.userValues.GetBytes(key)
}
-// VisitUserValues calls visitor for each existing userValue.
+// VisitUserValues calls visitor for each existing userValue with a key that is a string or []byte.
//
// visitor must not retain references to key and value after returning.
// Make key and/or value copies if you need storing them after returning.
func (ctx *RequestCtx) VisitUserValues(visitor func([]byte, interface{})) {
for i, n := 0, len(ctx.userValues); i < n; i++ {
kv := &ctx.userValues[i]
+ if _, ok := kv.key.(string); ok {
+ visitor(s2b(kv.key.(string)), kv.value)
+ }
+ }
+}
+
+// VisitUserValuesAll calls visitor for each existing userValue.
+//
+// visitor must not retain references to key and value after returning.
+// Make key and/or value copies if you need storing them after returning.
+func (ctx *RequestCtx) VisitUserValuesAll(visitor func(interface{}, interface{})) {
+ for i, n := 0, len(ctx.userValues); i < n; i++ {
+ kv := &ctx.userValues[i]
visitor(kv.key, kv.value)
}
}
@@ -715,7 +728,7 @@ func (ctx *RequestCtx) ResetUserValues() {
}
// RemoveUserValue removes the given key and the value under it in ctx.
-func (ctx *RequestCtx) RemoveUserValue(key string) {
+func (ctx *RequestCtx) RemoveUserValue(key interface{}) {
ctx.userValues.Remove(key)
}
@@ -2696,10 +2709,7 @@ func (ctx *RequestCtx) Err() error {
// This method is present to make RequestCtx implement the context interface.
// This method is the same as calling ctx.UserValue(key)
func (ctx *RequestCtx) Value(key interface{}) interface{} {
- if keyString, ok := key.(string); ok {
- return ctx.UserValue(keyString)
- }
- return nil
+ return ctx.UserValue(key)
}
var fakeServer = &Server{