-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024-05-05 12:44:09: merge [master:190abc0ed5fa8e683e1e03cbc7ca4ac0a4…
…7a0781];
- Loading branch information
txix
committed
May 5, 2024
1 parent
1e9bc8b
commit df311a8
Showing
6 changed files
with
75 additions
and
4 deletions.
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
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,55 @@ | ||
package store | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
type Store struct { | ||
data map[string]any | ||
lock sync.Locker | ||
} | ||
|
||
func New() *Store { | ||
return &Store{ | ||
data: make(map[string]any), | ||
lock: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (s *Store) Set(key string, value any) { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
|
||
s.data[key] = value | ||
} | ||
|
||
func (s *Store) Range(f func(key string, value any) bool) { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
|
||
for k, v := range s.data { | ||
if !f(k, v) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func Get[T any](store *Store, key string) (T, error) { | ||
store.lock.Lock() | ||
defer store.lock.Unlock() | ||
|
||
var empty T | ||
|
||
value, ok := store.data[key] | ||
if !ok { | ||
return empty, fmt.Errorf("value for key %s not found", key) | ||
} | ||
|
||
casted, ok := value.(T) | ||
if !ok { | ||
return empty, fmt.Errorf("expected type %T, but got %T", empty, value) | ||
} | ||
|
||
return casted, nil | ||
} |