-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
62 lines (50 loc) · 1.17 KB
/
store.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
52
53
54
55
56
57
58
59
60
61
62
package fitcha
import (
"sync"
)
type Storage interface {
Add(feature *Feature) error
Update(feature *Feature) error
Find(featureName string) (*Feature, error)
Exists(featureName string) (bool, error)
List() []*Feature
}
type inmemoryStore struct {
mu sync.Mutex
features map[string]*Feature
}
func NewInMemoryStorage() Storage {
return &inmemoryStore{
features: make(map[string]*Feature),
}
}
func (m *inmemoryStore) Add(feature *Feature) error {
m.mu.Lock()
defer m.mu.Unlock()
m.features[string(feature.Name)] = feature
return nil
}
func (m *inmemoryStore) Update(feature *Feature) error {
m.mu.Lock()
defer m.mu.Unlock()
m.features[string(feature.Name)] = feature
return nil
}
func (m *inmemoryStore) Find(featureName string) (*Feature, error) {
feature, ok := m.features[featureName]
if feature == nil || !ok {
return nil, ErrFeatureDoesNotExist
}
return feature, nil
}
func (m *inmemoryStore) Exists(featureName string) (bool, error) {
_, ok := m.features[featureName]
return ok, nil
}
func (m *inmemoryStore) List() []*Feature {
features := make([]*Feature, 0)
for _, f := range m.features {
features = append(features, f)
}
return features
}