-
Notifications
You must be signed in to change notification settings - Fork 241
/
mockstore.go
73 lines (59 loc) · 1.41 KB
/
mockstore.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
63
64
65
66
67
68
69
70
71
72
73
package store
import (
"encoding/json"
"fmt"
"time"
)
type mockStore struct {
lockFilePath string
data map[string]*json.RawMessage
}
// NewMockStore creates a new jsonFileStore object, accessed as a KeyValueStore.
func NewMockStore(lockFilePath string) KeyValueStore {
return &mockStore{
lockFilePath: lockFilePath,
data: make(map[string]*json.RawMessage),
}
}
func (ms *mockStore) Exists() bool {
return ms.data == nil
}
// Read restores the value for the given key from persistent store.
func (ms *mockStore) Read(key string, value interface{}) error {
if _, ok := ms.data[key]; !ok {
return ErrStoreEmpty
}
err := json.Unmarshal(*ms.data[key], value)
if err != nil {
return fmt.Errorf("%w", err)
}
return nil
}
func (ms *mockStore) Write(key string, value interface{}) error {
var raw json.RawMessage
raw, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("%w", err)
}
ms.data[key] = &raw
return nil
}
func (ms *mockStore) Flush() error {
return nil
}
func (ms *mockStore) Lock(duration time.Duration) error {
return nil
}
func (ms *mockStore) Unlock() error {
return nil
}
func (ms *mockStore) GetModificationTime() (time.Time, error) {
return time.Time{}, nil
}
func (ms *mockStore) GetLockFileModificationTime() (time.Time, error) {
return time.Time{}, nil
}
func (ms *mockStore) GetLockFileName() string {
return ms.lockFilePath
}
func (ms *mockStore) Remove() {}