From a6b57ae2aabf6447357f124054b5f5fb8c1d2440 Mon Sep 17 00:00:00 2001 From: Danial Date: Sat, 7 Sep 2024 16:39:23 +0700 Subject: [PATCH 1/4] ref: change the signature to accept type `any` instead of `string` --- contracts/http/context.go | 2 +- http/context.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/http/context.go b/contracts/http/context.go index 6bfe3a18d..86375fce6 100644 --- a/contracts/http/context.go +++ b/contracts/http/context.go @@ -24,7 +24,7 @@ type Context interface { // Context returns the Context Context() context.Context // WithValue add value associated with key in context - WithValue(key string, value any) + WithValue(key any, value any) // Request returns the ContextRequest Request() ContextRequest // Response returns the ContextResponse diff --git a/http/context.go b/http/context.go index 07120b8b8..9fc3123a3 100644 --- a/http/context.go +++ b/http/context.go @@ -26,7 +26,7 @@ func (r *Context) Context() context.Context { return r.Ctx } -func (r *Context) WithValue(key string, value any) { +func (r *Context) WithValue(key any, value any) { //nolint:all r.Ctx = context.WithValue(r.Ctx, key, value) } From fe2da0645b7e4a7a5b154dbe0e06fcb8dd11e78f Mon Sep 17 00:00:00 2001 From: Danial Date: Sat, 7 Sep 2024 16:39:40 +0700 Subject: [PATCH 2/4] test: update test for custom key --- http/context_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/http/context_test.go b/http/context_test.go index 90d5c53df..b9c6d3466 100644 --- a/http/context_test.go +++ b/http/context_test.go @@ -29,6 +29,13 @@ func (s *ContextTestSuite) TestWithValue() { s.Equal("world", s.ctx.Value("Hello")) } +func (s *ContextTestSuite) TestWithValueUsingCustomKey() { + var myKey struct{} + s.ctx.WithValue(myKey, "hola") + s.Equal("hola", s.ctx.Value(myKey)) + s.NotEqual("hola", s.ctx.Value("myKey")) +} + func (s *ContextTestSuite) TestRequest() { s.Nil(s.ctx.Request()) } From 2649383daf25f93db81860325d16331b4760c429 Mon Sep 17 00:00:00 2001 From: Danial Date: Sat, 7 Sep 2024 16:53:43 +0700 Subject: [PATCH 3/4] test: update some test cases in other parts of framework --- auth/auth_test.go | 6 +++--- http/middleware/throttle_test.go | 2 +- session/middleware/start_session_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/auth/auth_test.go b/auth/auth_test.go index 810e426e5..f07ffea17 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -32,7 +32,7 @@ type Context struct { ctx context.Context request http.ContextRequest response http.ContextResponse - values map[string]any + values map[any]any mu sync.RWMutex } @@ -66,7 +66,7 @@ func (mc *Context) Context() context.Context { return mc.ctx } -func (mc *Context) WithValue(key string, value any) { +func (mc *Context) WithValue(key any, value any) { mc.mu.Lock() mc.values[key] = value mc.mu.Unlock() @@ -85,7 +85,7 @@ func Background() http.Context { ctx: context.Background(), request: nil, response: nil, - values: make(map[string]any), + values: make(map[any]any), } } diff --git a/http/middleware/throttle_test.go b/http/middleware/throttle_test.go index adc0964f8..562b770ef 100644 --- a/http/middleware/throttle_test.go +++ b/http/middleware/throttle_test.go @@ -305,7 +305,7 @@ func (r *TestContext) Context() context.Context { panic("do not need to implement it") } -func (r *TestContext) WithValue(key string, value any) { +func (r *TestContext) WithValue(key any, value any) { panic("do not need to implement it") } diff --git a/session/middleware/start_session_test.go b/session/middleware/start_session_test.go index 87a4dba5e..d05daaff7 100644 --- a/session/middleware/start_session_test.go +++ b/session/middleware/start_session_test.go @@ -116,7 +116,7 @@ func (r *TestContext) Context() context.Context { return r.ctx } -func (r *TestContext) WithValue(key string, value any) { +func (r *TestContext) WithValue(key any, value any) { //nolint:all r.ctx = context.WithValue(r.ctx, key, value) } From 69a370a6cf6cac060bcc828d38af1ae960431f23 Mon Sep 17 00:00:00 2001 From: Danial Date: Mon, 9 Sep 2024 07:52:37 +0700 Subject: [PATCH 4/4] test: regenerate mock files --- mocks/http/Context.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mocks/http/Context.go b/mocks/http/Context.go index 44eca4532..ff2f465a8 100644 --- a/mocks/http/Context.go +++ b/mocks/http/Context.go @@ -361,7 +361,7 @@ func (_c *Context_Value_Call) RunAndReturn(run func(interface{}) interface{}) *C } // WithValue provides a mock function with given fields: key, value -func (_m *Context) WithValue(key string, value interface{}) { +func (_m *Context) WithValue(key interface{}, value interface{}) { _m.Called(key, value) } @@ -371,15 +371,15 @@ type Context_WithValue_Call struct { } // WithValue is a helper method to define mock.On call -// - key string +// - key interface{} // - value interface{} func (_e *Context_Expecter) WithValue(key interface{}, value interface{}) *Context_WithValue_Call { return &Context_WithValue_Call{Call: _e.mock.On("WithValue", key, value)} } -func (_c *Context_WithValue_Call) Run(run func(key string, value interface{})) *Context_WithValue_Call { +func (_c *Context_WithValue_Call) Run(run func(key interface{}, value interface{})) *Context_WithValue_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(interface{})) + run(args[0].(interface{}), args[1].(interface{})) }) return _c } @@ -389,7 +389,7 @@ func (_c *Context_WithValue_Call) Return() *Context_WithValue_Call { return _c } -func (_c *Context_WithValue_Call) RunAndReturn(run func(string, interface{})) *Context_WithValue_Call { +func (_c *Context_WithValue_Call) RunAndReturn(run func(interface{}, interface{})) *Context_WithValue_Call { _c.Call.Return(run) return _c }