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

refactor: change WithValue key type to any #103

Merged
merged 5 commits into from
Sep 13, 2024
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
12 changes: 3 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type Context struct {
request http.ContextRequest
}

type ctxKey string

func NewContext(ctx *fiber.Ctx) http.Context {
return &Context{instance: ctx}
}
Expand All @@ -39,8 +37,8 @@ func (c *Context) Response() http.ContextResponse {
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)
c.instance.SetUserContext(ctx)
}

Expand All @@ -61,11 +59,7 @@ func (c *Context) Err() error {
}

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

func (c *Context) Instance() *fiber.Ctx {
Expand Down
20 changes: 18 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ import (
)

func TestContext(t *testing.T) {
type customKeyType struct{}
var customKey customKeyType

httpCtx := Background()
httpCtx.WithValue("Hello", "world")
httpCtx.WithValue("Hi", "Goravel")
httpCtx.WithValue(customKey, "halo")
httpCtx.WithValue(1, "one")
httpCtx.WithValue(2.2, "two point two")

assert.Equal(t, "world", httpCtx.Value("Hello"))
assert.Equal(t, "Goravel", httpCtx.Value("Hi"))
assert.Equal(t, "halo", httpCtx.Value(customKey))
assert.Equal(t, "one", httpCtx.Value(1))
assert.Equal(t, "two point two", httpCtx.Value(2.2))

assert.Equal(t, httpCtx.Value("Hello").(string), "world")
assert.Equal(t, httpCtx.Value("Hi").(string), "Goravel")
ctx := httpCtx.Context()
assert.Equal(t, "world", ctx.Value("Hello"))
assert.Equal(t, "Goravel", ctx.Value("Hi"))
assert.Equal(t, "halo", ctx.Value(customKey))
assert.Equal(t, "one", ctx.Value(1))
assert.Equal(t, "two point two", ctx.Value(2.2))
mdanialr marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/gofiber/template/html/v2 v2.1.2
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.5
github.com/goravel/framework v1.14.6-0.20240912145232-6543aeadf1bc
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7a
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.5 h1:FItqxRGkBK0h/TIknF24TuMZCtBRaSr3DnQLEzhfvXc=
github.com/goravel/framework v1.14.5/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
github.com/goravel/framework v1.14.6-0.20240912145232-6543aeadf1bc h1:mWObAigy7PhrJkq1uK2KvV34JkTb8gIRxD8RU66MQVU=
github.com/goravel/framework v1.14.6-0.20240912145232-6543aeadf1bc/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
Expand Down