-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoryjdb.go
79 lines (67 loc) · 1.54 KB
/
memoryjdb.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
74
75
76
77
78
79
package jdb
import (
"github.com/egustafson/uberwerks/jsondb-go/jsondb"
"github.com/google/uuid"
)
// memoryJDB is an in-memory implementation of a JDB
type memoryJDB struct {
db map[jsondb.JID]jsondb.JSONObj
}
// static check: a memoryJDB ptr isA JDB interface
var _ JDB = (*memoryJDB)(nil)
func NewMemoryJDB() JDB {
return &memoryJDB{
db: make(map[jsondb.JID]jsondb.JSONObj),
}
}
func InitMemoryJDB(data []map[string]any) JDB {
jdb := NewMemoryJDB()
_ = jdb.Load(data)
return jdb
}
func (jdb *memoryJDB) Ping() error {
return nil
}
func (jdb *memoryJDB) List() ([]jsondb.JID, error) {
l := make([]jsondb.JID, 0, len(jdb.db))
for k := range jdb.db {
l = append(l, jsondb.JID(k))
}
return l, nil
}
func (jdb *memoryJDB) Put(jo jsondb.JSONObj) (jsondb.JSONObj, error) {
jid, ok := jo[jsondb.IDKey].(string)
if !ok {
jid = uuid.NewString()
jo[jsondb.IDKey] = jid
}
jdb.db[jsondb.JID(jid)] = jo
return jo, nil
}
func (jdb *memoryJDB) Get(id jsondb.JID) (jo jsondb.JSONObj, ok bool, err error) {
jo, ok = jdb.db[id]
return
}
func (jdb *memoryJDB) Del(id jsondb.JID) (ok bool, err error) {
if _, ok = jdb.db[id]; ok {
delete(jdb.db, id)
}
return
}
func (jdb *memoryJDB) Load(data []map[string]any) error {
for _, obj := range data {
jobj := make(map[jsondb.JID]any)
for k, v := range obj {
jobj[jsondb.JID(k)] = v
}
jdb.Put(jobj)
}
return nil
}
func (jdb *memoryJDB) Dump() ([]jsondb.JSONObj, error) {
objs := make([]jsondb.JSONObj, 0, len(jdb.db))
for _, v := range jdb.db {
objs = append(objs, v)
}
return objs, nil
}