This commit is contained in:
+39
-1
@@ -1,6 +1,44 @@
|
||||
package localdb
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
common "access-manager-api/app/common"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func (db *DB) load(localDBPath string) error {
|
||||
db.data = make(map[string]common.Pool)
|
||||
db.path = localDBPath
|
||||
|
||||
root, err := os.OpenRoot(".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer root.Close()
|
||||
|
||||
content, err := root.ReadFile(localDBPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &db.data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) save() error {
|
||||
localDBPath := db.path
|
||||
// write to file with pretty print for readability reasons
|
||||
json, err := json.MarshalIndent(db.data, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.WriteFile(localDBPath, []byte(json), 0600)
|
||||
return err
|
||||
}
|
||||
|
||||
// MergeNonZero overwrites fields in dst with fields in src iff src is not a zero value.
|
||||
func MergeNonZero[T any](dst *T, src *T) {
|
||||
|
||||
Reference in New Issue
Block a user