From 16c5bbf530236a355416c626dd2a345d021e2c0d Mon Sep 17 00:00:00 2001 From: Danial Date: Tue, 10 Sep 2024 09:44:21 +0700 Subject: [PATCH 1/5] ref: use any as the key type WithValue --- context.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 4403277..25c24c9 100644 --- a/context.go +++ b/context.go @@ -39,8 +39,8 @@ func (c *Context) Response() http.ContextResponse { return NewContextResponse(c.instance, &ResponseOrigin{Ctx: c.instance}) } -func (c *Context) WithValue(key string, value any) { - ctx := context.WithValue(c.instance.UserContext(), ctxKey(key), value) +func (c *Context) WithValue(key any, value any) { + ctx := context.WithValue(c.instance.UserContext(), key, value) c.instance.SetUserContext(ctx) } From 6bab7c0b9c9f96005e76b052b9126e04f9383054 Mon Sep 17 00:00:00 2001 From: Danial Date: Wed, 11 Sep 2024 09:09:34 +0700 Subject: [PATCH 2/5] ref: use the key as is in `Context.Value` --- context.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/context.go b/context.go index 25c24c9..db1457d 100644 --- a/context.go +++ b/context.go @@ -21,8 +21,6 @@ type Context struct { request http.ContextRequest } -type ctxKey string - func NewContext(ctx *fiber.Ctx) http.Context { return &Context{instance: ctx} } @@ -61,11 +59,7 @@ func (c *Context) Err() error { } func (c *Context) Value(key any) any { - if keyStr, ok := key.(string); ok { - return c.instance.UserContext().Value(ctxKey(keyStr)) - } - - return nil + return c.instance.UserContext().Value(key) } func (c *Context) Instance() *fiber.Ctx { From 9799af9b05707826c7cd304de6a893d56b704f0b Mon Sep 17 00:00:00 2001 From: Danial Date: Wed, 11 Sep 2024 09:10:24 +0700 Subject: [PATCH 3/5] test: context test cases for custom key --- context_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/context_test.go b/context_test.go index c589666..f870b81 100644 --- a/context_test.go +++ b/context_test.go @@ -14,3 +14,16 @@ func TestContext(t *testing.T) { assert.Equal(t, httpCtx.Value("Hello").(string), "world") assert.Equal(t, httpCtx.Value("Hi").(string), "Goravel") } + +func TestContextWithCustomKeyType(t *testing.T) { + type customKeyType struct{} + var customKey customKeyType + var customKeyTwo customKeyType + + httpCtx := Background() + httpCtx.WithValue(customKey, "hello") + httpCtx.WithValue(customKeyTwo, "world") + + assert.Equal(t, httpCtx.Value(customKey), "hello") + assert.Equal(t, httpCtx.Value(customKeyTwo), "world") +} From 5b854358a93fbe546a0156cad2cf79cabf3b195b Mon Sep 17 00:00:00 2001 From: Danial Date: Thu, 12 Sep 2024 09:29:47 +0700 Subject: [PATCH 4/5] test: remove unnecessary new test function --- context_test.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/context_test.go b/context_test.go index f870b81..c589666 100644 --- a/context_test.go +++ b/context_test.go @@ -14,16 +14,3 @@ func TestContext(t *testing.T) { assert.Equal(t, httpCtx.Value("Hello").(string), "world") assert.Equal(t, httpCtx.Value("Hi").(string), "Goravel") } - -func TestContextWithCustomKeyType(t *testing.T) { - type customKeyType struct{} - var customKey customKeyType - var customKeyTwo customKeyType - - httpCtx := Background() - httpCtx.WithValue(customKey, "hello") - httpCtx.WithValue(customKeyTwo, "world") - - assert.Equal(t, httpCtx.Value(customKey), "hello") - assert.Equal(t, httpCtx.Value(customKeyTwo), "world") -} From 9989eb4ce2ee4875e23cb85080f647f2f038b05e Mon Sep 17 00:00:00 2001 From: Danial Date: Thu, 12 Sep 2024 09:30:35 +0700 Subject: [PATCH 5/5] test: merge test cases --- context_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/context_test.go b/context_test.go index c589666..8a74074 100644 --- a/context_test.go +++ b/context_test.go @@ -7,10 +7,19 @@ import ( ) func TestContext(t *testing.T) { + type customKeyType struct{} + var customKey customKeyType + httpCtx := Background() httpCtx.WithValue("Hello", "world") httpCtx.WithValue("Hi", "Goravel") + httpCtx.WithValue(1, "one") + httpCtx.WithValue(1.1, "one point one") + httpCtx.WithValue(customKey, "hello") assert.Equal(t, httpCtx.Value("Hello").(string), "world") assert.Equal(t, httpCtx.Value("Hi").(string), "Goravel") + assert.Equal(t, httpCtx.Value(1).(string), "one") + assert.Equal(t, httpCtx.Value(1.1).(string), "one point one") + assert.Equal(t, httpCtx.Value(customKey), "hello") }