go-vise

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

commit 446935a857720117e92015a799da1621c3393a93
parent 3b53318a22a1196bdba8b92286e9af0213302cda
Author: lash <dev@holbrook.no>
Date:   Sun,  1 Sep 2024 06:48:32 +0100

Simplify resource.DbResource instantiation

Diffstat:
Mengine/default.go | 10++--------
Mengine/engine_test.go | 6++----
Mexamples/db/main.go | 13++++++-------
Mexamples/gdbm/main.go | 12++++--------
Mexamples/http/main.go | 4++--
Mexamples/intro/main.go | 4++--
Mexamples/languages/main.go | 4++--
Mexamples/pincheck/main.go | 4++--
Mexamples/preprocessor/main.go | 4++--
Mexamples/profile/main.go | 5+++--
Mexamples/quit/main.go | 7+++++--
Mexamples/reuse/main.go | 8++++++--
Mexamples/session/main.go | 4++--
Mexamples/state/main.go | 4++--
Mexamples/state_passive/main.go | 4++--
Mexamples/validate/main.go | 2+-
Mresource/db.go | 20+++++++++-----------
Mresource/db_test.go | 24++++++++++++------------
Mresource/resourcetest/util.go | 2+-
19 files changed, 67 insertions(+), 74 deletions(-)

diff --git a/engine/default.go b/engine/default.go @@ -18,10 +18,7 @@ func NewDefaultEngine(dir string, persistDb db.Db, session *string) (EngineIsh, ctx := context.Background() store := db.NewFsDb() store.Connect(ctx, dir) - rs, err := resource.NewDbResource(store) - if err != nil { - return nil, err - } + rs := resource.NewDbResource(store) ca := cache.NewCache() cfg := Config{ Root: "root", @@ -59,10 +56,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, db.DATATYPE_BIN, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU) - if err != nil { - return nil, err - } + rs := resource.NewDbResource(store) cfg := Config{ OutputSize: size, Root: "root", diff --git a/engine/engine_test.go b/engine/engine_test.go @@ -33,10 +33,8 @@ func newTestWrapper(path string, st *state.State) testWrapper { ctx := context.Background() store := db.NewFsDb() store.Connect(ctx, path) - rs, err := resource.NewDbResource(store, db.DATATYPE_BIN, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU, db.DATATYPE_STATICLOAD) - if err != nil { - panic(err) - } + rs := resource.NewDbResource(store) + rs.With(db.DATATYPE_STATICLOAD) wr := testWrapper { rs, st, diff --git a/examples/db/main.go b/examples/db/main.go @@ -102,11 +102,13 @@ func main() { fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) dataDir := path.Join(scriptDir, ".store") - store.Connect(ctx, dataDir) + err := store.Connect(ctx, dataDir) + if err != nil { + panic(err) + } store.SetSession("xyzzy") - store.SetLock(db.DATATYPE_TEMPLATE | db.DATATYPE_MENU | db.DATATYPE_BIN, false) - err := genCode(ctx, store) + err = genCode(ctx, store) if err != nil { panic(err) } @@ -122,10 +124,7 @@ func main() { } store.SetLock(db.DATATYPE_TEMPLATE | db.DATATYPE_MENU | db.DATATYPE_BIN, true) - tg, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU, db.DATATYPE_BIN) - if err != nil { - panic(err) - } + tg := resource.NewDbResource(store) rs := resource.NewMenuResource() rs.WithTemplateGetter(tg.GetTemplate) rs.WithMenuGetter(tg.GetMenu) diff --git a/examples/gdbm/main.go b/examples/gdbm/main.go @@ -40,20 +40,16 @@ func main() { panic(err) } - tg, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN) - if err != nil { - panic(err) - } + tg := resource.NewDbResource(store) + tg.Without(db.DATATYPE_MENU) rs := resource.NewMenuResource() rs = rs.WithTemplateGetter(tg.GetTemplate) rs = rs.WithCodeGetter(tg.GetCode) fsStore := db.NewFsDb() fsStore.Connect(ctx, scriptDir) - rsf, err := resource.NewDbResource(fsStore, db.DATATYPE_MENU) - if err != nil { - panic(err) - } + rsf := resource.NewDbResource(fsStore) + rsf.WithOnly(db.DATATYPE_MENU) rsf.AddLocalFunc("do", do) rs.WithMenuGetter(rsf.GetMenu) rs.WithEntryFuncGetter(rsf.FuncFor) diff --git a/examples/http/main.go b/examples/http/main.go @@ -78,11 +78,11 @@ type DefaultSessionHandler struct { 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) + err := store.Connect(ctx, resourceBase) if err != nil { panic(err) } + rs := resource.NewDbResource(store) rh := NewLocalHandler() rs.AddLocalFunc("echo", rh.AddSession) return &DefaultSessionHandler{ diff --git a/examples/intro/main.go b/examples/intro/main.go @@ -35,11 +35,11 @@ type introResource struct { 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) + err := store.Connect(ctx, scriptDir) if err != nil { panic(err) } + rs := resource.NewDbResource(store) return introResource{rs, 0, []string{}} } diff --git a/examples/languages/main.go b/examples/languages/main.go @@ -92,11 +92,11 @@ func main() { state.FlagDebugger.Register(USERFLAG_FLIP, "FLIP") ctx := context.Background() rsStore := db.NewFsDb() - rsStore.Connect(ctx, scriptDir) - rs, err := resource.NewDbResource(rsStore, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) + err := rsStore.Connect(ctx, scriptDir) if err != nil { panic(err) } + rs := resource.NewDbResource(rsStore) ca := cache.NewCache() cfg := engine.Config{ diff --git a/examples/pincheck/main.go b/examples/pincheck/main.go @@ -78,11 +78,11 @@ func main() { state.FlagDebugger.Register(USERFLAG_VALIDPIN, "VALIDPIN") state.FlagDebugger.Register(USERFLAG_QUERYPIN, "QUERYPIN") store := db.NewFsDb() - store.Connect(ctx, scriptDir) - rsf, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU, db.DATATYPE_BIN) + err := store.Connect(ctx, scriptDir) if err != nil { panic(err) } + rsf := resource.NewDbResource(store) rs := newPinResource(rsf, &st) rsf.AddLocalFunc("pincheck", rs.pinCheck) rsf.AddLocalFunc("pinclear", rs.pinClear) diff --git a/examples/preprocessor/main.go b/examples/preprocessor/main.go @@ -74,11 +74,11 @@ func main() { st := state.NewState(5) st.UseDebug() store := db.NewFsDb() - store.Connect(ctx, scriptDir) - rsf, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) + err := store.Connect(ctx, scriptDir) if err != nil { panic(err) } + rsf := resource.NewDbResource(store) rs, err := newCountResource(scriptDir) if err != nil { fmt.Fprintf(os.Stderr, "aux handler fail: %v\n", err) diff --git a/examples/profile/main.go b/examples/profile/main.go @@ -101,11 +101,12 @@ func main() { ctx := context.Background() st := state.NewState(3) store := db.NewFsDb() - store.Connect(ctx, scriptDir) - rsf, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU, db.DATATYPE_STATICLOAD) + err := store.Connect(ctx, scriptDir) if err != nil { panic(err) } + rsf := resource.NewDbResource(store) + rsf.With(db.DATATYPE_STATICLOAD) rs, ok := newProfileResource(&st, rsf).(*profileResource) if !ok { os.Exit(1) diff --git a/examples/quit/main.go b/examples/quit/main.go @@ -34,8 +34,11 @@ func main() { ctx := context.Background() store := db.NewFsDb() - store.Connect(ctx, scriptDir) - rs, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) + err := store.Connect(ctx, scriptDir) + if err != nil { + panic(err) + } + rs := resource.NewDbResource(store) cfg := engine.Config{ Root: "root", } diff --git a/examples/reuse/main.go b/examples/reuse/main.go @@ -39,8 +39,12 @@ func main() { ctx := context.Background() st := state.NewState(0) store := db.NewFsDb() - store.Connect(ctx, scriptDir) - rs, err := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) + err := store.Connect(ctx, scriptDir) + if err != nil { + panic(err) + } + rs := resource.NewDbResource(store) + rs.AddLocalFunc("do_foo", same) rs.AddLocalFunc("do_bar", same) ca := cache.NewCache() diff --git a/examples/session/main.go b/examples/session/main.go @@ -67,11 +67,11 @@ func main() { st := state.NewState(0) st.UseDebug() store := db.NewFsDb() - store.Connect(ctx, scriptDir) - rs, err := resource.NewDbResource(store) + err := store.Connect(ctx, scriptDir) if err != nil { panic(err) } + rs := resource.NewDbResource(store) rs.AddLocalFunc("do_save", save) ca := cache.NewCache() cfg := engine.Config{ diff --git a/examples/state/main.go b/examples/state/main.go @@ -76,11 +76,11 @@ func main() { st := state.NewState(3) st.UseDebug() store := db.NewFsDb() - store.Connect(ctx, scriptDir) - rs, err := resource.NewDbResource(store) + err := store.Connect(ctx, scriptDir) if err != nil { panic(err) } + rs := resource.NewDbResource(store) ca := cache.NewCache() cfg := engine.Config{ Root: "root", diff --git a/examples/state_passive/main.go b/examples/state_passive/main.go @@ -91,11 +91,11 @@ func main() { ctx := context.Background() st := state.NewState(4) rsStore := db.NewFsDb() - rsStore.Connect(ctx, dir) - rs, err := resource.NewDbResource(rsStore) + err := rsStore.Connect(ctx, dir) if err != nil { panic(err) } + rs := resource.NewDbResource(rsStore) ca := cache.NewCache() cfg := engine.Config{ Root: "root", diff --git a/examples/validate/main.go b/examples/validate/main.go @@ -65,7 +65,7 @@ func main() { if err != nil { panic(err) } - rsf, err := resource.NewDbResource(store) + rsf := resource.NewDbResource(store) rs := verifyResource{rsf, &st} rs.AddLocalFunc("verifyinput", rs.verify) rs.AddLocalFunc("again", rs.again) diff --git a/resource/db.go b/resource/db.go @@ -3,7 +3,6 @@ package resource import ( "context" "errors" - "fmt" "git.defalsify.org/vise.git/db" ) @@ -20,27 +19,26 @@ type DbResource struct { } // NewDbFuncGetter instantiates a new DbResource -func NewDbResource(store db.Db, typs... uint8) (*DbResource, error) { - var v uint8 - g := &DbResource{ +func NewDbResource(store db.Db) *DbResource { + return &DbResource{ MenuResource: NewMenuResource(), db: store, typs: db.DATATYPE_TEMPLATE | db.DATATYPE_MENU | db.DATATYPE_BIN, } - for _, v = range(typs) { - if v > resource_max_datatype { - return nil, fmt.Errorf("datatype %d is not a resource", v) - } - g.typs |= v - } - return g, nil } func(g *DbResource) Without(typ uint8) *DbResource { + g.typs &= ^typ return g } func(g *DbResource) With(typ uint8) *DbResource { + g.typs |= typ + return g +} + +func(g *DbResource) WithOnly(typ uint8) *DbResource { + g.typs = typ return g } diff --git a/resource/db_test.go b/resource/db_test.go @@ -13,10 +13,10 @@ func TestDb(t *testing.T) { ctx := context.Background() store := db.NewMemDb() store.Connect(ctx, "") - tg, err := NewDbResource(store, db.DATATYPE_TEMPLATE) - if err != nil { - t.Fatal(err) - } + tg := NewDbResource(store) + tg.Without(db.DATATYPE_BIN) + tg.Without(db.DATATYPE_MENU) + tg.Without(db.DATATYPE_TEMPLATE) // check that it fulfills interface rsifc = tg _ = rsifc @@ -39,6 +39,7 @@ func TestDb(t *testing.T) { t.Fatal(err) } store.SetLock(db.DATATYPE_TEMPLATE, true) + tg.With(db.DATATYPE_TEMPLATE) s, err = rs.GetTemplate(ctx, "foo") if err != nil { t.Fatal(err) @@ -58,14 +59,16 @@ func TestDb(t *testing.T) { rs.WithCodeGetter(tg.GetCode) b, err := rs.GetCode(ctx, "xyzzy") - if err != nil { - t.Fatal(err) + if err == nil { + t.Fatalf("expected error") } - - tg, err = NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN) + tg.With(db.DATATYPE_BIN) + b, err = rs.GetCode(ctx, "xyzzy") if err != nil { t.Fatal(err) } + + tg = NewDbResource(store) rs.WithTemplateGetter(tg.GetTemplate) rs.WithCodeGetter(tg.GetCode) @@ -77,10 +80,7 @@ func TestDb(t *testing.T) { t.Fatalf("expected 'deadbeef', got %x", b) } - tg, err = NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_BIN, db.DATATYPE_MENU) - if err != nil { - t.Fatal(err) - } + tg = NewDbResource(store) store.SetPrefix(db.DATATYPE_MENU) store.SetLock(db.DATATYPE_MENU, false) err = store.Put(ctx, []byte("inky"), []byte("pinky")) diff --git a/resource/resourcetest/util.go b/resource/resourcetest/util.go @@ -43,7 +43,7 @@ func newTestResource(path string) *TestResource { } store.Connect(ctx, path) - rsd, _ := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU, db.DATATYPE_BIN) + rsd := resource.NewDbResource(store) rs := &TestResource{ DbResource: rsd, ctx: ctx,