-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
279 lines (210 loc) · 5.54 KB
/
database.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/jwalton/gchalk"
)
type Database struct {
Tables map[string]Table
}
type Table struct {
Name string
File string
DefinitionFile string
Definition map[string]*Field
SchemaFile string
LastAutoID uint
}
type Entity map[string]any
type EntityCollection []Entity
type EntityJSON map[string]string
type Filter struct {
Field string
Operator string
Value any
Apply func(EntityCollection) EntityCollection
}
type Sort struct {
Field string
Order string
}
type PaginatedItems struct {
First int
Last int
Prev int
Next int
Pages int
Count int
Items EntityCollection
}
type EntityIds map[string]uint
func GenerateEntity(entity EntityJSON, table *Table) (Entity, *Table) {
fields := make(Entity, len(entity))
for key, value := range entity {
options := FieldOptions{false, false, false}
fieldName := key
if strings.HasSuffix(key, "!") {
options.Required = true
fieldName = strings.TrimSuffix(key, "!")
} else if strings.HasSuffix(key, "?") {
options.Nullable = true
fieldName = strings.TrimSuffix(key, "?")
} else if strings.HasSuffix(key, "[]") {
options.Children = true
fieldName = strings.TrimSuffix(key, "[]")
}
fields[fieldName], table = GenerateField(fieldName, value, table, options)
}
return fields, table
}
func HydrateDatabase(db *Database) *Database {
now := time.Now()
Debug("Building database...")
var entityJSON EntityJSON
for key, table := range db.Tables {
updated := CreateTable(&table, entityJSON)
db.Tables[key] = *updated
}
elapsed := time.Since(now).String()
Debug("Database is ready! " + gchalk.Italic("("+elapsed+")"))
return db
}
func CreateTable(table *Table, entityJSON EntityJSON) *Table {
filename := table.Name + ".amock.json"
dir := path.Join(DataDir, filename)
schemaDir := path.Join(SchemaDir, table.Name+".amock.schema.json")
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
if _, err = os.Stat(schemaDir); !errors.Is(err, os.ErrNotExist) {
Debug("Table "+gchalk.Bold(table.Name)+" found at "+gchalk.Italic(dir)+" - skipping...", "table", table.Name, "file", dir, "schema", schemaDir)
table.File = dir
table.SchemaFile = schemaDir
var schema []byte
schema, err = os.ReadFile(table.SchemaFile)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(schema, &table.Definition)
if err != nil {
log.Fatal(err)
}
return table
}
}
raw, err := os.ReadFile(table.DefinitionFile)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(raw, &entityJSON)
if err != nil {
log.Fatal(err)
}
entities := make([]Entity, config.InitCount)
for i := 0; i < config.InitCount; i++ {
entities[i], table = GenerateEntity(entityJSON, table)
}
schema, _ := json.Marshal(table.Definition)
_ = os.WriteFile(schemaDir, schema, os.ModePerm)
b, _ := json.Marshal(entities)
_ = os.WriteFile(dir, b, os.ModePerm)
table.File = dir
Debug("Table "+gchalk.Bold(table.Name)+" created at "+gchalk.Italic(dir)+" from file "+gchalk.Bold(table.DefinitionFile), "table", table.Name, "file", dir, "schema", table.DefinitionFile)
return table
}
// func SearchTable(table *Table, filters map[string]any) (EntityCollection, error) {
//
// }
func GetTable(table *Table) ([]byte, error) {
raw, err := os.ReadFile(table.File)
if err != nil {
return nil, fmt.Errorf("could not read file %s: %w", table.File, err)
}
return raw, err
}
func GetEntityById(table *Table, id string) ([]byte, error) {
collection, err := ReadTable(table)
if err != nil {
return nil, err
}
var (
entity *Entity
found bool
)
newId, err := strconv.ParseFloat(id, 64)
if err != nil {
entity, found = FindBy[string](&collection, "id", id)
} else {
entity, found = FindBy[float64](&collection, "id", newId)
}
if !found {
return nil, errors.New("entity not found, id: " + id)
}
b, err := json.Marshal(entity)
if err != nil {
return nil, fmt.Errorf("could not marshal entity: %w", err)
}
return b, err
}
func FindBy[T comparable](collection *EntityCollection, key string, search T) (*Entity, bool) {
for _, entity := range *collection {
Debug("Comparing", "key", key, "search", search, "entity", entity[key])
if entity[key] == search {
Debug("Found entity", "entity", entity)
return &entity, true
}
}
return nil, false
}
func ReadTable(table *Table) (EntityCollection, error) {
raw, err := GetTable(table)
if err != nil {
return nil, err
}
var collection EntityCollection
err = json.Unmarshal(raw, &collection)
if err != nil {
return nil, fmt.Errorf("could not unmarshal file %s: %w", table.File, err)
}
return collection, err
}
func WriteTable(table *Table, collection EntityCollection) error {
b, err := json.Marshal(collection)
if err != nil {
return fmt.Errorf("could not marshal collection: %w", err)
}
err = os.WriteFile(table.File, b, os.ModePerm)
return err
}
func AppendTable(table *Table, entity *Entity) error {
collection, err := ReadTable(table)
if err != nil {
return err
}
collection = append(collection, *entity)
err = WriteTable(table, collection)
return err
}
func RemoveById(table *Table, id string) error {
collection, err := ReadTable(table)
Debug("Removing entity", "id", id, "table", table.Name)
if err != nil {
return err
}
for i, entity := range collection {
if entity["id"] == id {
collection = append(collection[:i], collection[i+1:]...)
break
}
}
err = WriteTable(table, collection)
if err != nil {
return err
}
return nil
}