Make client input available for LOAD instruction execution

This commit is contained in:
lash 2023-04-13 07:56:35 +01:00
parent a2d947e106
commit 46288b240b
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
7 changed files with 24 additions and 23 deletions

View File

@ -32,16 +32,16 @@ func NewFsWrapper(path string, st *state.State) FsWrapper {
} }
} }
func(fs FsWrapper) one(sym string, ctx context.Context) (string, error) { func(fs FsWrapper) one(sym string, input []byte, ctx context.Context) (string, error) {
return "one", nil return "one", nil
} }
func(fs FsWrapper) inky(sym string, ctx context.Context) (string, error) { func(fs FsWrapper) inky(sym string, input []byte, ctx context.Context) (string, error) {
return "tinkywinky", nil return "tinkywinky", nil
} }
func(fs FsWrapper) pinky(sym string, ctx context.Context) (string, error) { func(fs FsWrapper) pinky(sym string, input []byte, ctx context.Context) (string, error) {
return "xyzzy", nil return fmt.Sprintf("xyzzy: %x", input), nil
} }
func(fs FsWrapper) FuncFor(sym string) (resource.EntryFunc, error) { func(fs FsWrapper) FuncFor(sym string) (resource.EntryFunc, error) {

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(sym string, ctx context.Context) (string, error) { func getFoo(sym string, input []byte, ctx context.Context) (string, error) {
return "inky", nil return "inky", nil
} }
func getBar(sym string, ctx context.Context) (string, error) { func getBar(sym string, input []byte, ctx context.Context) (string, error) {
return "pinky", nil return "pinky", nil
} }
func getBaz(sym string, ctx context.Context) (string, error) { func getBaz(sym string, input []byte, ctx context.Context) (string, error) {
return "blinky", nil return "blinky", nil
} }
func getXyzzy(sym string, ctx context.Context) (string, error) { func getXyzzy(sym string, input []byte, 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

@ -44,7 +44,7 @@ func(fs FsResource) FuncFor(sym string) (EntryFunc, error) {
if ok { if ok {
return fn, nil return fn, nil
} }
_, err := fs.getFuncNoCtx(sym) _, err := fs.getFuncNoCtx(sym, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("unknown sym: %s", sym) return nil, fmt.Errorf("unknown sym: %s", sym)
} }
@ -55,11 +55,11 @@ func(fs FsResource) String() string {
return fmt.Sprintf("fs resource at path: %s", fs.Path) return fmt.Sprintf("fs resource at path: %s", fs.Path)
} }
func(fs FsResource) getFunc(sym string, ctx context.Context) (string, error) { func(fs FsResource) getFunc(sym string, input []byte, ctx context.Context) (string, error) {
return fs.getFuncNoCtx(sym) return fs.getFuncNoCtx(sym, input)
} }
func(fs FsResource) getFuncNoCtx(sym string) (string, error) { func(fs FsResource) getFuncNoCtx(sym string, input []byte) (string, error) {
fb := sym + ".txt" fb := sym + ".txt"
fp := path.Join(fs.Path, fb) fp := path.Join(fs.Path, fb)
log.Printf("getfunc search dir %s %s for %s", fs.Path, fp, sym) log.Printf("getfunc search dir %s %s for %s", fs.Path, fp, sym)

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(sym string, ctx context.Context) (string, error) type EntryFunc func(sym string, input []byte, 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

@ -37,7 +37,7 @@ func funcFor(sym string) (EntryFunc, error) {
return nil, fmt.Errorf("unknown func: %s", sym) return nil, fmt.Errorf("unknown func: %s", sym)
} }
func get(sym string, ctx context.Context) (string, error) { func get(sym string, input []byte, ctx context.Context) (string, error) {
switch sym { switch sym {
case "foo": case "foo":
return "inky", nil return "inky", nil
@ -49,6 +49,6 @@ func get(sym string, ctx context.Context) (string, error) {
return "", fmt.Errorf("unknown sym: %s", sym) return "", fmt.Errorf("unknown sym: %s", sym)
} }
func getXyzzy(sym string, ctx context.Context) (string, error) { func getXyzzy(sym string, input []byte, 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

@ -211,7 +211,7 @@ func(vm *Vm) RunLoad(b []byte, ctx context.Context) ([]byte, error) {
if err != nil { if err != nil {
return b, err return b, err
} }
r, err := refresh(sym, vm.rs, ctx) r, err := vm.refresh(sym, vm.rs, ctx)
if err != nil { if err != nil {
return b, err return b, err
} }
@ -226,7 +226,7 @@ func(vm *Vm) RunReload(b []byte, ctx context.Context) ([]byte, error) {
return b, err return b, err
} }
r, err := refresh(sym, vm.rs, ctx) r, err := vm.refresh(sym, vm.rs, ctx)
if err != nil { if err != nil {
return b, err return b, err
} }
@ -421,7 +421,7 @@ func(vm *Vm) Render(ctx context.Context) (string, error) {
} }
// retrieve data for key // retrieve data for key
func refresh(key string, rs resource.Resource, ctx context.Context) (string, error) { func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (string, error) {
fn, err := rs.FuncFor(key) fn, err := rs.FuncFor(key)
if err != nil { if err != nil {
return "", err return "", err
@ -429,6 +429,7 @@ 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(key, ctx) input, _ := vm.st.GetInput()
return fn(key, input, ctx)
} }

View File

@ -20,15 +20,15 @@ type TestResource struct {
state *state.State state *state.State
} }
func getOne(sym string, ctx context.Context) (string, error) { func getOne(sym string, input []byte, ctx context.Context) (string, error) {
return "one", nil return "one", nil
} }
func getTwo(sym string, ctx context.Context) (string, error) { func getTwo(sym string, input []byte, ctx context.Context) (string, error) {
return "two", nil return "two", nil
} }
func getDyn(sym string, ctx context.Context) (string, error) { func getDyn(sym string, input []byte, 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(sym string, ctx context.Context) (string, error) { func(r TestResource) getInput(sym string, input []byte, ctx context.Context) (string, error) {
v, err := r.state.GetInput() v, err := r.state.GetInput()
return string(v), err return string(v), err
} }