From 2bab2141ebab56df079e584efa73285d03c09deb Mon Sep 17 00:00:00 2001 From: Danial Date: Tue, 10 Sep 2024 09:48:33 +0700 Subject: [PATCH 1/4] ref: use Sprintf as helper to accept type any for the key of WithValue --- context.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 34030a4..b514307 100644 --- a/context.go +++ b/context.go @@ -2,6 +2,7 @@ package gin import ( "context" + "fmt" "net/http/httptest" "time" @@ -41,8 +42,8 @@ func (c *Context) Response() http.ContextResponse { return NewContextResponse(c.instance, &BodyWriter{ResponseWriter: c.instance.Writer}) } -func (c *Context) WithValue(key string, value any) { - c.instance.Set(key, value) +func (c *Context) WithValue(key any, value any) { + c.instance.Set(fmt.Sprintf("%v", key), value) } func (c *Context) Context() context.Context { From d0afbc30aa9f9ab6b5b18db2d7484d26b5c797bd Mon Sep 17 00:00:00 2001 From: Danial Date: Wed, 11 Sep 2024 09:18:42 +0700 Subject: [PATCH 2/4] 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 6d0427f..903cae1 100644 --- a/context_test.go +++ b/context_test.go @@ -14,3 +14,16 @@ func TestContext(t *testing.T) { assert.Equal(t, ctx.Value("Hello").(string), "world") assert.Equal(t, ctx.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 38fa70570b04c4130ea4b8042cae0a0347f256f7 Mon Sep 17 00:00:00 2001 From: Danial Date: Thu, 12 Sep 2024 09:29:42 +0700 Subject: [PATCH 3/4] 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 903cae1..6d0427f 100644 --- a/context_test.go +++ b/context_test.go @@ -14,16 +14,3 @@ func TestContext(t *testing.T) { assert.Equal(t, ctx.Value("Hello").(string), "world") assert.Equal(t, ctx.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 3b44987393c32d31ef3c571661b054770f343cdd Mon Sep 17 00:00:00 2001 From: Danial Date: Thu, 12 Sep 2024 09:31:03 +0700 Subject: [PATCH 4/4] test: merge test cases --- context_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/context_test.go b/context_test.go index 6d0427f..c66a95a 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") ctx := httpCtx.Context() assert.Equal(t, ctx.Value("Hello").(string), "world") assert.Equal(t, ctx.Value("Hi").(string), "Goravel") + assert.Equal(t, ctx.Value(1).(string), "one") + assert.Equal(t, ctx.Value(1.1).(string), "one point one") + assert.Equal(t, ctx.Value(customKey), "hello") }