-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
✨ feat: Add support for application state management #3360
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
655677e
:sparkles: feat: add support for application state management
efectn 8fb7753
increase test coverage
efectn 5af18cc
fix linter
efectn 6fa12bb
Fix typo
gaby 7f0afef
add GetStateWithDefault helper
efectn ff5f09d
add docs
efectn 8f5aeed
update what's new
efectn cbd2f09
add has method
efectn 1dbd1cc
fix linter
efectn 804e8f8
update
efectn bf89799
Merge branch 'main' into state-management
gaby 99d8db9
Merge branch 'main' into state-management
gaby 7299299
Add missing helpers for golang built-in types
gaby 4ff6a2e
Fix lint issues
gaby 4c48576
Fix unit-tests. Update documentation
gaby 27f9563
Fix docs, add missing benchmarks
gaby 9ea8b22
Fix tests file
gaby a9c29b1
Update default example and test
gaby a5c07b7
Apply suggestions from code review
ReneWerner87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package fiber | ||
|
||
import "sync" | ||
|
||
// 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 | ||
} | ||
|
||
// NewState creates a new instance of State. | ||
func newState() *State { | ||
return &State{ | ||
dependencies: sync.Map{}, | ||
} | ||
} | ||
|
||
// Set sets a key-value pair in the State. | ||
func (s *State) Set(key string, value any) { | ||
s.dependencies.Store(key, value) | ||
} | ||
|
||
// Get retrieves a value from the State. | ||
func (s *State) Get(key string) (any, bool) { | ||
return s.dependencies.Load(key) | ||
} | ||
|
||
// GetString retrieves a string value from the State. | ||
func (s *State) GetString(key string) (string, bool) { | ||
dep, ok := s.Get(key) | ||
if ok { | ||
depString, okCast := dep.(string) | ||
return depString, okCast | ||
} | ||
|
||
return "", false | ||
} | ||
|
||
// GetInt retrieves an int value from the State. | ||
func (s *State) GetInt(key string) (int, bool) { | ||
dep, ok := s.Get(key) | ||
if ok { | ||
depInt, okCast := dep.(int) | ||
return depInt, okCast | ||
} | ||
|
||
return 0, false | ||
} | ||
|
||
// GetBool retrieves a bool value from the State. | ||
func (s *State) GetBool(key string) (bool, bool) { | ||
dep, ok := s.Get(key) | ||
if ok { | ||
depBool, okCast := dep.(bool) | ||
return depBool, okCast | ||
} | ||
|
||
return false, false | ||
} | ||
|
||
// GetFloat64 retrieves a float64 value from the State. | ||
func (s *State) GetFloat64(key string) (float64, bool) { | ||
dep, ok := s.Get(key) | ||
if ok { | ||
depFloat64, okCast := dep.(float64) | ||
return depFloat64, okCast | ||
} | ||
|
||
return 0, false | ||
} | ||
|
||
// MustGet retrieves a value from the State and panics if the key is not found. | ||
func (s *State) MustGet(key string) any { | ||
if dep, ok := s.Get(key); ok { | ||
return dep | ||
} | ||
|
||
panic("state: dependency not found!") | ||
} | ||
|
||
// MustGetString retrieves a string value from the State and panics if the key is not found. | ||
func (s *State) Delete(key string) { | ||
s.dependencies.Delete(key) | ||
} | ||
|
||
// Reset resets the State. | ||
func (s *State) Clear() { | ||
s.dependencies.Clear() | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Keys retrieves all the keys from the State. | ||
func (s *State) Keys() []string { | ||
keys := make([]string, 0) | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s.dependencies.Range(func(key, _ any) bool { | ||
keys = append(keys, key.(string)) | ||
return true | ||
}) | ||
|
||
return keys | ||
} | ||
|
||
// Len retrieves the number of dependencies in the State. | ||
func (s *State) Len() int { | ||
length := 0 | ||
s.dependencies.Range(func(_, _ any) bool { | ||
length++ | ||
return true | ||
}) | ||
|
||
return length | ||
} | ||
|
||
// GetState retrieves a value from the State and casts it to the desired type. | ||
func GetState[T any](s *State, key string) (T, bool) { | ||
dep, ok := s.Get(key) | ||
|
||
if ok { | ||
depT, okCast := dep.(T) | ||
return depT, okCast | ||
} | ||
|
||
var zeroVal T | ||
return zeroVal, false | ||
} | ||
|
||
// MustGetState retrieves a value from the State and casts it to the desired type, panicking if the key is not found. | ||
func MustGetState[T any](s *State, key string) T { | ||
dep, ok := GetState[T](s, key) | ||
if !ok { | ||
panic("state: dependency not found!") | ||
} | ||
|
||
return dep | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.