-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage.go
64 lines (54 loc) · 1.12 KB
/
storage.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
package goui
import (
"encoding/json"
"github.com/fipress/fml"
"sync"
)
const filename = "store"
type storage struct {
store *fml.FML
}
var store *storage
var once sync.Once
func Storage() *storage {
once.Do(func() {
f, err := fml.Load(filename)
if err != nil {
Log("storage - open file failed:", err)
f = fml.NewFml()
}
store = &storage{f}
})
return store
}
func (s *storage) Put(key string, v interface{}) {
s.store.SetValue(key, v)
s.store.WriteToFile(filename)
}
func (s *storage) GetInt(key string) int {
return s.store.GetInt(key)
}
func (s *storage) GetString(key string) string {
return s.store.GetString(key)
}
func (s *storage) PutStruct(key string, v interface{}) (err error) {
b, err := json.Marshal(v)
if err != nil {
Log("Put struct - marshal error:", err)
return
}
s.store.SetValue(key, string(b))
s.store.WriteToFile(filename)
return
}
func (s *storage) GetStruct(key string, v interface{}) (err error) {
str := s.store.GetString(key)
if str != "" {
err = json.Unmarshal([]byte(str), v)
if err != nil {
Log("Get struct - unmarshal error:", err)
return
}
}
return
}