state-management
Muhammed Efe Cetin 2025-03-19 15:29:50 +03:00
parent 1dbd1cc1d5
commit 804e8f8b75
No known key found for this signature in database
GPG Key ID: 0AA4D45CBAA86F73
2 changed files with 6 additions and 6 deletions

View File

@ -14,7 +14,7 @@ Below is the detailed description of all public methods and usage examples.
// State is a key-value store for Fiber's app in order to be used as a global storage for the app's dependencies.
// It's a thread-safe implementation of a map[string]any, using sync.Map.
type State struct {
dependencies sync.Map
dependencies sync.Map
}
```
@ -377,7 +377,7 @@ func main() {
// Check the Redis connection.
if err := rdb.Ping(ctx).Err(); err != nil {
log.Fatalf("Could not connect to Redis: %v", err)
log.Fatalf("Could not connect to Redis: %v", err)
}
// Inject the Redis client into Fiber's State for dependency injection.
@ -426,4 +426,4 @@ func main() {
app.Listen(":3000")
}
```
```

View File

@ -247,15 +247,15 @@ func Test_GetStateWithDefault(t *testing.T) {
st := newState()
st.Set("flag", true)
flag := GetStateWithDefault[bool](st, "flag", false)
flag := GetStateWithDefault(st, "flag", false)
require.True(t, flag)
// mismatched type should return the default value
str := GetStateWithDefault[string](st, "flag", "default")
str := GetStateWithDefault(st, "flag", "default")
require.Equal(t, "default", str)
// missing key should return the default value
flag = GetStateWithDefault[bool](st, "missing", false)
flag = GetStateWithDefault(st, "missing", false)
require.False(t, flag)
}