Newer
Older
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
52
53
54
55
package storage
import "errors"
// Tests for this code are in the "memory" package, since this package doesn't
// define a concrete storage implementation.
// staticClientsStorage is a storage that only allow read-only actions on clients.
// All read actions return from the list of clients stored in memory, not the
// underlying
type staticClientsStorage struct {
Storage
// A read-only set of clients.
clients []Client
clientsByID map[string]Client
}
// WithStaticClients returns a storage with a read-only set of clients. Write actions,
// such as creating other clients, will fail.
//
// In the future the returned storage may allow creating and storing additional clients
// in the underlying storage.
func WithStaticClients(s Storage, staticClients []Client) Storage {
clientsByID := make(map[string]Client, len(staticClients))
for _, client := range staticClients {
clientsByID[client.ID] = client
}
return staticClientsStorage{s, staticClients, clientsByID}
}
func (s staticClientsStorage) GetClient(id string) (Client, error) {
if client, ok := s.clientsByID[id]; ok {
return client, nil
}
return Client{}, ErrNotFound
}
func (s staticClientsStorage) ListClients() ([]Client, error) {
clients := make([]Client, len(s.clients))
copy(clients, s.clients)
return clients, nil
}
func (s staticClientsStorage) CreateClient(c Client) error {
return errors.New("static clients: read-only cannot create client")
}
func (s staticClientsStorage) DeleteClient(id string) error {
return errors.New("static clients: read-only cannot delete client")
}
func (s staticClientsStorage) UpdateClient(id string, updater func(old Client) (Client, error)) error {
return errors.New("static clients: read-only cannot update client")
}