commit c6e2b0b5a41a8d8839b79663b5f55321ba8e2060
parent 0f5b1195d745e627f20adc6aa727b82fe8d54ee3
Author: lash <dev@holbrook.no>
Date: Fri, 30 Aug 2024 21:19:17 +0100
Fix gdbm example to illustrate mix use of db and fs resource
Diffstat:
6 files changed, 46 insertions(+), 16 deletions(-)
diff --git a/db/gdbm.go b/db/gdbm.go
@@ -3,6 +3,7 @@ package db
import (
"context"
"errors"
+ "os"
gdbm "github.com/graygnuorg/go-gdbm"
)
@@ -16,7 +17,17 @@ type GdbmDb struct {
// Connect implements Db
func(gdb *GdbmDb) Connect(ctx context.Context, connStr string) error {
- db, err := gdbm.Open(connStr, gdbm.ModeWrcreat)
+ var db *gdbm.Database
+ _, err := os.Stat(connStr)
+ if err != nil {
+ if !errors.Is(os.ErrNotExist, err) {
+ return err
+ }
+ db, err = gdbm.Open(connStr, gdbm.ModeWrcreat)
+ } else {
+ db, err = gdbm.Open(connStr, gdbm.ModeWriter | gdbm.ModeReader)
+ }
+
if err != nil {
return err
}
diff --git a/engine/engine_test.go b/engine/engine_test.go
@@ -82,7 +82,7 @@ func(fs FsWrapper) set_lang(ctx context.Context, sym string, input []byte) (reso
}, nil
}
-func(fs FsWrapper) GetCode(sym string) ([]byte, error) {
+func(fs FsWrapper) GetCode(ctx context.Context, sym string) ([]byte, error) {
sym += ".bin"
fp := path.Join(fs.Path, sym)
r, err := ioutil.ReadFile(fp)
diff --git a/examples/gdbm/main.go b/examples/gdbm/main.go
@@ -12,6 +12,7 @@ import (
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state"
+ "git.defalsify.org/vise.git/db"
)
var (
@@ -27,12 +28,30 @@ func do(ctx context.Context, sym string, input []byte) (resource.Result, error)
}
func main() {
- var err error
+ ctx := context.Background()
root := "root"
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir)
st := state.NewState(0)
- rs := resource.NewGdbmResource(dbFile)
+ store := &db.GdbmDb{}
+ err := store.Connect(ctx, dbFile)
+ if err != nil {
+ panic(err)
+ }
+
+ tg, err := resource.NewDbFuncGetter(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN)
+ if err != nil {
+ panic(err)
+ }
+ rs := resource.NewMenuResource()
+ rs = rs.WithTemplateGetter(tg.GetTemplate)
+ rs = rs.WithCodeGetter(tg.GetCode)
+
+ rsf := resource.NewFsResource(scriptDir)
+ rsf.AddLocalFunc("do", do)
+ rs = rs.WithMenuGetter(rsf.GetMenu)
+ rs = rs.WithEntryFuncGetter(rsf.FuncFor)
+
ca := cache.NewCache()
if err != nil {
panic(err)
@@ -41,10 +60,8 @@ func main() {
Root: "root",
Language: "nor",
}
- ctx := context.Background()
en := engine.NewEngine(ctx, cfg, &st, rs, ca)
- rs.AddLocalFunc("do", do)
_, err = en.Init(ctx)
if err != nil {
diff --git a/resource/fs.go b/resource/fs.go
@@ -60,7 +60,7 @@ func(fsr FsResource) GetTemplate(ctx context.Context, sym string) (string, error
return strings.TrimSpace(s), err
}
-func(fsr FsResource) GetCode(sym string) ([]byte, error) {
+func(fsr FsResource) GetCode(ctx context.Context, sym string) ([]byte, error) {
fb := sym + ".bin"
fp := path.Join(fsr.Path, fb)
return ioutil.ReadFile(fp)
@@ -95,12 +95,6 @@ func(fsr FsResource) GetMenu(ctx context.Context, sym string) (string, error) {
return strings.TrimSpace(s), err
}
-func(fsr *FsResource) AddLocalFunc(sym string, fn EntryFunc) {
- if fsr.fns == nil {
- fsr.fns = make(map[string]EntryFunc)
- }
- fsr.fns[sym] = fn
-}
func(fsr FsResource) FuncFor(sym string) (EntryFunc, error) {
fn, ok := fsr.fns[sym]
@@ -154,3 +148,10 @@ func(fsr FsResource) getFuncNoCtx(sym string, input []byte, language *lang.Langu
Content: strings.TrimSpace(s),
}, nil
}
+
+func(fsr *FsResource) AddLocalFunc(sym string, fn EntryFunc) {
+ if fsr.fns == nil {
+ fsr.fns = make(map[string]EntryFunc)
+ }
+ fsr.fns[sym] = fn
+}
diff --git a/resource/resource.go b/resource/resource.go
@@ -87,3 +87,4 @@ func(m MenuResource) GetTemplate(ctx context.Context, sym string) (string, error
func(m MenuResource) GetMenu(ctx context.Context, sym string) (string, error) {
return m.menuFunc(ctx, sym)
}
+
diff --git a/vm/runner.go b/vm/runner.go
@@ -248,7 +248,7 @@ func(vm *Vm) runCatch(ctx context.Context, b []byte) ([]byte, error) {
}
Logg.InfoCtxf(ctx, "catch!", "flag", sig, "sym", sym, "target", actualSym, "mode", mode)
sym = actualSym
- bh, err := vm.rs.GetCode(sym)
+ bh, err := vm.rs.GetCode(ctx, sym)
if err != nil {
return b, err
}
@@ -323,7 +323,7 @@ func(vm *Vm) runMove(ctx context.Context, b []byte) ([]byte, error) {
if err != nil {
return b, err
}
- code, err := vm.rs.GetCode(sym)
+ code, err := vm.rs.GetCode(ctx, sym)
if err != nil {
return b, err
}
@@ -385,7 +385,7 @@ func(vm *Vm) runInCmp(ctx context.Context, b []byte) ([]byte, error) {
vm.Reset()
- code, err := vm.rs.GetCode(sym)
+ code, err := vm.rs.GetCode(ctx, sym)
if err != nil {
return b, err
}