-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtables.go
169 lines (158 loc) · 3.76 KB
/
tables.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package tinydb
import "reflect"
// Table the table of the database.
type Table[T any] struct {
name string
database *Database
}
// GetTable get a specific table from the database.
func GetTable[T any](database *Database) *Table[T] {
var t T
name := reflect.TypeOf(t).Name()
return &Table[T]{name, database}
}
// DropTable drop a specific table from the database.
func DropTable[T any](database *Database) error {
database.Lock()
defer database.Unlock()
var t T
name := reflect.TypeOf(t).Name()
var data = make(map[string][]interface{})
err := database.storage.Read(data)
if err != nil {
return err
}
delete(data, name)
return database.storage.Write(data)
}
// Insert a new document into table.
func (tbl *Table[T]) Insert(items ...T) error {
tbl.database.Lock()
defer tbl.database.Unlock()
var data = make(map[string][]interface{})
err := tbl.database.storage.Read(&data)
if err != nil {
return err
}
if _, ok := data[tbl.name]; !ok {
data[tbl.name] = nil
}
for i := range items {
data[tbl.name] = append(data[tbl.name], items[i])
}
return tbl.database.storage.Write(data)
}
// Delete delete all matching documents in table.
func (tbl *Table[T]) Delete(condition func(T) bool) ([]T, error) {
tbl.database.Lock()
defer tbl.database.Unlock()
var data = make(map[string][]T)
err := tbl.database.storage.Read(&data)
if err != nil {
return nil, err
}
var out []T
if _, ok := data[tbl.name]; !ok {
return nil, nil
}
for i := range data[tbl.name] {
item := data[tbl.name][i]
if !condition(item) {
out = append(out, item)
}
}
var other = make(map[string][]interface{})
err = tbl.database.storage.Read(&other)
if err != nil {
return nil, err
}
if _, ok := data[tbl.name]; !ok {
return nil, nil
}
data[tbl.name] = out
return out, tbl.database.storage.Write(data)
}
// Update all matching documents with updater.
func (tbl *Table[T]) Update(updater func(T) T, condition func(T) bool) error {
tbl.database.Lock()
defer tbl.database.Unlock()
var data = make(map[string][]T)
err := tbl.database.storage.Read(&data)
if err != nil {
return err
}
var out []T
if _, ok := data[tbl.name]; !ok {
return nil
}
for i := range data[tbl.name] {
item := data[tbl.name][i]
if condition(item) {
item = updater(item)
}
out = append(out, item)
}
var other = make(map[string][]interface{})
err = tbl.database.storage.Read(&other)
if err != nil {
return err
}
if _, ok := data[tbl.name]; !ok {
return nil
}
data[tbl.name] = out
return tbl.database.storage.Write(data)
}
// MustSelect for all documents matching condition without error.
func (tbl *Table[T]) MustSelect(condition func(T) bool) []T {
out, err := tbl.Select(condition)
if err != nil {
panic(err)
}
return out
}
// Select for all documents matching condition.
func (tbl *Table[T]) Select(condition func(T) bool) ([]T, error) {
var data = make(map[string][]T)
err := tbl.database.storage.Read(&data)
if err != nil {
return nil, err
}
var out []T
if _, ok := data[tbl.name]; !ok {
return nil, nil
}
for i := range data[tbl.name] {
item := data[tbl.name][i]
if condition(item) {
out = append(out, item)
}
}
return out, nil
}
// MustAny determines whether a documents contain any elements without error.
func (tbl *Table[T]) MustAny(condition func(T) bool) bool {
out, err := tbl.Any(condition)
if err != nil {
panic(err)
}
return out
}
// Any determines whether a documents contain any elements.
func (tbl *Table[T]) Any(condition func(T) bool) (bool, error) {
var data = make(map[string][]T)
err := tbl.database.storage.Read(&data)
if err != nil {
return false, err
}
if _, ok := data[tbl.name]; !ok {
return false, nil
}
for i := range data[tbl.name] {
item := data[tbl.name][i]
if condition(item) {
return true, nil
}
}
return false, nil
}