-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
✨ feat: add support for application state management #3360
Open
efectn
wants to merge
10
commits into
main
Choose a base branch
from
state-management
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 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 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 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 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 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,141 @@ | ||
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) (value, ok bool) { //nolint:nonamedreturns // Better idea to use named returns here | ||
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() | ||
} | ||
|
||
// Keys retrieves all the keys from the State. | ||
func (s *State) Keys() []string { | ||
keys := make([]string, 0) | ||
s.dependencies.Range(func(key, _ any) bool { | ||
keyStr, ok := key.(string) | ||
if !ok { | ||
return false | ||
} | ||
|
||
keys = append(keys, keyStr) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we can preallocate the keys slice using
s.Len()
to reduce the memory allocation; however, it will make the method a little bit slower.