Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change key type to any for context WithValue #630

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}

mdanialr marked this conversation as resolved.
Show resolved Hide resolved
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
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
Loading