-
Notifications
You must be signed in to change notification settings - Fork 13
/
store_memory.go
61 lines (47 loc) · 1.1 KB
/
store_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
52
53
54
55
56
57
58
59
60
61
package store
import (
"sort"
"strconv"
)
type MemoryStore struct {
db map[string]Configuration
}
var dbCounter int
func NewMemoryStore() Store {
dbCounter = 0
return MemoryStore{db: make(map[string]Configuration)}
}
func (store MemoryStore) Put(name string, value string, checksum string) (string, error) {
config := Configuration{
Name: name,
Value: value,
ID: strconv.Itoa(dbCounter),
ParameterChecksum: checksum,
}
dbCounter++
store.db[config.ID] = config
return config.ID, nil
}
func (store MemoryStore) GetByName(name string) (Configurations, error) {
var results Configurations
for _, config := range store.db {
if config.Name == name {
results = append(results, config)
}
}
sort.Sort(results)
return results, nil
}
func (store MemoryStore) GetByID(id string) (Configuration, error) {
return store.db[id], nil
}
func (store MemoryStore) Delete(name string) (int, error) {
deletedCount := 0
for _, config := range store.db {
if config.Name == name {
delete(store.db, config.ID)
deletedCount++
}
}
return deletedCount, nil
}