-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.go
54 lines (40 loc) · 874 Bytes
/
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
package store
import "github.com/LegendaryB/go-store/adapters"
type Store[T any] struct {
adapter adapters.Adapter[T]
Data T
}
func New[T any](adapter adapters.Adapter[T]) *Store[T] {
return &Store[T]{
adapter: adapter,
}
}
func NewWithJSONAdapter[T any](opts adapters.JSONAdapterOptions) (*Store[T], error) {
adapter, err := adapters.NewJSONAdapter[T](opts)
if err != nil {
return nil, err
}
return &Store[T]{
adapter: adapter,
}, nil
}
func NewWithInMemoryAdapter[T any]() *Store[T] {
adapter := adapters.NewInMemoryAdapter[T]()
return &Store[T]{
adapter: adapter,
}
}
func (store *Store[T]) Read() error {
data, err := store.adapter.Read()
if err != nil {
return err
}
store.Data = *data
return nil
}
func (store *Store[T]) Write() error {
if err := store.adapter.Write(store.Data); err != nil {
return err
}
return nil
}