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

ref: use any as the key type WithValue #102

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 3 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
request http.ContextRequest
}

type ctxKey string

func NewContext(ctx *fiber.Ctx) http.Context {
return &Context{instance: ctx}

Check failure on line 25 in context.go

View workflow job for this annotation

GitHub Actions / ubuntu (1.21)

cannot use &Context{…} (value of type *Context) as "github.com/goravel/framework/contracts/http".Context value in return statement: *Context does not implement "github.com/goravel/framework/contracts/http".Context (wrong type for method WithValue)

Check failure on line 25 in context.go

View workflow job for this annotation

GitHub Actions / codecov

cannot use &Context{…} (value of type *Context) as "github.com/goravel/framework/contracts/http".Context value in return statement: *Context does not implement "github.com/goravel/framework/contracts/http".Context (wrong type for method WithValue)

Check failure on line 25 in context.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &Context{…} (value of type *Context) as "github.com/goravel/framework/contracts/http".Context value in return statement: *Context does not implement "github.com/goravel/framework/contracts/http".Context (wrong type for method WithValue)

Check failure on line 25 in context.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &Context{…} (value of type *Context) as "github.com/goravel/framework/contracts/http".Context value in return statement: *Context does not implement "github.com/goravel/framework/contracts/http".Context (wrong type for method WithValue)

Check failure on line 25 in context.go

View workflow job for this annotation

GitHub Actions / windows (1.21)

cannot use &Context{…} (value of type *Context) as "github.com/goravel/framework/contracts/http".Context value in return statement: *Context does not implement "github.com/goravel/framework/contracts/http".Context (wrong type for method WithValue)
}

func (c *Context) Request() http.ContextRequest {
Expand All @@ -39,8 +37,8 @@
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)
mdanialr marked this conversation as resolved.
Show resolved Hide resolved
c.instance.SetUserContext(ctx)
}

Expand All @@ -61,11 +59,7 @@
}

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 {
Expand Down
9 changes: 9 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Loading