go-vise

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

commit 019a78d7c7a7140f7c0d7d50d08a8936c088b3f5
parent f5fe2dcd8268af64a4b48455547351a2f6d42e5b
Author: lash <dev@holbrook.no>
Date:   Sun,  1 Sep 2024 07:30:56 +0100

Remove gdbm resource, more inline doc improvement

Diffstat:
Mdb/fs.go | 1+
Mdb/mem.go | 1+
Mresource/db.go | 26++++++++++++++++++++++++--
Dresource/gdbm.go | 93-------------------------------------------------------------------------------
Mresource/resource.go | 14++++++++------
5 files changed, 34 insertions(+), 101 deletions(-)

diff --git a/db/fs.go b/db/fs.go @@ -9,6 +9,7 @@ import ( "path" ) +// holds string (filepath) versions of lookupKey type fsLookupKey struct { Default string Translation string diff --git a/db/mem.go b/db/mem.go @@ -6,6 +6,7 @@ import ( "errors" ) +// holds string (hex) versions of lookupKey type memLookupKey struct { Default string Translation string diff --git a/resource/db.go b/resource/db.go @@ -12,13 +12,17 @@ const ( ) // DbResource is a MenuResource that uses the given db.Db implementation as data retriever. +// +// The DbResource can resolve any db.DATATYPE_* if instructed to do so. type DbResource struct { *MenuResource typs uint8 db db.Db } -// NewDbFuncGetter instantiates a new DbResource +// NewDbResource instantiates a new DbResource +// +// By default it will handle db.DATATYPE_TEPMLATE, db.DATATYPE_MENU and db.DATATYPE_BIN. func NewDbResource(store db.Db) *DbResource { return &DbResource{ MenuResource: NewMenuResource(), @@ -27,25 +31,30 @@ func NewDbResource(store db.Db) *DbResource { } } +// Without is a chainable function that disables handling of the given data type. func(g *DbResource) Without(typ uint8) *DbResource { g.typs &= ^typ return g } +// Without is a chainable function that enables handling of the given data type. func(g *DbResource) With(typ uint8) *DbResource { g.typs |= typ return g } +// WithOnly is a chainable convenience function that disables handling of all except the given data type. func(g *DbResource) WithOnly(typ uint8) *DbResource { g.typs = typ return g } +// retrieve from underlying db. func(g *DbResource) fn(ctx context.Context, sym string) ([]byte, error) { return g.db.Get(ctx, []byte(sym)) } +// retrieve from underlying db using a string key. func(g *DbResource) sfn(ctx context.Context, sym string) (string, error) { b, err := g.fn(ctx, sym) if err != nil { @@ -55,6 +64,8 @@ func(g *DbResource) sfn(ctx context.Context, sym string) (string, error) { } // GetTemplate implements the Resource interface. +// +// Will fail if support for db.DATATYPE_TEMPLATE has been disabled. func(g *DbResource) GetTemplate(ctx context.Context, sym string) (string, error) { if g.typs & db.DATATYPE_TEMPLATE == 0{ return "", errors.New("not a template getter") @@ -64,6 +75,8 @@ func(g *DbResource) GetTemplate(ctx context.Context, sym string) (string, error) } // GetTemplate implements the Resource interface. +// +// Will fail if support for db.DATATYPE_MENU has been disabled. func(g *DbResource) GetMenu(ctx context.Context, sym string) (string, error) { if g.typs & db.DATATYPE_MENU == 0{ return "", errors.New("not a menu getter") @@ -80,7 +93,9 @@ func(g *DbResource) GetMenu(ctx context.Context, sym string) (string, error) { return v, nil } -// GetTemplate implements the Resource interface. +// GetCode implements the Resource interface. +// +// Will fail if support for db.DATATYPE_BIN has been disabled. func(g *DbResource) GetCode(ctx context.Context, sym string) ([]byte, error) { if g.typs & db.DATATYPE_BIN == 0{ return nil, errors.New("not a code getter") @@ -89,6 +104,13 @@ func(g *DbResource) GetCode(ctx context.Context, sym string) ([]byte, error) { return g.fn(ctx, sym) } +// FuncFor implements the Resource interface. +// +// The method will first attempt to resolve using the function registered +// with the MenuResource parent class. +// +// If no match is found, and if support for db.DATATYPE_STATICLOAD has been enabled, +// an additional lookup will be performed using the underlying db. func(g *DbResource) FuncFor(ctx context.Context, sym string) (EntryFunc, error) { fn, err := g.MenuResource.FuncFor(ctx, sym) if err == nil { diff --git a/resource/gdbm.go b/resource/gdbm.go @@ -1,93 +0,0 @@ -package resource - -import ( - "context" - "fmt" - - gdbm "github.com/graygnuorg/go-gdbm" - "git.defalsify.org/vise.git/lang" - "git.defalsify.org/vise.git/db" -) - -type gdbmResource struct { - db *gdbm.Database - fns map[string]EntryFunc -} - -func NewGdbmResource(fp string) *gdbmResource { - gdb, err := gdbm.Open(fp, gdbm.ModeReader) - if err != nil { - panic(err) - } - return NewGdbmResourceFromDatabase(gdb) -} - -func NewGdbmResourceFromDatabase(gdb *gdbm.Database) *gdbmResource { - return &gdbmResource{ - db: gdb, - } -} - -func(dbr *gdbmResource) GetTemplate(ctx context.Context, sym string) (string, error) { - var ln lang.Language - v := ctx.Value("Language") - if v != nil { - ln = v.(lang.Language) - } - k := db.ToDbKey(db.DATATYPE_TEMPLATE, []byte(sym), &ln) - r, err := dbr.db.Fetch(k) - if err != nil { - if err.(*gdbm.GdbmError).Is(gdbm.ErrItemNotFound) { - k = db.ToDbKey(db.DATATYPE_TEMPLATE, []byte(sym), nil) - r, err = dbr.db.Fetch(k) - if err != nil { - return "", err - } - } - } - return string(r), nil -} - -func(dbr *gdbmResource) GetCode(sym string) ([]byte, error) { - k := db.ToDbKey(db.DATATYPE_BIN, []byte(sym), nil) - return dbr.db.Fetch(k) -} - -func(dbr *gdbmResource) GetMenu(ctx context.Context, sym string) (string, error) { - msym := sym + "_menu" - var ln lang.Language - v := ctx.Value("Language") - if v != nil { - ln = v.(lang.Language) - } - k := db.ToDbKey(db.DATATYPE_TEMPLATE, []byte(msym), &ln) - r, err := dbr.db.Fetch(k) - if err != nil { - if err.(*gdbm.GdbmError).Is(gdbm.ErrItemNotFound) { - return sym, nil - } - return "", err - } - return string(r), nil - -} - -func(dbr gdbmResource) FuncFor(sym string) (EntryFunc, error) { - fn, ok := dbr.fns[sym] - if !ok { - return nil, fmt.Errorf("function %s not found", sym) - } - return fn, nil -} - -func(dbr *gdbmResource) AddLocalFunc(sym string, fn EntryFunc) { - if dbr.fns == nil { - dbr.fns = make(map[string]EntryFunc) - } - dbr.fns[sym] = fn -} - -// String implements the String interface. -func(dbr *gdbmResource) String() string { - return fmt.Sprintf("gdbm: %v", dbr.db) -} diff --git a/resource/resource.go b/resource/resource.go @@ -8,10 +8,14 @@ import ( // Result contains the results of an external code operation. type Result struct { - Content string // content value for symbol after execution. - Status int // application defined status code which can complement error returns - FlagSet []uint32 // request caller to set error flags at given indices. - FlagReset []uint32 // request caller to reset error flags at given indices. + // content value for symbol after execution. + Content string + // application defined status code which can complement error returns + Status int + // request caller to set error flags at given indices. + FlagSet []uint32 + // request caller to reset error flags at given indices. + FlagReset []uint32 } // EntryFunc is a function signature for a function that resolves the symbol of a LOAD instruction. @@ -44,8 +48,6 @@ type Resource interface { } // MenuResource contains the base definition for building Resource implementations. -// -// TODO: Rename to BaseResource type MenuResource struct { sinkValues []string codeFunc CodeFunc