Files
crowsnest/internal/data/FileDatastore.go

134 lines
3.6 KiB
Go
Raw Normal View History

2024-12-27 22:34:43 +01:00
package data
import (
"os"
"maps"
"errors"
"encoding/json"
)
2024-12-28 01:28:36 +01:00
// A very simple datastructure, implementing the IDatastore interface. It uses
// a simple text file to the data as json.
2024-12-27 22:34:43 +01:00
type FileDatastore struct {
path string
}
2024-12-28 01:28:36 +01:00
// Creates a new FileDatastore object, creating the storage file in the
// process.
2024-12-27 22:34:43 +01:00
func NewFileDatastore(path string) (*FileDatastore, error) {
fds := &FileDatastore{ path: path }
if _, err := fds.readMapObj(); err != nil {
if err := fds.writeMapObj(make(map[string]string)); err != nil { return nil, err }
}
return fds, nil
}
2024-12-28 01:28:36 +01:00
// Read the contents of the storage file and convert to a map object. May throw
// an error, if the file does not exit or the file content can not be
// converted.
2024-12-27 22:34:43 +01:00
func (fds *FileDatastore) readMapObj() (map[string]string, error) {
dat, err := os.ReadFile(fds.path)
if err != nil { return nil, err }
var mapobj map[string]string
err = json.Unmarshal(dat, &mapobj)
if err != nil { return nil, err }
return mapobj, nil
}
2024-12-28 01:28:36 +01:00
// Write the map object to the storage file. Will overwrite the content of the
// file. May throw an error, if the file cannot be created or written to.
2024-12-27 22:34:43 +01:00
func (fds *FileDatastore) writeMapObj(m map[string]string) error {
file, err := os.Create(fds.path)
if err != nil { return err }
defer file.Close()
encoder := json.NewEncoder(file)
if err := encoder.Encode(m); err != nil { return err }
return nil
}
2024-12-28 01:28:36 +01:00
2024-12-27 22:34:43 +01:00
// --- implement IDatastore interface ---
2024-12-28 01:28:36 +01:00
// Sets the key value pair given, overwriting if the key already exists. May
// through an error if the file cannot be opened or the contents cannot be
// decoded correctly.
2024-12-27 22:34:43 +01:00
func (fds *FileDatastore) Set(key string, val string) error {
m, err := fds.readMapObj()
if err != nil { return err }
m[key] = val
err = fds.writeMapObj(m)
if err != nil { return err }
return nil
}
2024-12-28 01:28:36 +01:00
// Check if for the given key a entry does exit. May through an error if the
// file cannot be opened or the contents cannot be decoded correctly.
2024-12-27 22:34:43 +01:00
func (fds *FileDatastore) KeyExists(key string) (bool, error) {
m, err := fds.readMapObj()
if err != nil { return false, err }
_, ok := m[key]
return ok, nil
}
2024-12-28 01:28:36 +01:00
// Gets the value for the given key. May through an error if the key does not
// exit, the file cannot be opened or the contents cannot be decoded
// correctly.
2024-12-27 22:34:43 +01:00
func (fds *FileDatastore) Get(key string) (string, error) {
m, err := fds.readMapObj()
if err != nil { return "", err }
val, ok := m[key]
if !ok { return "", errors.New("key not found") }
return val, nil
}
2024-12-28 01:28:36 +01:00
// Gets all the key value pairs from the file and returns them as a map object.
// May through an error if the file cannot be opened or the contents cannot be
// decoded correctly.
2024-12-27 22:34:43 +01:00
func (fds *FileDatastore) GetAll() (map[string]string, error) {
return fds.readMapObj()
}
2024-12-28 01:28:36 +01:00
// Gets all the key the file and returns them as a map object. May through an
// error if the file cannot be opened or the contents cannot be decoded
// correctly.
2024-12-27 22:34:43 +01:00
func (fds *FileDatastore) GetAllKeys() (map[string]bool, error) {
m, err := fds.readMapObj()
if err != nil { return nil, err }
out := make(map[string]bool)
for key := range maps.Keys(m) {
out[key] = true
}
return out, nil
}
2024-12-28 01:28:36 +01:00
// Deletes the entry with the given key. May through an error if the file
// cannot be opened or the contents cannot be decoded or encoded correctly.
func (fds *FileDatastore) Delete(key string) error {
m, err := fds.readMapObj()
if err != nil { return err }
delete(m, key)
err = fds.writeMapObj(m)
if err != nil { return err }
return nil
}