-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
51 lines (42 loc) · 1.12 KB
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package mem
import (
"code.google.com/p/go-uuid/uuid"
"github.com/jllopis/try5/account"
"github.com/jllopis/try5/store"
)
type MemStore struct {
accounts map[string]*account.Account
status int
}
func NewMemStore() *MemStore {
return &MemStore{accounts: make(map[string]*account.Account, 10), status: store.CONNECTED}
}
func (s *MemStore) Status() (int, string) {
return s.status, store.StatusStr[s.status]
}
func (s *MemStore) LoadAllAccounts() ([]*account.Account, error) {
accounts := make([]*account.Account, len(s.accounts))
for _, v := range s.accounts {
accounts = append(accounts, v)
}
return accounts, nil
}
func (s *MemStore) LoadAccount(uuid string) (*account.Account, error) {
return s.accounts[uuid], nil
}
func (s *MemStore) SaveAccount(account *account.Account) (*account.Account, error) {
if account.UID == nil {
*account.UID = uuid.New()
}
s.accounts[*account.UID] = account
return account, nil
}
func (s *MemStore) DeleteAccount(uuid string) (int, error) {
delete(s.accounts, uuid)
return 1, nil
}
func (s *MemStore) Close() error {
s.accounts = nil
s.status = store.DISCONNECTED
return nil
}