go-vise

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

commit 084b23babd4a7a131e601dee7813525946717aa1
parent 8ddfd68f3372d2d7723404692f9354d894f8b55c
Author: lash <dev@holbrook.no>
Date:   Mon, 10 Apr 2023 05:30:57 +0100

Add dynamic file reader alternative for funcfor in fs resource

Diffstat:
Mgo/engine/engine_test.go | 4++--
Mgo/render/page.go | 5+++++
Mgo/render/size_test.go | 8++++----
Mgo/resource/fs.go | 31++++++++++++++++++++++++++++---
Dgo/resource/render.go | 40----------------------------------------
Mgo/resource/resource.go | 2+-
Mgo/resource/resource_test.go | 28++++++++++++++--------------
Mgo/testdata/testdata.go | 32++++++++++++++++++++++++++------
Mgo/vm/runner.go | 2+-
Mgo/vm/runner_test.go | 8++++----
10 files changed, 85 insertions(+), 75 deletions(-)

diff --git a/go/engine/engine_test.go b/go/engine/engine_test.go @@ -32,11 +32,11 @@ func NewFsWrapper(path string, st *state.State) FsWrapper { } } -func(fs FsWrapper) one(ctx context.Context) (string, error) { +func(fs FsWrapper) one(sym string, ctx context.Context) (string, error) { return "one", nil } -func(fs FsWrapper) inky(ctx context.Context) (string, error) { +func(fs FsWrapper) inky(sym string, ctx context.Context) (string, error) { return "tinkywinky", nil } diff --git a/go/render/page.go b/go/render/page.go @@ -156,6 +156,11 @@ func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16) // render menu and all syms except sink, split sink into display chunks func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[string]string, error) { var sink string + + if pg.sizer == nil { + return values, nil + } + var sinkValues []string noSinkValues := make(map[string]string) for k, v := range values { diff --git a/go/render/size_test.go b/go/render/size_test.go @@ -41,19 +41,19 @@ func funcFor(sym string) (resource.EntryFunc, error) { return nil, fmt.Errorf("unknown func: %s", sym) } -func getFoo(ctx context.Context) (string, error) { +func getFoo(sym string, ctx context.Context) (string, error) { return "inky", nil } -func getBar(ctx context.Context) (string, error) { +func getBar(sym string, ctx context.Context) (string, error) { return "pinky", nil } -func getBaz(ctx context.Context) (string, error) { +func getBaz(sym string, ctx context.Context) (string, error) { return "blinky", nil } -func getXyzzy(ctx context.Context) (string, error) { +func getXyzzy(sym string, ctx context.Context) (string, error) { return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil } diff --git a/go/resource/fs.go b/go/resource/fs.go @@ -1,6 +1,7 @@ package resource import ( + "context" "fmt" "io/ioutil" "path" @@ -11,6 +12,7 @@ import ( type FsResource struct { MenuResource Path string + fns map[string]EntryFunc } func NewFsResource(path string) (FsResource) { @@ -37,9 +39,32 @@ func(fs FsResource) GetCode(sym string) ([]byte, error) { } func(fs FsResource) FuncFor(sym string) (EntryFunc, error) { - return nil, fmt.Errorf("not implemented") + fn, ok := fs.fns[sym] + if ok { + return fn, nil + } + _, err := fs.getFuncNoCtx(sym) + if err != nil { + return nil, fmt.Errorf("unknown sym: %s", sym) + } + return fs.getFunc, nil +} + +func(fs FsResource) String() string { + return fmt.Sprintf("fs resource at path: %s", fs.Path) +} + +func(fs FsResource) getFunc(sym string, ctx context.Context) (string, error) { + return fs.getFuncNoCtx(sym) } -func(rs FsResource) String() string { - return fmt.Sprintf("fs resource at path: %s", rs.Path) +func(fs FsResource) getFuncNoCtx(sym string) (string, error) { + fb := sym + ".txt" + fp := path.Join(fs.Path, fb) + r, err := ioutil.ReadFile(fp) + if err != nil { + return "", fmt.Errorf("failed getting data for sym '%s': %v", sym, err) + } + s := string(r) + return strings.TrimSpace(s), err } diff --git a/go/resource/render.go b/go/resource/render.go @@ -1,40 +0,0 @@ -package resource - -//import ( -// "bytes" -// "fmt" -// "log" -// "text/template" -//) - - -//// DefaultRenderTemplate is an adapter to implement the builtin golang text template renderer as resource.RenderTemplate. -//func DefaultRenderTemplate(r Resource, sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) { -// v, err := r.GetTemplate(sym, nil) -// if err != nil { -// return "", err -// } -// -// if sizer != nil { -// values, err = sizer.GetAt(values, idx) -// } else if idx > 0 { -// return "", fmt.Errorf("sizer needed for indexed render") -// } -// log.Printf("render for index: %v", idx) -// -// if err != nil { -// return "", err -// } -// -// tp, err := template.New("tester").Option("missingkey=error").Parse(v) -// if err != nil { -// return "", err -// } -// -// b := bytes.NewBuffer([]byte{}) -// err = tp.Execute(b, values) -// if err != nil { -// return "", err -// } -// return b.String(), err -//} diff --git a/go/resource/resource.go b/go/resource/resource.go @@ -5,7 +5,7 @@ import ( ) // EntryFunc is a function signature for retrieving value for a key -type EntryFunc func(ctx context.Context) (string, error) +type EntryFunc func(sym string, ctx context.Context) (string, error) type CodeFunc func(sym string) ([]byte, error) type TemplateFunc func(sym string) (string, error) type FuncForFunc func(sym string) (EntryFunc, error) diff --git a/go/resource/resource_test.go b/go/resource/resource_test.go @@ -26,29 +26,29 @@ func getTemplate(sym string) (string, error) { func funcFor(sym string) (EntryFunc, error) { switch sym { case "foo": - return getFoo, nil + return get, nil case "bar": - return getBar, nil + return get, nil case "baz": - return getBaz, nil + return get, nil case "xyzzy": return getXyzzy, nil } return nil, fmt.Errorf("unknown func: %s", sym) } -func getFoo(ctx context.Context) (string, error) { - return "inky", nil -} - -func getBar(ctx context.Context) (string, error) { - return "pinky", nil -} - -func getBaz(ctx context.Context) (string, error) { - return "blinky", nil +func get(sym string, ctx context.Context) (string, error) { + switch sym { + case "foo": + return "inky", nil + case "bar": + return "pinky", nil + case "baz": + return "blinky", nil + } + return "", fmt.Errorf("unknown sym: %s", sym) } -func getXyzzy(ctx context.Context) (string, error) { +func getXyzzy(sym string, ctx context.Context) (string, error) { return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil } diff --git a/go/testdata/testdata.go b/go/testdata/testdata.go @@ -19,7 +19,7 @@ var ( dirLock = false ) -func out(sym string, b []byte, tpl string) error { +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) if err != nil { @@ -32,6 +32,20 @@ func out(sym string, b []byte, tpl string) error { if err != nil { return err } + + if data == nil { + return nil + } + + for k, v := range data { + fb := k + ".txt" + fp = path.Join(DataDir, fb) + err = ioutil.WriteFile(fp, []byte(v), 0644) + if err != nil { + return err + } + } + return nil } @@ -45,7 +59,7 @@ func root() error { tpl := "hello world" - return out("root", b, tpl) + return out("root", b, tpl, nil) } func foo() error { @@ -58,11 +72,14 @@ func foo() error { b = vm.NewLine(b, vm.INCMP, []string{"1", "baz"}, nil, nil) b = vm.NewLine(b, vm.CATCH, []string{"_catch"}, []byte{1}, []uint8{1}) + data := make(map[string]string) + data["inky"] = "one" + tpl := `this is in foo it has more lines` - return out("foo", b, tpl) + return out("foo", b, tpl, data) } func bar() error { @@ -73,7 +90,10 @@ func bar() error { tpl := "this is bar - an end node" - return out("bar", b, tpl) + data := make(map[string]string) + data["pinky"] = "two" + + return out("bar", b, tpl, data) } func baz() error { @@ -83,7 +103,7 @@ func baz() error { tpl := "this is baz which uses the var {{.inky}} in the template." - return out("baz", b, tpl) + return out("baz", b, tpl, nil) } func defaultCatch() error { @@ -94,7 +114,7 @@ func defaultCatch() error { tpl := "invalid input" - return out("_catch", b, tpl) + return out("_catch", b, tpl, nil) } func generate() error { diff --git a/go/vm/runner.go b/go/vm/runner.go @@ -345,6 +345,6 @@ func refresh(key string, rs resource.Resource, ctx context.Context) (string, err if fn == nil { return "", fmt.Errorf("no retrieve function for external symbol %v", key) } - return fn(ctx) + return fn(key, ctx) } diff --git a/go/vm/runner_test.go b/go/vm/runner_test.go @@ -20,15 +20,15 @@ type TestResource struct { state *state.State } -func getOne(ctx context.Context) (string, error) { +func getOne(sym string, ctx context.Context) (string, error) { return "one", nil } -func getTwo(ctx context.Context) (string, error) { +func getTwo(sym string, ctx context.Context) (string, error) { return "two", nil } -func getDyn(ctx context.Context) (string, error) { +func getDyn(sym string, ctx context.Context) (string, error) { return dynVal, nil } @@ -69,7 +69,7 @@ func (r TestResource) FuncFor(sym string) (resource.EntryFunc, error) { return nil, fmt.Errorf("invalid function: '%s'", sym) } -func(r TestResource) getInput(ctx context.Context) (string, error) { +func(r TestResource) getInput(sym string, ctx context.Context) (string, error) { v, err := r.state.GetInput() return string(v), err }