61 lines
966 B
Go
61 lines
966 B
Go
package users
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
var _ UserStore = &MemoryUserStore{}
|
|
|
|
type MemoryUserStore struct {
|
|
users map[string]*User
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func NewMemoryUserStore() *MemoryUserStore {
|
|
return &MemoryUserStore{users: make(map[string]*User)}
|
|
}
|
|
|
|
func (s *MemoryUserStore) Get(username string) (*User, error) {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
user, ok := s.users[username]
|
|
|
|
if !ok {
|
|
return nil, fmt.Errorf("no such user: %s", username)
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (s *MemoryUserStore) Store(user *User) error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
s.users[user.Username] = user
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryUserStore) Delete(username string) error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
delete(s.users, username)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryUserStore) List() ([]string, error) {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
ids := make([]string, 0, len(s.users))
|
|
for k := range s.users {
|
|
ids = append(ids, k)
|
|
}
|
|
|
|
return ids, nil
|
|
}
|