Skip to content

Commit

Permalink
feat: change key type to any for context WithValue (#630)
Browse files Browse the repository at this point in the history
* ref: change the signature to accept type `any` instead of `string`

* test: update test for custom key

* test: update some test cases in other parts of framework

* test: merge similar test cases to one Test Function

* rerun mockery
  • Loading branch information
mdanialr authored Sep 12, 2024
1 parent d791838 commit 6543aea
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
6 changes: 3 additions & 3 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand All @@ -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),
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions http/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ func (s *ContextTestSuite) TestContext() {
}

func (s *ContextTestSuite) TestWithValue() {
var myKey struct{}
s.ctx.WithValue("Hello", "world")
s.ctx.WithValue(myKey, "hola")
s.ctx.WithValue(1, "hi")
s.ctx.WithValue(2.2, "hey")
s.Equal("world", s.ctx.Value("Hello"))
s.Equal("hola", s.ctx.Value(myKey))
s.Equal("hi", s.ctx.Value(1))
s.Equal("hey", s.ctx.Value(2.2))
}

func (s *ContextTestSuite) TestRequest() {
Expand Down
2 changes: 1 addition & 1 deletion http/middleware/throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,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")
}
Expand Down
10 changes: 5 additions & 5 deletions mocks/http/Context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion session/middleware/start_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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)
}
Expand Down

0 comments on commit 6543aea

Please sign in to comment.