commit b4386fa0eaddb33c431c384f58edeed9a636b747
parent 88277264aecd1b7d30351d8c076e73d92445a23f
Author: lash <dev@holbrook.no>
Date: Sun, 1 Sep 2024 03:09:17 +0100
WIP remove legacy resource implementations
Diffstat:
32 files changed, 485 insertions(+), 835 deletions(-)
diff --git a/db/db.go b/db/db.go
@@ -16,10 +16,12 @@ const (
DATATYPE_MENU = 2
// Template symbol
DATATYPE_TEMPLATE = 4
+ // Static LOAD symbols
+ DATATYPE_STATICLOAD = 8
// State and cache from persister
- DATATYPE_STATE = 8
+ DATATYPE_STATE = 16
// Application data
- DATATYPE_USERSTART = 16
+ DATATYPE_USERSTART = 32
)
const (
@@ -52,8 +54,8 @@ type Db interface {
// If language is not nil, and the context does not support language, the language value will silently will be ignored.
func ToDbKey(typ uint8, b []byte, l *lang.Language) []byte {
k := []byte{typ}
- if l != nil && l.Code != "" {
- k = append(k, []byte("_" + l.Code)...)
+ if l != nil && l.Code != "" && typ & (DATATYPE_MENU | DATATYPE_TEMPLATE) > 0 {
+ b = append(b, []byte("_" + l.Code)...)
//s += "_" + l.Code
}
return append(k, b...)
@@ -68,7 +70,7 @@ type baseDb struct {
// ensures default locking of read-only entries
func(db *baseDb) defaultLock() {
- db.lock = DATATYPE_BIN | DATATYPE_MENU | DATATYPE_TEMPLATE
+ db.lock = DATATYPE_BIN | DATATYPE_MENU | DATATYPE_TEMPLATE | DATATYPE_STATICLOAD
}
// SetPrefix implements Db.
@@ -95,7 +97,7 @@ func(db *baseDb) checkPut() bool {
}
// ToKey creates a DbKey within the current session context.
-func(db *baseDb) ToKey(key []byte) ([]byte, error) {
+func(db *baseDb) ToKey(ctx context.Context, key []byte) ([]byte, error) {
var b []byte
if db.pfx == DATATYPE_UNKNOWN {
return nil, errors.New("datatype prefix cannot be UNKNOWN")
@@ -105,5 +107,9 @@ func(db *baseDb) ToKey(key []byte) ([]byte, error) {
} else {
b = key
}
+ ln, ok := ctx.Value("Language").(lang.Language)
+ if ok {
+ return ToDbKey(db.pfx, b, &ln), nil
+ }
return ToDbKey(db.pfx, b, nil), nil
}
diff --git a/db/error.go b/db/error.go
@@ -2,6 +2,11 @@ package db
import (
"fmt"
+ "strings"
+)
+
+const (
+ notFoundPrefix = "key not found: "
)
// ErrNotFound is returned with a key was successfully queried, but did not match a stored key.
@@ -16,5 +21,14 @@ func NewErrNotFound(k []byte) error {
// Error implements Error.
func(e ErrNotFound) Error() string {
- return fmt.Sprintf("key not found: %x", e.k)
+ return fmt.Sprintf("%s%x", notFoundPrefix, e.k)
+}
+
+func (e ErrNotFound) Is(err error) bool {
+ return strings.Contains(err.Error(), notFoundPrefix)
+}
+
+func IsNotFound(err error) bool {
+ target := ErrNotFound{}
+ return target.Is(err)
}
diff --git a/db/fs.go b/db/fs.go
@@ -36,14 +36,14 @@ func(fdb *fsDb) Connect(ctx context.Context, connStr string) error {
// Get implements the Db interface.
func(fdb *fsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
- fp, err := fdb.pathFor(key)
+ fp, err := fdb.pathFor(ctx, key)
if err != nil {
return nil, err
}
logg.TraceCtxf(ctx, "trying fs get", "key", key, "path", fp)
f, err := os.Open(fp)
if err != nil {
- fp, err = fdb.altPathFor(key)
+ fp, err = fdb.altPathFor(ctx, key)
if err != nil {
return nil, err
}
@@ -66,7 +66,7 @@ func(fdb *fsDb) Put(ctx context.Context, key []byte, val []byte) error {
if !fdb.checkPut() {
return errors.New("unsafe put and safety set")
}
- fp, err := fdb.pathFor(key)
+ fp, err := fdb.pathFor(ctx, key)
if err != nil {
return err
}
@@ -79,8 +79,8 @@ func(fdb *fsDb) Close() error {
}
// create a key safe for the filesystem.
-func(fdb *fsDb) pathFor(key []byte) (string, error) {
- kb, err := fdb.ToKey(key)
+func(fdb *fsDb) pathFor(ctx context.Context, key []byte) (string, error) {
+ kb, err := fdb.ToKey(ctx, key)
if err != nil {
return "", err
}
@@ -89,10 +89,14 @@ func(fdb *fsDb) pathFor(key []byte) (string, error) {
}
// create a key safe for the filesystem, matching legacy resource.FsResource name.
-func(fdb *fsDb) altPathFor(key []byte) (string, error) {
- kb, err := fdb.ToKey(key)
+func(fdb *fsDb) altPathFor(ctx context.Context, key []byte) (string, error) {
+ kb, err := fdb.ToKey(ctx, key)
if err != nil {
return "", err
}
- return path.Join(fdb.dir, string(kb[1:])), nil
+ fb := string(kb[1:])
+ if fdb.pfx == DATATYPE_BIN {
+ fb += ".bin"
+ }
+ return path.Join(fdb.dir, fb), nil
}
diff --git a/db/gdbm.go b/db/gdbm.go
@@ -50,7 +50,7 @@ func(gdb *gdbmDb) Put(ctx context.Context, key []byte, val []byte) error {
if !gdb.checkPut() {
return errors.New("unsafe put and safety set")
}
- k, err := gdb.ToKey(key)
+ k, err := gdb.ToKey(ctx, key)
if err != nil {
return err
}
@@ -59,7 +59,7 @@ func(gdb *gdbmDb) Put(ctx context.Context, key []byte, val []byte) error {
// Get implements Db
func(gdb *gdbmDb) Get(ctx context.Context, key []byte) ([]byte, error) {
- k, err := gdb.ToKey(key)
+ k, err := gdb.ToKey(ctx, key)
if err != nil {
return nil, err
}
diff --git a/db/mem.go b/db/mem.go
@@ -13,7 +13,7 @@ type memDb struct {
}
// NewmemDb returns an in-process volatile Db implementation.
-func NewMemDb(ctx context.Context) *memDb {
+func NewMemDb() *memDb {
db := &memDb{}
db.baseDb.defaultLock()
return db
@@ -29,14 +29,14 @@ func(mdb *memDb) Connect(ctx context.Context, connStr string) error {
}
// convert to a supported map key type
-func(mdb *memDb) toHexKey(key []byte) (string, error) {
- k, err := mdb.ToKey(key)
+func(mdb *memDb) toHexKey(ctx context.Context, key []byte) (string, error) {
+ k, err := mdb.ToKey(ctx, key)
return hex.EncodeToString(k), err
}
// Get implements Db
func(mdb *memDb) Get(ctx context.Context, key []byte) ([]byte, error) {
- k, err := mdb.toHexKey(key)
+ k, err := mdb.toHexKey(ctx, key)
if err != nil {
return nil, err
}
@@ -54,7 +54,7 @@ func(mdb *memDb) Put(ctx context.Context, key []byte, val []byte) error {
if !mdb.checkPut() {
return errors.New("unsafe put and safety set")
}
- k, err := mdb.toHexKey(key)
+ k, err := mdb.toHexKey(ctx, key)
if err != nil {
return err
}
diff --git a/db/mem_test.go b/db/mem_test.go
@@ -10,7 +10,7 @@ func TestPutGetMem(t *testing.T) {
var dbi Db
ctx := context.Background()
sid := "ses"
- db := NewMemDb(ctx)
+ db := NewMemDb()
db.SetPrefix(DATATYPE_USERSTART)
db.SetSession(sid)
diff --git a/db/pg.go b/db/pg.go
@@ -50,7 +50,7 @@ func(pdb *pgDb) Put(ctx context.Context, key []byte, val []byte) error {
if !pdb.checkPut() {
return errors.New("unsafe put and safety set")
}
- k, err := pdb.ToKey(key)
+ k, err := pdb.ToKey(ctx, key)
if err != nil {
return err
}
@@ -70,7 +70,7 @@ func(pdb *pgDb) Put(ctx context.Context, key []byte, val []byte) error {
// Get implements Db.
func(pdb *pgDb) Get(ctx context.Context, key []byte) ([]byte, error) {
- k, err := pdb.ToKey(key)
+ k, err := pdb.ToKey(ctx, key)
if err != nil {
return nil, err
}
diff --git a/engine/default.go b/engine/default.go
@@ -15,7 +15,13 @@ import (
func NewDefaultEngine(dir string, persistDb db.Db, session *string) (EngineIsh, error) {
var err error
st := state.NewState(0)
- rs := resource.NewFsResource(dir)
+ ctx := context.Background()
+ store := db.NewFsDb()
+ store.Connect(ctx, dir)
+ rs, err := resource.NewDbResource(store)
+ if err != nil {
+ return nil, err
+ }
ca := cache.NewCache()
cfg := Config{
Root: "root",
@@ -25,7 +31,6 @@ func NewDefaultEngine(dir string, persistDb db.Db, session *string) (EngineIsh,
} else if persistDb != nil {
return nil, fmt.Errorf("session must be set if persist is used")
}
- ctx := context.TODO()
var en EngineIsh
if persistDb != nil {
pr := persist.NewPersister(persistDb)
@@ -50,8 +55,14 @@ func NewDefaultEngine(dir string, persistDb db.Db, session *string) (EngineIsh,
func NewSizedEngine(dir string, size uint32, persistDb db.Db, session *string) (EngineIsh, error) {
var err error
st := state.NewState(0)
- rs := resource.NewFsResource(dir)
ca := cache.NewCache()
+ ctx := context.Background()
+ store := db.NewFsDb()
+ store.Connect(ctx, dir)
+ rs, err := resource.NewDbResource(store)
+ if err != nil {
+ return nil, err
+ }
cfg := Config{
OutputSize: size,
Root: "root",
@@ -61,7 +72,6 @@ func NewSizedEngine(dir string, size uint32, persistDb db.Db, session *string) (
} else if persistDb != nil {
return nil, fmt.Errorf("session must be set if persist is used")
}
- ctx := context.TODO()
var en EngineIsh
if persistDb != nil {
pr := persist.NewPersister(persistDb)
diff --git a/engine/engine_test.go b/engine/engine_test.go
@@ -4,9 +4,9 @@ import (
"bytes"
"context"
"fmt"
- "io/ioutil"
+// "io/ioutil"
"log"
- "path"
+// "path"
"testing"
"git.defalsify.org/vise.git/cache"
@@ -15,6 +15,7 @@ import (
"git.defalsify.org/vise.git/state"
"git.defalsify.org/vise.git/testdata"
"git.defalsify.org/vise.git/vm"
+ "git.defalsify.org/vise.git/db"
)
var (
@@ -22,45 +23,58 @@ var (
dataDir string = testdata.DataDir
)
-type FsWrapper struct {
- *resource.FsResource
+type testWrapper struct {
+ resource.Resource
st *state.State
+ db db.Db
}
-func NewFsWrapper(path string, st *state.State) FsWrapper {
- rs := resource.NewFsResource(path)
- wr := FsWrapper {
+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)
+ }
+ wr := testWrapper {
rs,
st,
- }
- wr.AddLocalFunc("one", wr.one)
- //wr.AddLocalFunc("inky", wr.inky)
- wr.AddLocalFunc("pinky", wr.pinky)
- wr.AddLocalFunc("set_lang", wr.set_lang)
- wr.AddLocalFunc("translate", wr.translate)
+ store,
+ }
+ rs.AddLocalFunc("one", wr.one)
+ rs.AddLocalFunc("inky", wr.inky)
+ rs.AddLocalFunc("pinky", wr.pinky)
+ rs.AddLocalFunc("set_lang", wr.set_lang)
+ rs.AddLocalFunc("translate", wr.translate)
+ rs.WithCodeGetter(wr.GetCode)
return wr
}
-func(fs FsWrapper) one(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+func(fs testWrapper) getStore() db.Db {
+ return fs.db
+}
+
+func(fs testWrapper) one(ctx context.Context, sym string, input []byte) (resource.Result, error) {
return resource.Result{
Content: "one",
}, nil
}
-func(fs FsWrapper) inky(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+func(fs testWrapper) inky(ctx context.Context, sym string, input []byte) (resource.Result, error) {
return resource.Result{
Content: "tinkywinky",
}, nil
}
-func(fs FsWrapper) pinky(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+func(fs testWrapper) pinky(ctx context.Context, sym string, input []byte) (resource.Result, error) {
r := fmt.Sprintf("xyzzy: %x", input)
return resource.Result{
Content: r,
}, nil
}
-func(fs FsWrapper) translate(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+func(fs testWrapper) translate(ctx context.Context, sym string, input []byte) (resource.Result, error) {
r := "cool"
v := ctx.Value("Language")
code := ""
@@ -76,19 +90,19 @@ func(fs FsWrapper) translate(ctx context.Context, sym string, input []byte) (res
}, nil
}
-func(fs FsWrapper) set_lang(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+func(fs testWrapper) set_lang(ctx context.Context, sym string, input []byte) (resource.Result, error) {
return resource.Result{
Content: string(input),
FlagSet: []uint32{state.FLAG_LANG},
}, nil
}
-func(fs FsWrapper) GetCode(ctx context.Context, sym string) ([]byte, error) {
- sym += ".bin"
- fp := path.Join(fs.Path, sym)
- r, err := ioutil.ReadFile(fp)
- return r, err
-}
+//func(fs testWrapper) GetCode(ctx context.Context, sym string) ([]byte, error) {
+// sym += ".bin"
+// fp := path.Join(fs.Path, sym)
+// r, err := ioutil.ReadFile(fp)
+// return r, err
+//}
func generateTestData(t *testing.T) {
if dataGenerated {
@@ -106,7 +120,7 @@ func TestEngineInit(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(17)
- rs := NewFsWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
@@ -164,7 +178,7 @@ func TestEngineExecInvalidInput(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(17)
- rs := NewFsWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
@@ -186,7 +200,7 @@ func TestEngineResumeTerminated(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(17)
- rs := NewFsWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
@@ -222,7 +236,7 @@ func TestLanguageSet(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := NewFsWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
@@ -275,7 +289,7 @@ func TestLanguageRender(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := NewFsWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache()
cfg := Config{
@@ -315,7 +329,7 @@ func TestConfigLanguageRender(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := NewFsWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache()
cfg := Config{
@@ -344,7 +358,7 @@ func TestConfigLanguageRender(t *testing.T) {
t.Fatal(err)
}
- expect := `dette endrer med språket one
+ expect := `dette endrer med språket tinkywinky
0:tilbake`
r := br.String()
if r != expect {
@@ -372,7 +386,7 @@ func TestPreVm(t *testing.T) {
ctx := context.Background()
st := state.NewState(0)
st.UseDebug()
- rs := NewFsWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache()
cfg := Config{
diff --git a/engine/loop_test.go b/engine/loop_test.go
@@ -9,22 +9,21 @@ import (
"testing"
"git.defalsify.org/vise.git/cache"
- "git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state"
)
func TestLoopTop(t *testing.T) {
+ var err error
generateTestData(t)
- ctx := context.TODO()
+ ctx := context.Background()
st := state.NewState(0)
- rs := resource.NewFsResource(dataDir)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
en := NewEngine(ctx, cfg, &st, rs, ca)
- var err error
_, err = en.Init(ctx)
if err != nil {
t.Fatal(err)
@@ -51,17 +50,17 @@ func TestLoopTop(t *testing.T) {
}
func TestLoopBackForth(t *testing.T) {
+ var err error
generateTestData(t)
- ctx := context.TODO()
+ ctx := context.Background()
st := state.NewState(0)
- rs := resource.NewFsResource(dataDir)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
en := NewEngine(ctx, cfg, &st, rs, ca)
- var err error
_, err = en.Init(ctx)
if err != nil {
t.Fatal(err)
@@ -85,10 +84,11 @@ func TestLoopBackForth(t *testing.T) {
}
func TestLoopBrowse(t *testing.T) {
+ var err error
generateTestData(t)
- ctx := context.TODO()
+ ctx := context.Background()
st := state.NewState(0)
- rs := resource.NewFsResource(dataDir)
+ rs := newTestWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
@@ -96,7 +96,6 @@ func TestLoopBrowse(t *testing.T) {
Root: "root",
}
en := NewEngine(ctx, cfg, &st, rs, ca)
- var err error
_, err = en.Init(ctx)
if err != nil {
t.Fatal(err)
diff --git a/engine/persist_test.go b/engine/persist_test.go
@@ -19,11 +19,11 @@ func TestRunPersist(t *testing.T) {
SessionId: "xyzzy",
Root: "root",
}
- rs := NewFsWrapper(dataDir, nil)
+ rs := newTestWrapper(dataDir, nil)
st := state.NewState(3)
ca := cache.NewCache().WithCacheSize(1024)
- store := db.NewMemDb(context.Background())
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := persist.NewPersister(store).WithContent(&st, ca)
@@ -77,11 +77,11 @@ func TestEnginePersist(t *testing.T) {
SessionId: "xyzzy",
Root: "root",
}
- rs := NewFsWrapper(dataDir, nil)
+ rs := newTestWrapper(dataDir, nil)
st := state.NewState(3)
ca := cache.NewCache().WithCacheSize(1024)
- store := db.NewMemDb(context.Background())
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := persist.NewPersister(store).WithContent(&st, ca)
diff --git a/persist/fs_test.go b/persist/fs_test.go
@@ -29,7 +29,7 @@ func TestSerializeState(t *testing.T) {
ca.Add("blinky", "clyde", 42)
ctx := context.Background()
- store := db.NewMemDb(ctx)
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := NewPersister(store).WithContext(context.Background()).WithSession("xyzzy").WithContent(&st, ca)
v, err := pr.Serialize()
@@ -81,7 +81,7 @@ func TestSaveLoad(t *testing.T) {
ca.Add("blinky", "clyde", 42)
ctx := context.Background()
- store := db.NewMemDb(ctx)
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := NewPersister(store).WithContent(&st, ca)
err := pr.Save("xyzzy")
@@ -122,7 +122,7 @@ func TestSaveLoadFlags(t *testing.T) {
st.SetFlag(8)
ca := cache.NewCache()
- store := db.NewMemDb(ctx)
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := NewPersister(store).WithContent(&st, ca)
err := pr.Save("xyzzy")
diff --git a/persist/persist_test.go b/persist/persist_test.go
@@ -14,7 +14,7 @@ func TestInvalidateState(t *testing.T) {
ca := cache.NewCache()
ctx := context.Background()
- store := db.NewMemDb(ctx)
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := NewPersister(store).WithSession("xyzzy").WithContent(&st, ca)
err := pr.Save("foo")
@@ -36,7 +36,7 @@ func TestInvalidateCache(t *testing.T) {
ca := cache.NewCache()
ctx := context.Background()
- store := db.NewMemDb(ctx)
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := NewPersister(store).WithSession("xyzzy").WithContent(&st, ca)
err := pr.Save("foo")
@@ -58,7 +58,7 @@ func TestInvalidateAll(t *testing.T) {
ca := cache.NewCache()
ctx := context.Background()
- store := db.NewMemDb(ctx)
+ store := db.NewMemDb()
store.Connect(ctx, "")
pr := NewPersister(store).WithSession("xyzzy").WithContent(&st, ca)
err := pr.Save("foo")
diff --git a/render/page_test.go b/render/page_test.go
@@ -6,7 +6,7 @@ import (
"testing"
"git.defalsify.org/vise.git/cache"
- "git.defalsify.org/vise.git/resource"
+ "git.defalsify.org/vise.git/resource/resourcetest"
)
@@ -118,9 +118,10 @@ func TestStateMapSink(t *testing.T) {
}
func TestWithError(t *testing.T) {
+ ctx := context.Background()
ca := cache.NewCache()
- rs := resource.NewMemResource()
- rs.AddTemplate("foo", "bar")
+ rs := resourcetest.NewTestResource()
+ rs.AddTemplate(ctx, "foo", "bar")
pg := NewPage(ca, rs)
ca.Push()
@@ -132,7 +133,6 @@ func TestWithError(t *testing.T) {
err = fmt.Errorf("my humps")
pg = pg.WithMenu(mn).WithError(err)
- ctx := context.TODO()
r, err := pg.Render(ctx, "foo", 0)
if err != nil {
t.Fatal(err)
diff --git a/render/size_test.go b/render/size_test.go
@@ -7,24 +7,28 @@ import (
"git.defalsify.org/vise.git/state"
"git.defalsify.org/vise.git/resource"
+ "git.defalsify.org/vise.git/resource/resourcetest"
"git.defalsify.org/vise.git/cache"
)
-type TestSizeResource struct {
- *resource.MemResource
+type testSizeResource struct {
+ *resourcetest.TestResource
}
-func NewTestSizeResource() TestSizeResource {
- mem := resource.NewMemResource()
- rs := TestSizeResource{&mem}
- rs.AddTemplate("small", "one {{.foo}} two {{.bar}} three {{.baz}}")
- rs.AddTemplate("toobug", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus.")
- rs.AddTemplate("pages", "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}")
- rs.AddEntryFunc("foo", get)
- rs.AddEntryFunc("bar", get)
- rs.AddEntryFunc("baz", get)
- rs.AddEntryFunc("xyzzy", getXyzzy)
- return rs
+func newTestSizeResource() *testSizeResource {
+ ctx := context.Background()
+ rs := resourcetest.NewTestResource()
+ tr := &testSizeResource{
+ TestResource: rs,
+ }
+ 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 tr
}
func get(ctx context.Context, sym string, input []byte) (resource.Result, error) {
@@ -134,7 +138,7 @@ func TestSizeLimit(t *testing.T) {
//rs := TestSizeResource{
// mrs,
//}
- rs := NewTestSizeResource()
+ rs := newTestSizeResource()
szr := NewSizer(128)
pg := NewPage(ca, rs).WithMenu(mn).WithSizer(szr)
ca.Push()
@@ -167,7 +171,7 @@ func TestSizeLimit(t *testing.T) {
mn.Put("1", "foo the foo")
mn.Put("2", "go to bar")
- ctx := context.TODO()
+ ctx := context.Background()
_, err = pg.Render(ctx, "small", 0)
if err != nil {
t.Fatal(err)
@@ -187,7 +191,7 @@ func TestSizePages(t *testing.T) {
//rs := TestSizeResource{
// mrs,
//}
- rs := NewTestSizeResource()
+ rs := newTestSizeResource()
szr := NewSizer(128)
pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
ca.Push()
@@ -204,7 +208,7 @@ func TestSizePages(t *testing.T) {
mn.Put("1", "foo the foo")
mn.Put("2", "go to bar")
- ctx := context.TODO()
+ ctx := context.Background()
r, err := pg.Render(ctx, "pages", 0)
if err != nil {
t.Fatal(err)
@@ -244,7 +248,7 @@ func TestManySizes(t *testing.T) {
st := state.NewState(0)
ca := cache.NewCache()
mn := NewMenu() //.WithOutputSize(32)
- rs := NewTestSizeResource() //.WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
+ rs := newTestSizeResource() //.WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
//rs := TestSizeResource{
// mrs,
//}
@@ -261,7 +265,7 @@ func TestManySizes(t *testing.T) {
pg.Map("baz")
pg.Map("xyzzy")
- ctx := context.TODO()
+ ctx := context.Background()
_, err := pg.Render(ctx, "pages", 0)
if err != nil {
t.Fatal(err)
@@ -274,7 +278,7 @@ func TestManySizesMenued(t *testing.T) {
st := state.NewState(0)
ca := cache.NewCache()
mn := NewMenu() //.WithOutputSize(32)
- rs := NewTestSizeResource()
+ rs := newTestSizeResource()
szr := NewSizer(uint32(i))
pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
ca.Push()
@@ -290,7 +294,7 @@ func TestManySizesMenued(t *testing.T) {
mn.Put("0", "yay")
mn.Put("12", "nay")
- ctx := context.TODO()
+ ctx := context.Background()
_, err := pg.Render(ctx, "pages", 0)
if err != nil {
t.Fatal(err)
@@ -299,9 +303,10 @@ func TestManySizesMenued(t *testing.T) {
}
func TestMenuCollideSink(t *testing.T) {
+ ctx := context.Background()
ca := cache.NewCache()
- rs := resource.NewMemResource()
- rs.AddTemplate("foo", "bar")
+ rs := resourcetest.NewTestResource()
+ rs.AddTemplate(ctx, "foo", "bar")
szr := NewSizer(30)
pg := NewPage(ca, rs).WithSizer(szr)
ca.Push()
@@ -314,7 +319,6 @@ func TestMenuCollideSink(t *testing.T) {
pg = pg.WithMenu(mn)
var err error
- ctx := context.TODO()
_, err = pg.Render(ctx, "foo", 0)
if err != nil {
t.Fatal(err)
@@ -330,9 +334,12 @@ func TestMenuCollideSink(t *testing.T) {
}
func TestMenuSink(t *testing.T) {
+ var err error
+ ctx := context.Background()
+
ca := cache.NewCache()
- rs := resource.NewMemResource()
- rs.AddTemplate("foo", "bar {{.baz}}")
+ rs := resourcetest.NewTestResource()
+ rs.AddTemplate(ctx, "foo", "bar {{.baz}}")
szr := NewSizer(45)
mn := NewMenu().WithSink().WithBrowseConfig(DefaultBrowseConfig())
@@ -348,8 +355,6 @@ func TestMenuSink(t *testing.T) {
ca.Add("baz", "xyzzy", 5)
pg.Map("baz")
- var err error
- ctx := context.TODO()
r, err := pg.Render(ctx, "foo", 0)
if err != nil {
t.Fatal(err)
diff --git a/resource/db.go b/resource/db.go
@@ -9,19 +9,21 @@ import (
)
const (
- resource_max_datatype = db.DATATYPE_TEMPLATE
+ resource_max_datatype = db.DATATYPE_STATICLOAD
)
-type dbResource struct {
- MenuResource
+// DbResource is a MenuResource that uses the given db.Db implementation as data retriever.
+type DbResource struct {
+ *MenuResource
typs uint8
db db.Db
}
-// NewDbFuncGetter returns a MenuResource that uses the given db.Db implementation as data retriever.
-func NewDbResource(store db.Db, typs... uint8) (*dbResource, error) {
+// NewDbFuncGetter instantiates a new DbResource
+func NewDbResource(store db.Db, typs... uint8) (*DbResource, error) {
var v uint8
- g := &dbResource{
+ g := &DbResource{
+ MenuResource: NewMenuResource(),
db: store,
}
for _, v = range(typs) {
@@ -33,11 +35,11 @@ func NewDbResource(store db.Db, typs... uint8) (*dbResource, error) {
return g, nil
}
-func(g *dbResource) fn(ctx context.Context, sym string) ([]byte, error) {
+func(g *DbResource) fn(ctx context.Context, sym string) ([]byte, error) {
return g.db.Get(ctx, []byte(sym))
}
-func(g *dbResource) sfn(ctx context.Context, sym string) (string, error) {
+func(g *DbResource) sfn(ctx context.Context, sym string) (string, error) {
b, err := g.fn(ctx, sym)
if err != nil {
return "", err
@@ -46,7 +48,7 @@ func(g *dbResource) sfn(ctx context.Context, sym string) (string, error) {
}
// GetTemplate implements the Resource interface.
-func(g *dbResource) GetTemplate(ctx context.Context, sym string) (string, error) {
+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")
}
@@ -55,19 +57,47 @@ func(g *dbResource) GetTemplate(ctx context.Context, sym string) (string, error)
}
// GetTemplate implements the Resource interface.
-func(g *dbResource) GetMenu(ctx context.Context, sym string) (string, error) {
+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")
}
g.db.SetPrefix(db.DATATYPE_MENU)
- return g.sfn(ctx, sym)
+ qSym := sym + "_menu"
+ v, err := g.sfn(ctx, qSym)
+ if err != nil {
+ if db.IsNotFound(err) {
+ logg.TraceCtxf(ctx, "menu unresolved", "sym", sym)
+ v = sym
+ }
+ }
+ return v, nil
}
// GetTemplate implements the Resource interface.
-func(g *dbResource) GetCode(ctx context.Context, sym string) ([]byte, error) {
+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")
}
g.db.SetPrefix(db.DATATYPE_BIN)
return g.fn(ctx, sym)
}
+
+func(g *DbResource) FuncFor(ctx context.Context, sym string) (EntryFunc, error) {
+ fn, err := g.MenuResource.FuncFor(ctx, sym)
+ if err == nil {
+ return fn, nil
+ }
+ if g.typs & db.DATATYPE_STATICLOAD == 0 {
+ return nil, errors.New("not a staticload getter")
+ }
+ g.db.SetPrefix(db.DATATYPE_STATICLOAD)
+ b, err := g.fn(ctx, sym)
+ if err != nil {
+ return nil, err
+ }
+ return func(ctx context.Context, nodeSym string, input []byte) (Result, error) {
+ return Result{
+ Content: string(b),
+ }, nil
+ }, nil
+}
diff --git a/resource/db_test.go b/resource/db_test.go
@@ -11,7 +11,7 @@ import (
func TestDb(t *testing.T) {
var rsifc Resource
ctx := context.Background()
- store := db.NewMemDb(ctx)
+ store := db.NewMemDb()
store.Connect(ctx, "")
tg, err := NewDbResource(store, db.DATATYPE_TEMPLATE)
if err != nil {
diff --git a/resource/fs.go b/resource/fs.go
@@ -1,150 +0,0 @@
-package resource
-
-import (
- "context"
- "errors"
- "fmt"
- "io/ioutil"
- "os"
- "path"
- "path/filepath"
- "strings"
-
- "git.defalsify.org/vise.git/lang"
-)
-
-type FsResource struct {
- MenuResource
- Path string
-// languageStrict bool
-}
-
-func NewFsResource(path string) *FsResource {
- absPath, err := filepath.Abs(path)
- if err != nil {
- panic(err)
- }
- return &FsResource{
- Path: absPath,
- }
-}
-
-//func(fsr *FsResource) WithStrictLanguage() *FsResource {
-// fsr.languageStrict = true
-// return fsr
-//}
-
-func(fsr FsResource) GetTemplate(ctx context.Context, sym string) (string, error) {
- fp := path.Join(fsr.Path, sym)
- fpl := fp
- v := ctx.Value("Language")
- if v != nil {
- lang := v.(lang.Language)
- fpl += "_" + lang.Code
- }
- var r []byte
- var err error
- r, err = ioutil.ReadFile(fpl)
- if err != nil {
- if errors.Is(err, os.ErrNotExist) {
- if fpl != fp {
- r, err = ioutil.ReadFile(fp)
- }
- }
- if err != nil {
- return "", fmt.Errorf("failed getting template for sym '%s': %v", sym, err)
- }
- }
- s := string(r)
- return strings.TrimSpace(s), err
-}
-
-func(fsr FsResource) GetCode(ctx context.Context, sym string) ([]byte, error) {
- fb := sym + ".bin"
- fp := path.Join(fsr.Path, fb)
- return ioutil.ReadFile(fp)
-}
-
-func(fsr FsResource) GetMenu(ctx context.Context, sym string) (string, error) {
- fp := path.Join(fsr.Path, sym + "_menu")
- fpl := fp
- v := ctx.Value("Language")
- Logg.DebugCtxf(ctx, "getmenu", "lang", v, "path", fp)
- if v != nil {
- lang := v.(lang.Language)
- fpl += "_" + lang.Code
- }
- var r []byte
- var err error
- r, err = ioutil.ReadFile(fpl)
- if err != nil {
- if errors.Is(err, os.ErrNotExist) {
- if fpl != fp {
- r, err = ioutil.ReadFile(fp)
- }
- }
- if err != nil {
- if errors.Is(err, os.ErrNotExist) {
- return sym, nil
- }
- return "", fmt.Errorf("failed getting template for sym '%s': %v", sym, err)
- }
- }
- s := string(r)
- return strings.TrimSpace(s), err
-}
-
-
-func(fsr FsResource) FuncFor(sym string) (EntryFunc, error) {
- fn, err := fsr.MenuResource.FallbackFunc(sym)
- if err == nil {
- return fn, nil
- }
- _, err = fsr.getFuncNoCtx(sym, nil, nil)
- if err != nil {
- return nil, fmt.Errorf("unknown sym: %s", sym)
- }
- return fsr.getFunc, nil
-}
-
-// String implements the String interface.
-func(fsr FsResource) String() string {
- return fmt.Sprintf("fs resource at path: %s", fsr.Path)
-}
-
-func(fsr FsResource) getFunc(ctx context.Context, sym string, input []byte) (Result, error) {
- v := ctx.Value("Language")
- if v == nil {
- return fsr.getFuncNoCtx(sym, input, nil)
- }
- language := v.(lang.Language)
- return fsr.getFuncNoCtx(sym, input, &language)
-}
-
-func(fsr FsResource) getFuncNoCtx(sym string, input []byte, language *lang.Language) (Result, error) {
- fb := sym
- fbl := fb
- if language != nil {
- fbl += "_" + language.Code
- }
- fb += ".txt"
- fbl += ".txt"
- fp := path.Join(fsr.Path, fb)
- fpl := path.Join(fsr.Path, fbl)
- Logg.Debugf("getfunc search dir", "dir", fsr.Path, "path", fp, "lang_path", fpl, "sym", sym, "language", language)
- r, err := ioutil.ReadFile(fpl)
- if err != nil {
- if errors.Is(err, os.ErrNotExist) {
- if fpl != fp {
- r, err = ioutil.ReadFile(fp)
- }
- }
- if err != nil {
- return Result{}, fmt.Errorf("failed getting data for sym '%s': %v", sym, err)
- }
- }
- s := string(r)
- return Result{
- Content: strings.TrimSpace(s),
- }, nil
-}
diff --git a/resource/fs_test.go b/resource/fs_test.go
@@ -1,126 +0,0 @@
-package resource
-
-import (
- "context"
- "os"
- "path"
- "testing"
-
- "git.defalsify.org/vise.git/lang"
-)
-
-func TestNewFs(t *testing.T) {
- n := NewFsResource("./testdata")
- _ = n
-}
-
-func TestResourceLanguage(t *testing.T) {
- lang, err := lang.LanguageFromCode("nor")
- if err != nil {
- t.Fatal(err)
- }
- ctx := context.TODO()
-
- dir, err := os.MkdirTemp("", "vise_fsresource")
- if err != nil {
- t.Fatal(err)
- }
-
- fp := path.Join(dir, "foo")
- tmpl := "one two three"
- err = os.WriteFile(fp, []byte(tmpl), 0600)
- if err != nil {
- t.Fatal(err)
- }
-
- rs := NewFsResource(dir)
- r, err := rs.GetTemplate(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != tmpl {
- t.Fatalf("expected '%s', got '%s'", tmpl, r)
- }
-
- ctx = context.WithValue(ctx, "Language", lang)
- rs = NewFsResource(dir)
- r, err = rs.GetTemplate(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != tmpl {
- t.Fatalf("expected '%s', got '%s'", tmpl, r)
- }
-
- tmpl = "en to tre"
- err = os.WriteFile(fp + "_nor", []byte(tmpl), 0600)
- if err != nil {
- t.Fatal(err)
- }
- r, err = rs.GetTemplate(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != tmpl {
- t.Fatalf("expected '%s', got '%s'", tmpl, r)
- }
-}
-
-func TestResourceMenuLanguage(t *testing.T) {
- lang, err := lang.LanguageFromCode("nor")
- if err != nil {
- t.Fatal(err)
- }
- ctx := context.TODO()
-
- dir, err := os.MkdirTemp("", "vise_fsresource")
- if err != nil {
- t.Fatal(err)
- }
- rs := NewFsResource(dir)
-
- r, err := rs.GetMenu(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != "foo" {
- t.Fatalf("expected 'foo', got '%s'", r)
- }
-
- fp := path.Join(dir, "foo_menu")
- menu := "foo bar"
- err = os.WriteFile(fp, []byte(menu), 0600)
- if err != nil {
- t.Fatal(err)
- }
- r, err = rs.GetMenu(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != menu {
- t.Fatalf("expected '%s', got '%s'", menu, r)
- }
-
- ctx = context.WithValue(ctx, "Language", lang)
- r, err = rs.GetMenu(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != menu {
- t.Fatalf("expected '%s', got '%s'", menu, r)
- }
-
- fp = path.Join(dir, "foo_menu_nor")
- menu = "baz bar"
- err = os.WriteFile(fp, []byte(menu), 0600)
- if err != nil {
- t.Fatal(err)
- }
- r, err = rs.GetMenu(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != menu {
- t.Fatalf("expected '%s', got '%s'", menu, r)
- }
-}
diff --git a/resource/log.go b/resource/log.go
@@ -5,5 +5,5 @@ import (
)
var (
- Logg logging.Logger = logging.NewVanilla().WithDomain("resource")
+ logg logging.Logger = logging.NewVanilla().WithDomain("resource")
)
diff --git a/resource/mem.go b/resource/mem.go
@@ -1,75 +0,0 @@
-package resource
-
-import (
- "context"
- "fmt"
-)
-
-type MemResource struct {
- MenuResource
- templates map[string]string
- bytecodes map[string][]byte
- menus map[string]string
- funcs map[string]EntryFunc
-}
-
-func NewMemResource() MemResource {
- mr := MemResource{
- templates: make(map[string]string),
- bytecodes: make(map[string][]byte),
- funcs: make(map[string]EntryFunc),
- }
- mr.WithCodeGetter(mr.getCode)
- mr.WithTemplateGetter(mr.getTemplate)
- mr.WithEntryFuncGetter(mr.getFunc)
- mr.WithMenuGetter(mr.getMenu)
- return mr
-}
-
-func(mr MemResource) getTemplate(ctx context.Context, sym string) (string, error) {
- r, ok := mr.templates[sym]
- if !ok {
- return "", fmt.Errorf("unknown template symbol: %s", sym)
- }
- return r, nil
-}
-
-func(mr MemResource) getCode(ctx context.Context, sym string) ([]byte, error) {
- r, ok := mr.bytecodes[sym]
- if !ok {
- return nil, fmt.Errorf("unknown bytecode: %s", sym)
- }
- return r, nil
-}
-
-func(mr MemResource) getMenu(ctx context.Context, sym string) (string, error) {
- r, ok := mr.menus[sym]
- if !ok {
- r = sym
- }
- return r, nil
-
-}
-
-func(mr MemResource) getFunc(sym string) (EntryFunc, error) {
- r, ok := mr.funcs[sym]
- if !ok {
- return nil, fmt.Errorf("unknown entry func: %s", sym)
- }
- return r, nil
-}
-
-func(mr *MemResource) AddTemplate(sym string, tpl string) {
- Logg.Tracef("mem resource added template", "sym", sym, "length", len(tpl))
- mr.templates[sym] = tpl
-}
-
-
-func(mr *MemResource) AddEntryFunc(sym string, fn EntryFunc) {
- mr.funcs[sym] = fn
-}
-
-func(mr *MemResource) AddBytecode(sym string, code []byte) {
- Logg.Tracef("mem resource added bytecode", "sym", sym, "code", code)
- mr.bytecodes[sym] = code
-}
diff --git a/resource/mem_test.go b/resource/mem_test.go
@@ -1,76 +0,0 @@
-package resource
-
-import (
- "bytes"
- "context"
- "fmt"
- "testing"
-)
-
-func testEntry(ctx context.Context, sym string, input []byte) (Result, error) {
- return Result{
- Content: fmt.Sprintf("%sbar", input),
- }, nil
-}
-
-func TestMemResourceTemplate(t *testing.T) {
- rs := NewMemResource()
- rs.AddTemplate("foo", "bar")
-
- ctx := context.TODO()
- r, err := rs.GetTemplate(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if r != "bar" {
- fmt.Errorf("expected 'bar', got %s", r)
- }
-
- _, err = rs.GetTemplate(ctx, "bar")
- if err == nil {
- t.Fatalf("expected error")
- }
-}
-
-func TestMemResourceCode(t *testing.T) {
- rs := NewMemResource()
- rs.AddBytecode("foo", []byte("bar"))
-
- ctx := context.Background()
- r, err := rs.GetCode(ctx, "foo")
- if err != nil {
- t.Fatal(err)
- }
- if !bytes.Equal(r, []byte("bar")) {
- fmt.Errorf("expected 'bar', got %x", r)
- }
-
- _, err = rs.GetCode(ctx, "bar")
- if err == nil {
- t.Fatalf("expected error")
- }
-}
-
-func TestMemResourceEntry(t *testing.T) {
- rs := NewMemResource()
- rs.AddEntryFunc("foo", testEntry)
-
- fn, err := rs.FuncFor("foo")
- if err != nil {
- t.Fatal(err)
- }
-
- ctx := context.TODO()
- r, err := fn(ctx, "foo", []byte("xyzzy"))
- if err != nil {
- t.Fatal(err)
- }
- if r.Content != "foobar" {
- fmt.Errorf("expected 'foobar', got %x", r.Content)
- }
-
- _, err = rs.FuncFor("bar")
- if err == nil {
- t.Fatalf("expected error")
- }
-}
diff --git a/resource/resource.go b/resource/resource.go
@@ -40,7 +40,7 @@ type Resource interface {
// GetMenu retrieves the menu label associated with the given symbol.
GetMenu(ctx context.Context, menuSym string) (string, error)
// FuncFor retrieves the external function (EntryFunc) associated with the given symbol.
- FuncFor(loadSym string) (EntryFunc, error)
+ FuncFor(ctx context.Context, loadSym string) (EntryFunc, error)
}
// MenuResource contains the base definition for building Resource implementations.
@@ -87,7 +87,7 @@ func(m *MenuResource) WithMenuGetter(menuGetter MenuFunc) *MenuResource {
}
// FuncFor implements Resource interface.
-func(m MenuResource) FuncFor(sym string) (EntryFunc, error) {
+func(m MenuResource) FuncFor(ctx context.Context, sym string) (EntryFunc, error) {
return m.funcFunc(sym)
}
diff --git a/resource/resource_test.go b/resource/resource_test.go
@@ -1,49 +0,0 @@
-package resource
-
-import (
- "context"
- "fmt"
-)
-
-
-type TestSizeResource struct {
- *MemResource
-}
-
-func NewTestSizeResource() TestSizeResource {
- mem := NewMemResource()
- rs := TestSizeResource{&mem}
- rs.AddTemplate("small", "one {{.foo}} two {{.bar}} three {{.baz}}")
- rs.AddTemplate("toobug", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus.")
- rs.AddTemplate("pages", "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}")
- rs.AddEntryFunc("foo", get)
- rs.AddEntryFunc("bar", get)
- rs.AddEntryFunc("baz", get)
- rs.AddEntryFunc("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
-}
diff --git a/resource/resourcetest/util.go b/resource/resourcetest/util.go
@@ -0,0 +1,72 @@
+package resourcetest
+
+import (
+ "context"
+
+ "git.defalsify.org/vise.git/resource"
+ "git.defalsify.org/vise.git/db"
+)
+
+type TestResource struct {
+ *resource.DbResource
+ db db.Db
+ ctx context.Context
+}
+
+func NewTestResource() *TestResource {
+ return newTestResource("")
+}
+
+func NewTestFsResource(path string) *TestResource {
+ if path == "" {
+ panic("empty path")
+ }
+ return newTestResource(path)
+}
+
+func newTestResource(path string) *TestResource {
+ var store db.Db
+ ctx := context.Background()
+
+ if path == "" {
+ mem := db.NewMemDb()
+ mem.SetLock(db.DATATYPE_TEMPLATE, false)
+ mem.SetLock(db.DATATYPE_BIN, false)
+ mem.SetLock(db.DATATYPE_MENU, false)
+ store = mem
+ } else {
+ fs := db.NewMemDb()
+ fs.SetLock(db.DATATYPE_TEMPLATE, false)
+ fs.SetLock(db.DATATYPE_BIN, false)
+ fs.SetLock(db.DATATYPE_MENU, false)
+ store = fs
+ }
+
+ store.Connect(ctx, path)
+ rsd, _ := resource.NewDbResource(store, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU, db.DATATYPE_BIN)
+ rs := &TestResource{
+ DbResource: rsd,
+ ctx: ctx,
+ db: store,
+ }
+ return rs
+}
+
+func(tr *TestResource) AddTemplate(ctx context.Context, key string, val string) error {
+ tr.db.SetPrefix(db.DATATYPE_TEMPLATE)
+ return tr.db.Put(ctx, []byte(key), []byte(val))
+}
+
+func(tr *TestResource) AddBytecode(ctx context.Context, key string, val []byte) error {
+ tr.db.SetPrefix(db.DATATYPE_BIN)
+ return tr.db.Put(ctx, []byte(key), val)
+}
+
+func(tr *TestResource) AddMenu(ctx context.Context, key string, val string) error {
+ tr.db.SetPrefix(db.DATATYPE_MENU)
+ return tr.db.Put(ctx, []byte(key), []byte(val))
+}
+
+func(tr *TestResource) AddFunc(ctx context.Context, key string, fn resource.EntryFunc) {
+ tr.AddLocalFunc(key, fn)
+}
diff --git a/resource/resourcetest/util_test.go b/resource/resourcetest/util_test.go
@@ -0,0 +1,55 @@
+package resourcetest
+
+import (
+ "context"
+ "fmt"
+
+ "git.defalsify.org/vise.git/db"
+ "git.defalsify.org/vise.git/resource/resourcetest"
+)
+
+
+//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
+}
diff --git a/state/state.go b/state/state.go
@@ -11,6 +11,10 @@ var (
IndexError = fmt.Errorf("already at first index")
)
+const (
+ MaxLevel = 128
+)
+
// State holds the command stack, error condition of a unique execution session.
//
// It also holds cached values for all results of executed symbols.
@@ -268,6 +272,11 @@ func(st *State) Top() (bool, error) {
//
// Clears mapping and sink.
func(st *State) Down(input string) error {
+ var n uint16
+ if len(st.ExecPath) > MaxLevel {
+ panic("maxlevel")
+ return fmt.Errorf("max levels exceeded (%d)", n)
+ }
st.ExecPath = append(st.ExecPath, input)
st.SizeIdx = 0
st.Moves += 1
diff --git a/testdata/testdata.go b/testdata/testdata.go
@@ -1,186 +1,88 @@
package testdata
import (
+ "context"
"fmt"
"io/ioutil"
"os"
- "path"
- testdataloader "github.com/peteole/testdata-loader"
-
- "git.defalsify.org/vise.git/vm"
+ "git.defalsify.org/vise.git/db"
+ "git.defalsify.org/vise.git/resource"
+ "git.defalsify.org/vise.git/logging"
)
-
-type genFunc func() error
+//
+//type TestdataDb struct {
+// db db.Db
+//}
+//
+//var (
+// store *TestdataDb
+// ctx = context.Background()
+//)
+//
+//func NewTestDataDb(path string) *TestdataDb {
+// store := db.NewFsDb()
+// store.Connect(ctx, path)
+// store.SetLock(db.DATATYPE_BIN, false)
+// store.SetLock(db.DATATYPE_MENU, false)
+// store.SetLock(db.DATATYPE_TEMPLATE, false)
+// return &TestdataDb{
+// db: store,
+// }
+//}
var (
- BaseDir = testdataloader.GetBasePath()
- DataDir = ""
- dirLock = false
+ ctx = context.Background()
+ //testResource = resourcetest.NewTestResource()
+ store = db.NewFsDb()
+ out = outNew
+ logg = logging.NewVanilla().WithDomain("testdata")
)
-func out(sym string, b []byte, tpl string, data map[string]string) error {
- fp := path.Join(DataDir, sym)
- err := ioutil.WriteFile(fp, []byte(tpl), 0644)
+type echoFunc struct {
+ v string
+}
+
+func(e *echoFunc) get(ctx context.Context, nodeSym string, input []byte) (resource.Result, error) {
+ return resource.Result{
+ Content: e.v,
+ }, nil
+}
+
+func outNew(sym string, b []byte, tpl string, data map[string]string) error {
+ logg.Debugf("testdata out", "sym", sym)
+ store.SetPrefix(db.DATATYPE_TEMPLATE)
+ err := store.Put(ctx, []byte(sym), []byte(tpl))
if err != nil {
return err
}
-
- fb := sym + ".bin"
- fp = path.Join(DataDir, fb)
- err = ioutil.WriteFile(fp, b, 0644)
+ store.SetPrefix(db.DATATYPE_BIN)
+ err = store.Put(ctx, []byte(sym), b)
if err != nil {
return err
}
-
- if data == nil {
- return nil
- }
-
+ store.SetPrefix(db.DATATYPE_STATICLOAD)
for k, v := range data {
- fb := k + ".txt"
- fp = path.Join(DataDir, fb)
- err = ioutil.WriteFile(fp, []byte(v), 0644)
+ logg.Debugf("testdata out staticload", "sym", sym, "k", k, "v", v)
+ err = store.Put(ctx, []byte(k), []byte(v))
if err != nil {
return err
}
}
-
return nil
}
-func root() error {
- b := []byte{}
- b = vm.NewLine(b, vm.MOUT, []string{"do the foo", "1"}, nil, nil)
- b = vm.NewLine(b, vm.MOUT, []string{"go to the bar", "2"}, nil, nil)
- b = vm.NewLine(b, vm.MOUT, []string{"language template", "3"}, nil, nil)
- b = vm.NewLine(b, vm.HALT, nil, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"foo", "1"}, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"bar", "2"}, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"lang", "3"}, nil, nil)
-
- tpl := "hello world"
-
- return out("root", b, tpl, nil)
-}
-
-func foo() error {
- b := []byte{}
- b = vm.NewLine(b, vm.MOUT, []string{"to foo", "0"}, nil, nil)
- b = vm.NewLine(b, vm.MOUT, []string{"go bar", "1"}, nil, nil)
- b = vm.NewLine(b, vm.MOUT, []string{"see long", "2"}, nil, nil)
- b = vm.NewLine(b, vm.LOAD, []string{"inky"}, []byte{20}, nil)
- b = vm.NewLine(b, vm.HALT, nil, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"_", "0"}, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"baz", "1"}, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"long", "2"}, nil, nil)
-
- data := make(map[string]string)
- data["inky"] = "one"
-
- tpl := `this is in foo
-
-it has more lines`
-
- return out("foo", b, tpl, data)
-}
-
-func bar() error {
- b := []byte{}
- b = vm.NewLine(b, vm.LOAD, []string{"pinky"}, []byte{0}, nil)
- b = vm.NewLine(b, vm.HALT, nil, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"^", "*"}, nil, nil)
-
- tpl := "this is bar - any input will return to top"
-
- data := make(map[string]string)
- data["pinky"] = "two"
-
- return out("bar", b, tpl, data)
-}
-
-func baz() error {
- b := []byte{}
- b = vm.NewLine(b, vm.MAP, []string{"inky"}, nil, nil)
- b = vm.NewLine(b, vm.HALT, nil, nil, nil)
-
- tpl := "this is baz which uses the var {{.inky}} in the template."
-
- return out("baz", b, tpl, nil)
-}
-
-func long() error {
- b := []byte{}
- b = vm.NewLine(b, vm.MOUT, []string{"back", "0"}, nil, nil)
- b = vm.NewLine(b, vm.MNEXT, []string{"nexxt", "00"}, nil, nil)
- b = vm.NewLine(b, vm.MPREV, []string{"prevvv", "11"}, nil, nil)
- b = vm.NewLine(b, vm.LOAD, []string{"longdata"}, []byte{0x00}, nil)
- b = vm.NewLine(b, vm.MAP, []string{"longdata"}, nil, nil)
- b = vm.NewLine(b, vm.HALT, nil, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"_", "0"}, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{">", "00"}, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"<", "11"}, nil, nil)
-
- tpl := `data
-{{.longdata}}`
-
- data := make(map[string]string)
- data["longdata"] = `INKY 12
-PINKY 5555
-BLINKY 3t7
-CLYDE 11
-TINKYWINKY 22
-DIPSY 666
-LALA 111
-POO 222
-`
-
- return out("long", b, tpl, data)
-}
-
-func defaultCatch() error {
- b := []byte{}
- b = vm.NewLine(b, vm.MOUT, []string{"back", "0"}, nil, nil)
- b = vm.NewLine(b, vm.HALT, nil, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"_", "*"}, nil, nil)
-
- tpl := "invalid input"
-
- return out("_catch", b, tpl, nil)
-}
-
-func lang() error {
- b := []byte{}
- b = vm.NewLine(b, vm.MOUT, []string{"back", "0"}, nil, nil)
- b = vm.NewLine(b, vm.LOAD, []string{"inky"}, []byte{20}, nil)
- b = vm.NewLine(b, vm.MAP, []string{"inky"}, nil, nil)
- b = vm.NewLine(b, vm.HALT, nil, nil, nil)
- b = vm.NewLine(b, vm.INCMP, []string{"_", "*"}, nil, nil)
-
- tpl := "this changes with language {{.inky}}"
-
- err := out("lang", b, tpl, nil)
- if err != nil {
- return err
- }
-
- tpl = "dette endrer med språket {{.inky}}"
- fp := path.Join(DataDir, "lang_nor")
- err = os.WriteFile(fp, []byte(tpl), 0600)
- if err != nil {
- return err
- }
-
- menu := "tilbake"
- fp = path.Join(DataDir, "back_menu_nor")
- return os.WriteFile(fp, []byte(menu), 0600)
-}
-
func generate() error {
err := os.MkdirAll(DataDir, 0755)
if err != nil {
return err
}
+ store = db.NewFsDb()
+ store.Connect(ctx, DataDir)
+ store.SetLock(db.DATATYPE_TEMPLATE, false)
+ store.SetLock(db.DATATYPE_BIN, false)
+ store.SetLock(db.DATATYPE_MENU, false)
+ store.SetLock(db.DATATYPE_STATICLOAD, false)
fns := []genFunc{root, foo, bar, baz, long, lang, defaultCatch}
for _, fn := range fns {
diff --git a/vm/debug.go b/vm/debug.go
@@ -13,7 +13,7 @@ func ToString(b []byte) (string, error) {
if err != nil {
return "", err
}
- Logg.Tracef("", "bytes_written", n)
+ logg.Tracef("", "bytes_written", n)
return buf.String(), nil
}
@@ -152,7 +152,7 @@ func ParseAll(b []byte, w io.Writer) (int, error) {
return rn, err
}
rn += n
- Logg.Tracef("instruction debug write", "bytes", n, "instruction", s)
+ logg.Tracef("instruction debug write", "bytes", n, "instruction", s)
}
//rs += "\n"
diff --git a/vm/log.go b/vm/log.go
@@ -5,5 +5,5 @@ import (
)
var (
- Logg logging.Logger = logging.NewVanilla().WithDomain("vm")
+ logg logging.Logger = logging.NewVanilla().WithDomain("vm")
)
diff --git a/vm/runner.go b/vm/runner.go
@@ -33,7 +33,7 @@ func(e *ExternalCodeError) WithCode(code int) *ExternalCodeError {
// Error implements the Error interface.
func(e ExternalCodeError) Error() string {
- Logg.Errorf("external code error: %v", e.err)
+ logg.Errorf("external code error: %v", e.err)
return fmt.Sprintf("error %v:%v", e.sym, e.code)
}
@@ -58,7 +58,7 @@ func NewVm(st *state.State, rs resource.Resource, ca cache.Memory, sizer *render
sizer: sizer,
}
vmi.Reset()
- Logg.Infof("vm created with state", "state", st, "renderer", vmi.pg)
+ logg.Infof("vm created with state", "state", st, "renderer", vmi.pg)
return vmi
}
@@ -78,12 +78,12 @@ func(vmi *Vm) Reset() {
//
// On error, the remaining instructions will be returned. State will not be rolled back.
func(vm *Vm) Run(ctx context.Context, b []byte) ([]byte, error) {
- Logg.Tracef("new vm run")
+ logg.Tracef("new vm run")
running := true
for running {
r := vm.st.MatchFlag(state.FLAG_TERMINATE, true)
if r {
- Logg.InfoCtxf(ctx, "terminate set! bailing")
+ logg.InfoCtxf(ctx, "terminate set! bailing")
return []byte{}, nil
}
@@ -109,8 +109,8 @@ func(vm *Vm) Run(ctx context.Context, b []byte) ([]byte, error) {
return b, err
}
b = bb
- Logg.DebugCtxf(ctx, "execute code", "opcode", op, "op", OpcodeString[op], "code", b)
- Logg.DebugCtxf(ctx, "", "state", vm.st)
+ logg.DebugCtxf(ctx, "execute code", "opcode", op, "op", OpcodeString[op], "code", b)
+ logg.DebugCtxf(ctx, "", "state", vm.st)
switch op {
case CATCH:
b, err = vm.runCatch(ctx, b)
@@ -186,17 +186,17 @@ func(vm *Vm) runDeadCheck(ctx context.Context, b []byte) ([]byte, error) {
}
r := vm.st.MatchFlag(state.FLAG_READIN, false)
if r {
- Logg.DebugCtxf(ctx, "Not processing input. Setting terminate")
+ logg.DebugCtxf(ctx, "Not processing input. Setting terminate")
vm.st.SetFlag(state.FLAG_TERMINATE)
return b, nil
}
r = vm.st.MatchFlag(state.FLAG_TERMINATE, true)
if r {
- Logg.TraceCtxf(ctx, "Terminate found!!")
+ logg.TraceCtxf(ctx, "Terminate found!!")
return b, nil
}
- Logg.TraceCtxf(ctx, "no code remaining but not terminating")
+ logg.TraceCtxf(ctx, "no code remaining but not terminating")
location, _ := vm.st.Where()
if location == "" {
return b, fmt.Errorf("dead runner with no current location")
@@ -233,7 +233,7 @@ func(vm *Vm) runCatch(ctx context.Context, b []byte) ([]byte, error) {
if err != nil {
return b, err
}
- Logg.InfoCtxf(ctx, "catch!", "flag", sig, "sym", sym, "target", actualSym, "mode", mode)
+ logg.InfoCtxf(ctx, "catch!", "flag", sig, "sym", sym, "target", actualSym, "mode", mode)
sym = actualSym
bh, err := vm.rs.GetCode(ctx, sym)
if err != nil {
@@ -252,7 +252,7 @@ func(vm *Vm) runCroak(ctx context.Context, b []byte) ([]byte, error) {
}
r := vm.st.MatchFlag(sig, mode)
if r {
- Logg.InfoCtxf(ctx, "croak! purging and moving to top", "signal", sig)
+ logg.InfoCtxf(ctx, "croak! purging and moving to top", "signal", sig)
vm.Reset()
vm.ca.Reset()
b = []byte{}
@@ -268,7 +268,7 @@ func(vm *Vm) runLoad(ctx context.Context, b []byte) ([]byte, error) {
}
_, err = vm.ca.Get(sym)
if err == nil {
- Logg.DebugCtxf(ctx, "skip already loaded symbol", "symbol", sym)
+ logg.DebugCtxf(ctx, "skip already loaded symbol", "symbol", sym)
return b, nil
}
r, err := vm.refresh(sym, vm.rs, ctx)
@@ -314,7 +314,7 @@ func(vm *Vm) runMove(ctx context.Context, b []byte) ([]byte, error) {
if err != nil {
return b, err
}
- Logg.DebugCtxf(ctx, "loaded code", "sym", sym, "code", code)
+ logg.DebugCtxf(ctx, "loaded code", "sym", sym, "code", code)
b = append(b, code...)
vm.Reset()
return b, nil
@@ -335,7 +335,7 @@ func(vm *Vm) runInCmp(ctx context.Context, b []byte) ([]byte, error) {
}
if have {
if reading {
- Logg.DebugCtxf(ctx, "ignoring input - already have match", "input", sym)
+ logg.DebugCtxf(ctx, "ignoring input - already have match", "input", sym)
return b, nil
}
} else {
@@ -345,15 +345,15 @@ func(vm *Vm) runInCmp(ctx context.Context, b []byte) ([]byte, error) {
if err != nil {
return b, err
}
- Logg.TraceCtxf(ctx, "testing sym", "sym", sym, "input", input)
+ logg.TraceCtxf(ctx, "testing sym", "sym", sym, "input", input)
if !have && target == "*" {
- Logg.DebugCtxf(ctx, "input wildcard match", "input", input, "next", sym)
+ logg.DebugCtxf(ctx, "input wildcard match", "input", input, "next", sym)
} else {
if target != string(input) {
return b, nil
}
- Logg.InfoCtxf(ctx, "input match", "input", input, "next", sym)
+ logg.InfoCtxf(ctx, "input match", "input", input, "next", sym)
}
vm.st.SetFlag(state.FLAG_INMATCH)
vm.st.ResetFlag(state.FLAG_READIN)
@@ -377,7 +377,7 @@ func(vm *Vm) runInCmp(ctx context.Context, b []byte) ([]byte, error) {
if err != nil {
return b, err
}
- Logg.DebugCtxf(ctx, "loaded additional code", "next", sym, "code", code)
+ logg.DebugCtxf(ctx, "loaded additional code", "next", sym, "code", code)
b = append(b, code...)
return b, err
}
@@ -389,7 +389,7 @@ func(vm *Vm) runHalt(ctx context.Context, b []byte) ([]byte, error) {
if err != nil {
return b, err
}
- Logg.DebugCtxf(ctx, "found HALT, stopping")
+ logg.DebugCtxf(ctx, "found HALT, stopping")
vm.st.SetFlag(state.FLAG_WAIT)
return b, nil
@@ -469,7 +469,7 @@ func(vm *Vm) Render(ctx context.Context) (string, error) {
func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (string, error) {
var err error
- fn, err := rs.FuncFor(key)
+ fn, err := rs.FuncFor(ctx, key)
if err != nil {
return "", err
}
@@ -482,7 +482,7 @@ func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (str
_ = vm.st.SetFlag(state.FLAG_LOADFAIL)
return "", NewExternalCodeError(key, err).WithCode(r.Status)
}
- Logg.TraceCtxf(ctx, "foo", "flags", r.FlagSet)
+ logg.TraceCtxf(ctx, "foo", "flags", r.FlagSet)
for _, flag := range r.FlagReset {
if !state.IsWriteableFlag(flag) {
continue
diff --git a/vm/runner_test.go b/vm/runner_test.go
@@ -10,58 +10,64 @@ import (
"git.defalsify.org/vise.git/cache"
"git.defalsify.org/vise.git/render"
"git.defalsify.org/vise.git/resource"
+ "git.defalsify.org/vise.git/resource/resourcetest"
"git.defalsify.org/vise.git/state"
)
-var dynVal = "three"
+var (
+ ctx = context.Background()
+ dynVal = "three"
+)
-type TestResource struct {
- resource.MemResource
+type testResource struct {
+ *resourcetest.TestResource
state *state.State
RootCode []byte
CatchContent string
}
-func NewTestResource(st *state.State) TestResource {
- tr := TestResource{
- MemResource: resource.NewMemResource(),
- state: st,
- }
- tr.AddTemplate("foo", "inky pinky blinky clyde")
- tr.AddTemplate("bar", "inky pinky {{.one}} blinky {{.two}} clyde")
- tr.AddTemplate("baz", "inky pinky {{.baz}} blinky clyde")
- tr.AddTemplate("three", "{{.one}} inky pinky {{.three}} blinky clyde {{.two}}")
- tr.AddTemplate("root", "root")
- tr.AddTemplate("_catch", tr.CatchContent)
- tr.AddTemplate("ouf", "ouch")
- tr.AddTemplate("flagCatch", "flagiee")
- tr.AddEntryFunc("one", getOne)
- tr.AddEntryFunc("two", getTwo)
- tr.AddEntryFunc("dyn", getDyn)
- tr.AddEntryFunc("arg", tr.getInput)
- tr.AddEntryFunc("echo", getEcho)
- tr.AddEntryFunc("setFlagOne", setFlag)
- tr.AddEntryFunc("set_lang", set_lang)
- tr.AddEntryFunc("aiee", uhOh)
+func newTestResource(st *state.State) testResource {
+ rs := resourcetest.NewTestResource()
+ tr := testResource{
+ TestResource: rs,
+ state: st,
+ }
+ rs.AddTemplate(ctx, "foo", "inky pinky blinky clyde")
+ rs.AddTemplate(ctx, "bar", "inky pinky {{.one}} blinky {{.two}} clyde")
+ rs.AddTemplate(ctx, "baz", "inky pinky {{.baz}} blinky clyde")
+ rs.AddTemplate(ctx, "three", "{{.one}} inky pinky {{.three}} blinky clyde {{.two}}")
+ rs.AddTemplate(ctx, "root", "root")
+ rs.AddTemplate(ctx, "_catch", tr.CatchContent)
+ rs.AddTemplate(ctx, "ouf", "ouch")
+ rs.AddTemplate(ctx, "flagCatch", "flagiee")
+ rs.AddMenu(ctx, "one", "one")
+ rs.AddMenu(ctx, "two", "two")
+ rs.AddLocalFunc("two", getTwo)
+ rs.AddLocalFunc("dyn", getDyn)
+ rs.AddLocalFunc("arg", tr.getInput)
+ rs.AddLocalFunc("echo", getEcho)
+ rs.AddLocalFunc("setFlagOne", setFlag)
+ rs.AddLocalFunc("set_lang", set_lang)
+ rs.AddLocalFunc("aiee", uhOh)
var b []byte
b = NewLine(nil, HALT, nil, nil, nil)
- tr.AddBytecode("one", b)
+ rs.AddBytecode(ctx, "one", b)
b = NewLine(nil, MOUT, []string{"repent", "0"}, nil, nil)
b = NewLine(b, HALT, nil, nil, nil)
- tr.AddBytecode("_catch", b)
+ rs.AddBytecode(ctx, "_catch", b)
b = NewLine(nil, MOUT, []string{"repent", "0"}, nil, nil)
b = NewLine(b, HALT, nil, nil, nil)
b = NewLine(b, MOVE, []string{"_"}, nil, nil)
- tr.AddBytecode("flagCatch", b)
+ rs.AddBytecode(ctx, "flagCatch", b)
b = NewLine(nil, MOUT, []string{"oo", "1"}, nil, nil)
b = NewLine(b, HALT, nil, nil, nil)
- tr.AddBytecode("ouf", b)
+ rs.AddBytecode(ctx, "ouf", b)
- tr.AddBytecode("root", tr.RootCode)
+ rs.AddBytecode(ctx, "root", tr.RootCode)
return tr
}
@@ -122,7 +128,7 @@ func set_lang(ctx context.Context, sym string, input []byte) (resource.Result, e
// state *state.State
//}
-func (r TestResource) FuncFor(sym string) (resource.EntryFunc, error) {
+func (r testResource) FuncFor(ctx context.Context, sym string) (resource.EntryFunc, error) {
switch sym {
case "one":
return getOne, nil
@@ -144,14 +150,14 @@ func (r TestResource) FuncFor(sym string) (resource.EntryFunc, error) {
return nil, fmt.Errorf("invalid function: '%s'", sym)
}
-func(r TestResource) getInput(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+func(r testResource) getInput(ctx context.Context, sym string, input []byte) (resource.Result, error) {
v, err := r.state.GetInput()
return resource.Result{
Content: string(v),
}, err
}
-func(r TestResource) getCode(sym string) ([]byte, error) {
+func(r testResource) getCode(sym string) ([]byte, error) {
var b []byte
switch sym {
case "_catch":
@@ -173,7 +179,7 @@ func(r TestResource) getCode(sym string) ([]byte, error) {
func TestRun(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -194,7 +200,7 @@ func TestRun(t *testing.T) {
func TestRunLoadRender(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -246,7 +252,7 @@ func TestRunLoadRender(t *testing.T) {
func TestRunMultiple(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -264,7 +270,7 @@ func TestRunMultiple(t *testing.T) {
func TestRunReload(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
szr := render.NewSizer(128)
vm := NewVm(&st, &rs, ca, szr)
@@ -296,7 +302,7 @@ func TestRunReload(t *testing.T) {
func TestHalt(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -321,7 +327,7 @@ func TestHalt(t *testing.T) {
func TestRunArg(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -348,7 +354,7 @@ func TestRunArg(t *testing.T) {
func TestRunInputHandler(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -376,7 +382,7 @@ func TestRunInputHandler(t *testing.T) {
func TestRunArgInvalid(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -410,7 +416,7 @@ func TestRunArgInvalid(t *testing.T) {
func TestRunMenu(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -418,7 +424,7 @@ func TestRunMenu(t *testing.T) {
ctx := context.Background()
- rs.AddBytecode("foo", []byte{})
+ rs.AddBytecode(ctx, "foo", []byte{})
b := NewLine(nil, MOVE, []string{"foo"}, nil, nil)
b = NewLine(b, MOUT, []string{"one", "0"}, nil, nil)
b = NewLine(b, MOUT, []string{"two", "1"}, nil, nil)
@@ -446,7 +452,7 @@ func TestRunMenu(t *testing.T) {
func TestRunMenuBrowse(t *testing.T) {
log.Printf("This test is incomplete, it must check the output of a menu browser once one is implemented. For now it only checks whether it can execute the runner endpoints for the instrucitons.")
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -454,7 +460,7 @@ func TestRunMenuBrowse(t *testing.T) {
ctx := context.Background()
- rs.AddBytecode("foo", []byte{})
+ rs.AddBytecode(ctx, "foo", []byte{})
b := NewLine(nil, MOVE, []string{"foo"}, nil, nil)
b = NewLine(b, MOUT, []string{"one", "0"}, nil, nil)
b = NewLine(b, MOUT, []string{"two", "1"}, nil, nil)
@@ -481,7 +487,7 @@ func TestRunMenuBrowse(t *testing.T) {
func TestRunReturn(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -517,7 +523,7 @@ func TestRunReturn(t *testing.T) {
func TestRunLoadInput(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -547,7 +553,7 @@ func TestRunLoadInput(t *testing.T) {
func TestInputBranch(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -560,7 +566,7 @@ func TestInputBranch(t *testing.T) {
b = NewLine(b, CATCH, []string{"flagCatch"}, []byte{8}, []uint8{1})
b = NewLine(b, CATCH, []string{"one"}, []byte{9}, []uint8{1})
rs.RootCode = b
- rs.AddBytecode("root", rs.RootCode)
+ rs.AddBytecode(ctx, "root", rs.RootCode)
ctx := context.Background()
@@ -587,7 +593,7 @@ func TestInputBranch(t *testing.T) {
func TestInputIgnore(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -615,7 +621,7 @@ func TestInputIgnore(t *testing.T) {
func TestInputIgnoreWildcard(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -642,7 +648,7 @@ func TestInputIgnoreWildcard(t *testing.T) {
func TestCatchCleanMenu(t *testing.T) {
st := state.NewState(5)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -678,7 +684,7 @@ func TestCatchCleanMenu(t *testing.T) {
func TestSetLang(t *testing.T) {
st := state.NewState(0)
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -704,7 +710,7 @@ func TestSetLang(t *testing.T) {
func TestLoadError(t *testing.T) {
st := state.NewState(0)
st.UseDebug()
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)
@@ -737,7 +743,7 @@ func TestMatchFlag(t *testing.T) {
st := state.NewState(1)
st.UseDebug()
- rs := NewTestResource(&st)
+ rs := newTestResource(&st)
ca := cache.NewCache()
vm := NewVm(&st, &rs, ca, nil)