Add dynamic file reader alternative for funcfor in fs resource

This commit is contained in:
lash 2023-04-10 05:30:57 +01:00
parent 8ddfd68f33
commit 084b23babd
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 85 additions and 75 deletions

View File

@ -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 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 return "tinkywinky", nil
} }

View File

@ -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 // 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) { func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[string]string, error) {
var sink string var sink string
if pg.sizer == nil {
return values, nil
}
var sinkValues []string var sinkValues []string
noSinkValues := make(map[string]string) noSinkValues := make(map[string]string)
for k, v := range values { for k, v := range values {

View File

@ -41,19 +41,19 @@ func funcFor(sym string) (resource.EntryFunc, error) {
return nil, fmt.Errorf("unknown func: %s", sym) 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 return "inky", nil
} }
func getBar(ctx context.Context) (string, error) { func getBar(sym string, ctx context.Context) (string, error) {
return "pinky", nil return "pinky", nil
} }
func getBaz(ctx context.Context) (string, error) { func getBaz(sym string, ctx context.Context) (string, error) {
return "blinky", nil 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 return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil
} }

View File

@ -1,6 +1,7 @@
package resource package resource
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path" "path"
@ -11,6 +12,7 @@ import (
type FsResource struct { type FsResource struct {
MenuResource MenuResource
Path string Path string
fns map[string]EntryFunc
} }
func NewFsResource(path string) (FsResource) { 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) { 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(rs FsResource) String() string { func(fs FsResource) String() string {
return fmt.Sprintf("fs resource at path: %s", rs.Path) 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(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
} }

View File

@ -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
//}

View File

@ -5,7 +5,7 @@ import (
) )
// EntryFunc is a function signature for retrieving value for a key // 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 CodeFunc func(sym string) ([]byte, error)
type TemplateFunc func(sym string) (string, error) type TemplateFunc func(sym string) (string, error)
type FuncForFunc func(sym string) (EntryFunc, error) type FuncForFunc func(sym string) (EntryFunc, error)

View File

@ -26,29 +26,29 @@ func getTemplate(sym string) (string, error) {
func funcFor(sym string) (EntryFunc, error) { func funcFor(sym string) (EntryFunc, error) {
switch sym { switch sym {
case "foo": case "foo":
return getFoo, nil return get, nil
case "bar": case "bar":
return getBar, nil return get, nil
case "baz": case "baz":
return getBaz, nil return get, nil
case "xyzzy": case "xyzzy":
return getXyzzy, nil return getXyzzy, nil
} }
return nil, fmt.Errorf("unknown func: %s", sym) return nil, fmt.Errorf("unknown func: %s", sym)
} }
func getFoo(ctx context.Context) (string, error) { func get(sym string, ctx context.Context) (string, error) {
return "inky", nil 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 getBar(ctx context.Context) (string, error) { func getXyzzy(sym string, ctx context.Context) (string, error) {
return "pinky", nil
}
func getBaz(ctx context.Context) (string, error) {
return "blinky", nil
}
func getXyzzy(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 return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil
} }

View File

@ -19,7 +19,7 @@ var (
dirLock = false 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) fp := path.Join(DataDir, sym)
err := ioutil.WriteFile(fp, []byte(tpl), 0644) err := ioutil.WriteFile(fp, []byte(tpl), 0644)
if err != nil { if err != nil {
@ -32,6 +32,20 @@ func out(sym string, b []byte, tpl string) error {
if err != nil { if err != nil {
return err 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 return nil
} }
@ -45,7 +59,7 @@ func root() error {
tpl := "hello world" tpl := "hello world"
return out("root", b, tpl) return out("root", b, tpl, nil)
} }
func foo() error { 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.INCMP, []string{"1", "baz"}, nil, nil)
b = vm.NewLine(b, vm.CATCH, []string{"_catch"}, []byte{1}, []uint8{1}) 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 tpl := `this is in foo
it has more lines` it has more lines`
return out("foo", b, tpl) return out("foo", b, tpl, data)
} }
func bar() error { func bar() error {
@ -73,7 +90,10 @@ func bar() error {
tpl := "this is bar - an end node" 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 { func baz() error {
@ -83,7 +103,7 @@ func baz() error {
tpl := "this is baz which uses the var {{.inky}} in the template." 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 { func defaultCatch() error {
@ -94,7 +114,7 @@ func defaultCatch() error {
tpl := "invalid input" tpl := "invalid input"
return out("_catch", b, tpl) return out("_catch", b, tpl, nil)
} }
func generate() error { func generate() error {

View File

@ -345,6 +345,6 @@ func refresh(key string, rs resource.Resource, ctx context.Context) (string, err
if fn == nil { if fn == nil {
return "", fmt.Errorf("no retrieve function for external symbol %v", key) return "", fmt.Errorf("no retrieve function for external symbol %v", key)
} }
return fn(ctx) return fn(key, ctx)
} }

View File

@ -20,15 +20,15 @@ type TestResource struct {
state *state.State state *state.State
} }
func getOne(ctx context.Context) (string, error) { func getOne(sym string, ctx context.Context) (string, error) {
return "one", nil return "one", nil
} }
func getTwo(ctx context.Context) (string, error) { func getTwo(sym string, ctx context.Context) (string, error) {
return "two", nil return "two", nil
} }
func getDyn(ctx context.Context) (string, error) { func getDyn(sym string, ctx context.Context) (string, error) {
return dynVal, nil return dynVal, nil
} }
@ -69,7 +69,7 @@ func (r TestResource) FuncFor(sym string) (resource.EntryFunc, error) {
return nil, fmt.Errorf("invalid function: '%s'", sym) 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() v, err := r.state.GetInput()
return string(v), err return string(v), err
} }