go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 3b53318a22a1196bdba8b92286e9af0213302cda
parent 7de399c30f03f832466bd8d438beabba150d2076
Author: lash <dev@holbrook.no>
Date:   Sun,  1 Sep 2024 06:22:06 +0100

Complete resource.FsResource elimination

Diffstat:
Mengine/default.go | 2+-
Mexamples/http/main.go | 12+++++++++---
Mexamples/intro/main.go | 20+++++++++++++-------
Mexamples/languages/main.go | 11++++++++---
Mexamples/pincheck/main.go | 11++++++++---
Mexamples/preprocessor/main.go | 10++++++++--
Mexamples/profile/main.go | 15++++++++++-----
Mexamples/quit/main.go | 9++++++---
Mexamples/reuse/main.go | 8+++++---
Mexamples/session/main.go | 11++++++++---
Mexamples/state/main.go | 11++++++++---
Mexamples/state_passive/main.go | 9+++++++--
Mexamples/validate/main.go | 15++++++++++-----
Mresource/db.go | 17++++++++++++++++-
Mresource/db_test.go | 5++---
Mresource/resourcetest/util_test.go | 94++++++++++++++++++++++++++++++++++++++++----------------------------------------
16 files changed, 166 insertions(+), 94 deletions(-)

diff --git a/engine/default.go b/engine/default.go @@ -59,7 +59,7 @@ func NewSizedEngine(dir string, size uint32, persistDb db.Db, session *string) ( ctx := context.Background() store := db.NewFsDb() store.Connect(ctx, dir) - rs, err := resource.NewDbResource(store) + rs, err := resource.NewDbResource(store, db.DATATYPE_BIN, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU) if err != nil { return nil, err } diff --git a/examples/http/main.go b/examples/http/main.go @@ -76,8 +76,13 @@ type DefaultSessionHandler struct { peBase string } -func NewDefaultSessionHandler(persistBase string, resourceBase string, rp RequestParser, outputSize uint32, cacheSize uint32, flagCount uint32) *DefaultSessionHandler { - rs := resource.NewFsResource(resourceBase) +func NewDefaultSessionHandler(ctx context.Context, persistBase string, resourceBase string, rp RequestParser, outputSize uint32, cacheSize uint32, flagCount uint32) *DefaultSessionHandler { + store := db.NewFsDb() + store.Connect(ctx, resourceBase) + rs, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) + if err != nil { + panic(err) + } rh := NewLocalHandler() rs.AddLocalFunc("echo", rh.AddSession) return &DefaultSessionHandler{ @@ -195,8 +200,9 @@ func main() { flag.Parse() fmt.Fprintf(os.Stderr, "starting server:\n\tpersistence dir: %s\n\tresource dir: %s\n", rsDir, peDir) + ctx := context.Background() rp := &DefaultRequestParser{} - h := NewDefaultSessionHandler(peDir, rsDir, rp, uint32(outSize), uint32(cacheSize), uint32(flagCount)) + h := NewDefaultSessionHandler(ctx, peDir, rsDir, rp, uint32(outSize), uint32(cacheSize), uint32(flagCount)) s := &http.Server{ Addr: fmt.Sprintf("%s:%s", host, port), Handler: h, diff --git a/examples/intro/main.go b/examples/intro/main.go @@ -15,6 +15,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" ) const ( @@ -27,14 +28,19 @@ var ( ) type introResource struct { - *resource.FsResource + *resource.DbResource c int64 v []string } -func newintroResource() introResource { - fs := resource.NewFsResource(scriptDir) - return introResource{fs, 0, []string{}} +func newintroResource(ctx context.Context) introResource { + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rs, err := resource.NewDbResource(store, db.DATATYPE_BIN, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU) + if err != nil { + panic(err) + } + return introResource{rs, 0, []string{}} } // increment counter. @@ -74,9 +80,10 @@ func main() { flag.StringVar(&sessionId, "session-id", "default", "session id") flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) - + + ctx := context.Background() st := state.NewState(3) - rs := newintroResource() + rs := newintroResource(ctx) rs.AddLocalFunc("count", rs.count) rs.AddLocalFunc("something", rs.something) ca := cache.NewCache() @@ -85,7 +92,6 @@ func main() { SessionId: sessionId, OutputSize: uint32(size), } - ctx := context.Background() en := engine.NewEngine(ctx, cfg, &st, rs, ca) var err error _, err = en.Init(ctx) diff --git a/examples/languages/main.go b/examples/languages/main.go @@ -90,18 +90,23 @@ func empty(ctx context.Context, sym string, input []byte) (resource.Result, erro func main() { st := state.NewState(1) state.FlagDebugger.Register(USERFLAG_FLIP, "FLIP") - rs := resource.NewFsResource(scriptDir) + ctx := context.Background() + rsStore := db.NewFsDb() + rsStore.Connect(ctx, scriptDir) + rs, err := resource.NewDbResource(rsStore, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) + if err != nil { + panic(err) + } ca := cache.NewCache() cfg := engine.Config{ Root: "root", SessionId: "default", } - ctx := context.Background() dp := path.Join(scriptDir, ".state") store := db.NewFsDb() - err := store.Connect(ctx, dp) + err = store.Connect(ctx, dp) if err != nil { logg.ErrorCtxf(ctx, "db connect fail", "err", err) os.Exit(1) diff --git a/examples/pincheck/main.go b/examples/pincheck/main.go @@ -16,6 +16,7 @@ import ( "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/db" ) const ( @@ -71,11 +72,17 @@ func main() { flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + ctx := context.Background() st := state.NewState(3) st.UseDebug() state.FlagDebugger.Register(USERFLAG_VALIDPIN, "VALIDPIN") state.FlagDebugger.Register(USERFLAG_QUERYPIN, "QUERYPIN") - rsf := resource.NewFsResource(scriptDir) + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rsf, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU, db.DATATYPE_BIN) + if err != nil { + panic(err) + } rs := newPinResource(rsf, &st) rsf.AddLocalFunc("pincheck", rs.pinCheck) rsf.AddLocalFunc("pinclear", rs.pinClear) @@ -83,9 +90,7 @@ func main() { cfg := engine.Config{ Root: "root", } - ctx := context.Background() en := engine.NewEngine(ctx, cfg, &st, rs, ca) - var err error _, err = en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) diff --git a/examples/preprocessor/main.go b/examples/preprocessor/main.go @@ -15,6 +15,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" ) @@ -69,9 +70,15 @@ func main() { root := "root" fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + ctx := context.Background() st := state.NewState(5) st.UseDebug() - rsf := resource.NewFsResource(scriptDir) + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rsf, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) + if err != nil { + panic(err) + } rs, err := newCountResource(scriptDir) if err != nil { fmt.Fprintf(os.Stderr, "aux handler fail: %v\n", err) @@ -85,7 +92,6 @@ func main() { cfg := engine.Config{ Root: "root", } - ctx := context.Background() en := engine.NewEngine(ctx, cfg, &st, rsf, ca) _, err = en.Init(ctx) diff --git a/examples/profile/main.go b/examples/profile/main.go @@ -16,6 +16,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" ) const ( @@ -31,12 +32,12 @@ var ( ) type profileResource struct { - *resource.FsResource + *resource.DbResource st *state.State haveEntered bool } -func newProfileResource(st *state.State, rs *resource.FsResource) resource.Resource { +func newProfileResource(st *state.State, rs *resource.DbResource) resource.Resource { return &profileResource{ rs, st, @@ -97,8 +98,14 @@ func main() { flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) + ctx := context.Background() st := state.NewState(3) - rsf := resource.NewFsResource(scriptDir) + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rsf, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU, db.DATATYPE_STATICLOAD) + if err != nil { + panic(err) + } rs, ok := newProfileResource(&st, rsf).(*profileResource) if !ok { os.Exit(1) @@ -111,9 +118,7 @@ func main() { SessionId: sessionId, OutputSize: uint32(size), } - ctx := context.Background() en := engine.NewEngine(ctx, cfg, &st, rs, ca) - var err error _, err = en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) diff --git a/examples/quit/main.go b/examples/quit/main.go @@ -13,6 +13,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 ( @@ -30,16 +31,18 @@ func main() { st := state.NewState(0) st.UseDebug() ca := cache.NewCache() - rs := resource.NewFsResource(scriptDir) + + ctx := context.Background() + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rs, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) cfg := engine.Config{ Root: "root", } - ctx := context.Background() en := engine.NewEngine(ctx, cfg, &st, rs, ca) rs.AddLocalFunc("quitcontent", quit) - var err error _, err = en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) diff --git a/examples/reuse/main.go b/examples/reuse/main.go @@ -12,6 +12,7 @@ import ( "git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/db" "git.defalsify.org/vise.git/state" ) @@ -35,17 +36,18 @@ func main() { root := "root" fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + ctx := context.Background() st := state.NewState(0) - rs := resource.NewFsResource(scriptDir) + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rs, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) rs.AddLocalFunc("do_foo", same) rs.AddLocalFunc("do_bar", same) ca := cache.NewCache() cfg := engine.Config{ Root: "root", } - ctx := context.Background() en := engine.NewEngine(ctx, cfg, &st, rs, ca) - var err error _, err = en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) diff --git a/examples/session/main.go b/examples/session/main.go @@ -16,6 +16,7 @@ import ( "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/db" ) var ( @@ -62,9 +63,15 @@ func main() { flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + ctx := context.Background() st := state.NewState(0) st.UseDebug() - rs := resource.NewFsResource(scriptDir) + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rs, err := resource.NewDbResource(store) + if err != nil { + panic(err) + } rs.AddLocalFunc("do_save", save) ca := cache.NewCache() cfg := engine.Config{ @@ -72,10 +79,8 @@ func main() { SessionId: sessionId, OutputSize: uint32(size), } - ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) en := engine.NewEngine(ctx, cfg, &st, rs, ca) - var err error _, err = en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) diff --git a/examples/state/main.go b/examples/state/main.go @@ -14,6 +14,7 @@ import ( "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/db" ) const ( @@ -71,14 +72,19 @@ func main() { root := "root" fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + ctx := context.Background() st := state.NewState(3) st.UseDebug() - rs := resource.NewFsResource(scriptDir) + store := db.NewFsDb() + store.Connect(ctx, scriptDir) + rs, err := resource.NewDbResource(store) + if err != nil { + panic(err) + } ca := cache.NewCache() cfg := engine.Config{ Root: "root", } - ctx := context.Background() en := engine.NewEngine(ctx, cfg, &st, rs, ca) en.SetDebugger(engine.NewSimpleDebug(nil)) @@ -91,7 +97,6 @@ func main() { state.FlagDebugger.Register(USER_FOO, "FOO") state.FlagDebugger.Register(USER_BAR, "BAR") state.FlagDebugger.Register(USER_BAZ, "BAZ") - var err error _, err = en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) diff --git a/examples/state_passive/main.go b/examples/state_passive/main.go @@ -90,7 +90,12 @@ func main() { ctx := context.Background() st := state.NewState(4) - rs := resource.NewFsResource(dir) + rsStore := db.NewFsDb() + rsStore.Connect(ctx, dir) + rs, err := resource.NewDbResource(rsStore) + if err != nil { + panic(err) + } ca := cache.NewCache() cfg := engine.Config{ Root: "root", @@ -99,7 +104,7 @@ func main() { dp := path.Join(dir, ".state") store := db.NewFsDb() - err := store.Connect(ctx, dp) + err = store.Connect(ctx, dp) if err != nil { fmt.Fprintf(os.Stderr, "db connect fail: %s", err) os.Exit(1) diff --git a/examples/validate/main.go b/examples/validate/main.go @@ -14,6 +14,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,7 +28,7 @@ const ( ) type verifyResource struct { - *resource.FsResource + *resource.DbResource st *state.State } @@ -56,8 +57,15 @@ func main() { flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + ctx := context.Background() + ctx = context.WithValue(ctx, "SessionId", sessionId) st := state.NewState(1) - rsf := resource.NewFsResource(scriptDir) + store := db.NewFsDb() + err := store.Connect(ctx, scriptDir) + if err != nil { + panic(err) + } + rsf, err := resource.NewDbResource(store) rs := verifyResource{rsf, &st} rs.AddLocalFunc("verifyinput", rs.verify) rs.AddLocalFunc("again", rs.again) @@ -67,10 +75,7 @@ func main() { SessionId: sessionId, OutputSize: uint32(size), } - ctx := context.Background() - ctx = context.WithValue(ctx, "SessionId", sessionId) en := engine.NewEngine(ctx, cfg, &st, rs, ca) - var err error _, err = en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) diff --git a/resource/db.go b/resource/db.go @@ -25,6 +25,7 @@ func NewDbResource(store db.Db, typs... uint8) (*DbResource, error) { g := &DbResource{ MenuResource: NewMenuResource(), db: store, + typs: db.DATATYPE_TEMPLATE | db.DATATYPE_MENU | db.DATATYPE_BIN, } for _, v = range(typs) { if v > resource_max_datatype { @@ -35,6 +36,14 @@ func NewDbResource(store db.Db, typs... uint8) (*DbResource, error) { return g, nil } +func(g *DbResource) Without(typ uint8) *DbResource { + return g +} + +func(g *DbResource) With(typ uint8) *DbResource { + return g +} + func(g *DbResource) fn(ctx context.Context, sym string) ([]byte, error) { return g.db.Get(ctx, []byte(sym)) } @@ -93,7 +102,13 @@ func(g *DbResource) FuncFor(ctx context.Context, sym string) (EntryFunc, error) g.db.SetPrefix(db.DATATYPE_STATICLOAD) b, err := g.fn(ctx, sym) if err != nil { - return nil, err + if !db.IsNotFound(err) { + return nil, err + } + b, err = g.fn(ctx, sym + ".txt") + if err != nil { + return nil, err + } } return func(ctx context.Context, nodeSym string, input []byte) (Result, error) { return Result{ diff --git a/resource/db_test.go b/resource/db_test.go @@ -28,7 +28,6 @@ func TestDb(t *testing.T) { t.Fatal("expected error") } - store.SetPrefix(db.DATATYPE_TEMPLATE) err = store.Put(ctx, []byte("foo"), []byte("bar")) if err == nil { @@ -59,8 +58,8 @@ func TestDb(t *testing.T) { rs.WithCodeGetter(tg.GetCode) b, err := rs.GetCode(ctx, "xyzzy") - if err == nil { - t.Fatal("expected error") + if err != nil { + t.Fatal(err) } tg, err = NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN) diff --git a/resource/resourcetest/util_test.go b/resource/resourcetest/util_test.go @@ -1,55 +1,55 @@ package resourcetest -import ( - "context" - "fmt" - - "git.defalsify.org/vise.git/db" - "git.defalsify.org/vise.git/resource/resourcetest" -) +//import ( +// "context" +// "fmt" +// +// "git.defalsify.org/vise.git/db" +// "git.defalsify.org/vise.git/resource" +//) //type TestSizeResource struct { // *DbResource //} -func NewTestSizeResource() Resource { - rs, err := NewResourceTest() - if err != nil { - panic(err) - } - - rs.AddTemplate(ctx, "small", "one {{.foo}} two {{.bar}} three {{.baz}}") - rs.AddTemplate(ctx, "toobug", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus.") - rs.AddTemplate(ctx, "pages", "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}") - rs.AddLocalFunc("foo", get) - rs.AddLocalFunc("bar", get) - rs.AddLocalFunc("baz", get) - rs.AddLocalFunc("xyzzy", getXyzzy) - return rs -} - -func get(ctx context.Context, sym string, input []byte) (Result, error) { - switch sym { - case "foo": - return Result{ - Content: "inky", - }, nil - case "bar": - return Result{ - Content: "pinky", - }, nil - case "baz": - return Result{ - Content: "blinky", - }, nil - } - return Result{}, fmt.Errorf("unknown sym: %s", sym) -} - -func getXyzzy(ctx context.Context, sym string, input []byte) (Result, error) { - r := "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve" - return Result{ - Content: r, - }, nil -} +//func NewTestSizeResource() resource.Resource { +// rs, err := NewResourceTest() +// if err != nil { +// panic(err) +// } +// +// rs.AddTemplate(ctx, "small", "one {{.foo}} two {{.bar}} three {{.baz}}") +// rs.AddTemplate(ctx, "toobug", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus.") +// rs.AddTemplate(ctx, "pages", "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}") +// rs.AddLocalFunc("foo", get) +// rs.AddLocalFunc("bar", get) +// rs.AddLocalFunc("baz", get) +// rs.AddLocalFunc("xyzzy", getXyzzy) +// return rs +//} +// +//func get(ctx context.Context, sym string, input []byte) (Result, error) { +// switch sym { +// case "foo": +// return Result{ +// Content: "inky", +// }, nil +// case "bar": +// return Result{ +// Content: "pinky", +// }, nil +// case "baz": +// return Result{ +// Content: "blinky", +// }, nil +// } +// return Result{}, fmt.Errorf("unknown sym: %s", sym) +//} +// +//func getXyzzy(ctx context.Context, sym string, input []byte) (Result, error) { +// r := "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve" +// return Result{ +// Content: r, +// }, nil +//}