Save, Del, Now, Keys
This commit is contained in:
parent
0bb1bb4379
commit
d1ea0e52ef
2 changed files with 114 additions and 0 deletions
49
zordfsdb.go
49
zordfsdb.go
|
@ -4,6 +4,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"g.arns.lt/zordsdavini/abcex"
|
||||
)
|
||||
|
@ -195,3 +196,51 @@ func (db *DB) Dec(vpath string) bool {
|
|||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Now(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
now := time.Now()
|
||||
formatted := now.Format(time.RFC3339)
|
||||
|
||||
os.WriteFile(path.Join(fullPath...), []byte(formatted), 0644)
|
||||
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Save(vpath string, value string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
err := os.WriteFile(path.Join(fullPath...), []byte(value), 0644)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Del(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
os.RemoveAll(path.Join(fullPath...))
|
||||
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -107,3 +107,68 @@ func TestDec(t *testing.T) {
|
|||
t.Fatal("counter value get fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNow(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.Now("today")
|
||||
val, found := db.Get("today")
|
||||
fmt.Println(val)
|
||||
if !found || len(val) != 25 {
|
||||
t.Fatal("today value length wrong:", len(val))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.Save("newKey", "newValue")
|
||||
val, found := db.Get("newKey")
|
||||
fmt.Println(val)
|
||||
if !found || val != "newValue" {
|
||||
t.Fatal("newKey value wrong")
|
||||
}
|
||||
|
||||
db.Save("object.newKey", "newValue2")
|
||||
val, found = db.Get("object.newKey")
|
||||
fmt.Println(val)
|
||||
if !found || val != "newValue2" {
|
||||
t.Fatal("object.newKey value wrong")
|
||||
}
|
||||
|
||||
success := db.Save("object2.newKey", "newValue2")
|
||||
if success {
|
||||
t.Fatal("object2.newKey value created - should not")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDel(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.Del("counter")
|
||||
_, found := db.Get("counter")
|
||||
if found {
|
||||
t.Fatal("delete failed")
|
||||
}
|
||||
|
||||
db.Del("list.2")
|
||||
_, found = db.Get("list.2.key2")
|
||||
if found {
|
||||
t.Fatal("delete failed #2")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue