Skip to content

Commit

Permalink
refactor: change WithValue key type to any (#103)
Browse files Browse the repository at this point in the history
* bump ver goravel framework

* ref: use any as the key type for WithValue

* test: more test cases for WithValue and Context

* test: swap the expected and actual value in assertion

* test: remove type assert
  • Loading branch information
mdanialr authored Sep 13, 2024
1 parent 1c76e56 commit 636f637
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
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)
}

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))
}
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
16 changes: 8 additions & 8 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,16 +484,16 @@ func TestGroup(t *testing.T) {
route2.Get("/middleware/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"id": ctx.Request().Input("id"),
"ctx": ctx.Value("ctx").(string),
"ctx1": ctx.Value("ctx1").(string),
"ctx": ctx.Value("ctx"),
"ctx1": ctx.Value("ctx1"),
})
})
})
route1.Middleware(contextMiddleware2()).Get("/middleware/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"id": ctx.Request().Input("id"),
"ctx": ctx.Value("ctx").(string),
"ctx2": ctx.Value("ctx2").(string),
"ctx": ctx.Value("ctx"),
"ctx2": ctx.Value("ctx2"),
})
})
})
Expand All @@ -511,16 +511,16 @@ func TestGroup(t *testing.T) {
route2.Get("/middleware/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"id": ctx.Request().Input("id"),
"ctx": ctx.Value("ctx").(string),
"ctx1": ctx.Value("ctx1").(string),
"ctx": ctx.Value("ctx"),
"ctx1": ctx.Value("ctx1"),
})
})
})
route1.Middleware(contextMiddleware2()).Get("/middleware/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"id": ctx.Request().Input("id"),
"ctx": ctx.Value("ctx").(string),
"ctx2": ctx.Value("ctx2").(string),
"ctx": ctx.Value("ctx"),
"ctx2": ctx.Value("ctx2"),
})
})
})
Expand Down

0 comments on commit 636f637

Please sign in to comment.