From a9c29b121c4ce65648d6093c9239cfec4d637a16 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez Date: Sun, 30 Mar 2025 21:31:41 -0400 Subject: [PATCH] Update default example and test --- docs/api/state.md | 15 ++++++++------- state_test.go | 6 +++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/api/state.md b/docs/api/state.md index 88dc93fe..9d2cc3b7 100644 --- a/docs/api/state.md +++ b/docs/api/state.md @@ -478,6 +478,7 @@ package main import ( "fmt" + "github.com/gofiber/fiber/v3" ) @@ -489,7 +490,7 @@ func main() { // Middleware: Increase counter for every request. app.Use(func(c fiber.Ctx) error { - count := c.App().GetStateWithDefault(app.State(), "requestCount", 0) + count, _ := c.App().State().GetInt("requestCount") app.State().Set("requestCount", count+1) return c.Next() }) @@ -499,7 +500,7 @@ func main() { }) app.Get("/stats", func(c fiber.Ctx) error { - count := c.App().GetStateWithDefault(c.App().State(), "requestCount", 0) + count, _ := c.App().State().Get("requestCount") return c.SendString(fmt.Sprintf("Total requests: %d", count)) }) @@ -532,7 +533,7 @@ func main() { // Set environment-specific configurations. if environment == "development" { - app.State().Set("apiUrl", "http://localhost:8080/api") + app.State().Set("apiUrl", "http://localhost:8080/api") app.State().Set("debug", true) } else { app.State().Set("apiUrl", "https://api.production.com") @@ -542,8 +543,8 @@ func main() { app.Get("/config", func(c fiber.Ctx) error { config := map[string]any{ "environment": environment, - "apiUrl": c.App().GetStateWithDefault(app.State(), "apiUrl", ""), - "debug": c.App().GetStateWithDefault(app.State(), "debug", false), + "apiUrl": fiber.GetStateWithDefault(app.State(), "apiUrl", ""), + "debug": fiber.GetStateWithDefault(app.State(), "debug", false), } return c.JSON(config) }) @@ -600,7 +601,7 @@ func main() { } // Save the user to the database. - rdb, ok := c.App().GetState[*redis.Client](app.State(), "redis") + rdb, ok := fiber.GetState[*redis.Client](app.State(), "redis") if !ok { return c.Status(fiber.StatusInternalServerError).SendString("Redis client not found") } @@ -618,7 +619,7 @@ func main() { app.Get("/user/:id", func(c fiber.Ctx) error { id := c.Params("id") - rdb, ok := c.App().GetState[*redis.Client](app.State(), "redis") + rdb, ok := fiber.GetState[*redis.Client](app.State(), "redis") if !ok { return c.Status(fiber.StatusInternalServerError).SendString("Redis client not found") } diff --git a/state_test.go b/state_test.go index 8a3287dc..e96ea0dd 100644 --- a/state_test.go +++ b/state_test.go @@ -13,13 +13,13 @@ func TestState_SetAndGet_WithApp(t *testing.T) { app := New() // test setting and getting a value - app.state.Set("foo", "bar") - val, ok := app.state.Get("foo") + app.State().Set("foo", "bar") + val, ok := app.State().Get("foo") require.True(t, ok) require.Equal(t, "bar", val) // test key not found - _, ok = app.state.Get("unknown") + _, ok = app.State().Get("unknown") require.False(t, ok) }