commit 4e98f1a54ce96c72c66d1f85b2f9d756ece6f298
parent 218086c8a3ca500940df4023ce8aabf5e331c535
Author: lash <dev@holbrook.no>
Date: Fri, 21 Apr 2023 20:34:46 +0100
Pass context to render methods
Diffstat:
7 files changed, 32 insertions(+), 25 deletions(-)
diff --git a/render/page.go b/render/page.go
@@ -2,6 +2,7 @@ package render
import (
"bytes"
+ "context"
"fmt"
"strings"
"text/template"
@@ -129,8 +130,8 @@ func(pg *Page) Sizes() (map[string]uint16, error) {
}
// RenderTemplate is an adapter to implement the builtin golang text template renderer as resource.RenderTemplate.
-func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16) (string, error) {
- tpl, err := pg.resource.GetTemplate(sym)
+func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16, ctx context.Context) (string, error) {
+ tpl, err := pg.resource.GetTemplate(sym, ctx)
if err != nil {
return "", err
}
@@ -159,15 +160,15 @@ func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16)
}
// Render renders the current mapped content and menu state against the template associated with the symbol.
-func(pg *Page) Render(sym string, idx uint16) (string, error) {
+func(pg *Page) Render(sym string, idx uint16, ctx context.Context) (string, error) {
var err error
- values, err := pg.prepare(sym, pg.cacheMap, idx)
+ values, err := pg.prepare(sym, pg.cacheMap, idx, ctx)
if err != nil {
return "", err
}
- return pg.render(sym, values, idx)
+ return pg.render(sym, values, idx, ctx)
}
// Reset prepared the Page object for re-use.
@@ -184,7 +185,7 @@ func(pg *Page) Reset() {
// render menu and all syms except sink, split sink into display chunks
// TODO: Function too long, split up
-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, ctx context.Context) (map[string]string, error) {
var sink string
if pg.sizer == nil {
@@ -213,7 +214,7 @@ func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[st
}
pg.sizer.AddCursor(0)
- s, err := pg.render(sym, noSinkValues, 0)
+ s, err := pg.render(sym, noSinkValues, 0, ctx)
if err != nil {
return nil, err
}
@@ -305,10 +306,10 @@ func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[st
}
// render template, menu (if it exists), and audit size constraint (if it exists).
-func(pg *Page) render(sym string, values map[string]string, idx uint16) (string, error) {
+func(pg *Page) render(sym string, values map[string]string, idx uint16, ctx context.Context) (string, error) {
var ok bool
r := ""
- s, err := pg.RenderTemplate(sym, values, idx)
+ s, err := pg.RenderTemplate(sym, values, idx, ctx)
if err != nil {
return "", err
}
diff --git a/render/size_test.go b/render/size_test.go
@@ -14,7 +14,7 @@ type TestSizeResource struct {
*resource.MenuResource
}
-func getTemplate(sym string) (string, error) {
+func getTemplate(sym string, ctx context.Context) (string, error) {
var tpl string
switch sym {
case "small":
@@ -124,12 +124,13 @@ func TestSizeLimit(t *testing.T) {
mn.Put("1", "foo the foo")
mn.Put("2", "go to bar")
- _, err = pg.Render("small", 0)
+ ctx := context.TODO()
+ _, err = pg.Render("small", 0, ctx)
if err != nil {
t.Fatal(err)
}
- _, err = pg.Render("toobig", 0)
+ _, err = pg.Render("toobig", 0, ctx)
if err == nil {
t.Fatalf("expected size exceeded")
}
@@ -159,7 +160,8 @@ func TestSizePages(t *testing.T) {
mn.Put("1", "foo the foo")
mn.Put("2", "go to bar")
- r, err := pg.Render("pages", 0)
+ ctx := context.TODO()
+ r, err := pg.Render("pages", 0, ctx)
if err != nil {
t.Fatal(err)
}
@@ -176,7 +178,7 @@ lala poo
if r != expect {
t.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, r)
}
- r, err = pg.Render("pages", 1)
+ r, err = pg.Render("pages", 1, ctx)
if err != nil {
t.Fatal(err)
}
@@ -214,7 +216,9 @@ func TestManySizes(t *testing.T) {
pg.Map("bar")
pg.Map("baz")
pg.Map("xyzzy")
- _, err := pg.Render("pages", 0)
+
+ ctx := context.TODO()
+ _, err := pg.Render("pages", 0, ctx)
if err != nil {
t.Fatal(err)
}
@@ -244,7 +248,9 @@ func TestManySizesMenued(t *testing.T) {
pg.Map("xyzzy")
mn.Put("0", "yay")
mn.Put("12", "nay")
- _, err := pg.Render("pages", 0)
+
+ ctx := context.TODO()
+ _, err := pg.Render("pages", 0, ctx)
if err != nil {
t.Fatal(err)
}
diff --git a/resource/fs.go b/resource/fs.go
@@ -27,7 +27,7 @@ func NewFsResource(path string) (FsResource) {
}
}
-func(fs FsResource) GetTemplate(sym string) (string, error) {
+func(fs FsResource) GetTemplate(sym string, ctx context.Context) (string, error) {
fp := path.Join(fs.Path, sym)
r, err := ioutil.ReadFile(fp)
s := string(r)
diff --git a/resource/resource.go b/resource/resource.go
@@ -14,12 +14,12 @@ type Result struct {
// EntryFunc is a function signature for retrieving value for a key
type EntryFunc func(sym string, input []byte, ctx context.Context) (Result, error)
type CodeFunc func(sym string) ([]byte, error)
-type TemplateFunc func(sym string) (string, error)
+type TemplateFunc func(sym string, ctx context.Context) (string, error)
type FuncForFunc func(sym string) (EntryFunc, error)
// Resource implementation are responsible for retrieving values and templates for symbols, and can render templates from value dictionaries.
type Resource interface {
- GetTemplate(sym string) (string, error) // Get the template for a given symbol.
+ GetTemplate(sym string, ctx context.Context) (string, error) // Get the template for a given symbol.
GetCode(sym string) ([]byte, error) // Get the bytecode for the given symbol.
FuncFor(sym string) (EntryFunc, error) // Resolve symbol content point for.
}
@@ -68,6 +68,6 @@ func(m *MenuResource) GetCode(sym string) ([]byte, error) {
}
// GetTemplate implements Resource interface
-func(m *MenuResource) GetTemplate(sym string) (string, error) {
- return m.templateFunc(sym)
+func(m *MenuResource) GetTemplate(sym string, ctx context.Context) (string, error) {
+ return m.templateFunc(sym, ctx)
}
diff --git a/resource/resource_test.go b/resource/resource_test.go
@@ -10,7 +10,7 @@ type TestSizeResource struct {
*MenuResource
}
-func getTemplate(sym string) (string, error) {
+func getTemplate(sym string, ctx context.Context) (string, error) {
var tpl string
switch sym {
case "small":
diff --git a/vm/runner.go b/vm/runner.go
@@ -451,7 +451,7 @@ func(vm *Vm) Render(ctx context.Context) (string, error) {
return "", nil
}
sym, idx := vm.st.Where()
- r, err := vm.pg.Render(sym, idx)
+ r, err := vm.pg.Render(sym, idx, ctx)
var ok bool
_, ok = err.(*render.BrowseError)
if ok {
@@ -459,7 +459,7 @@ func(vm *Vm) Render(ctx context.Context) (string, error) {
b := NewLine(nil, MOVE, []string{"_catch"}, nil, nil)
vm.Run(b, ctx)
sym, idx := vm.st.Where()
- r, err = vm.pg.Render(sym, idx)
+ r, err = vm.pg.Render(sym, idx, ctx)
}
if err != nil {
return "", err
diff --git a/vm/runner_test.go b/vm/runner_test.go
@@ -73,7 +73,7 @@ type TestStatefulResolver struct {
state *state.State
}
-func (r TestResource) GetTemplate(sym string) (string, error) {
+func (r TestResource) GetTemplate(sym string, ctx context.Context) (string, error) {
switch sym {
case "foo":
return "inky pinky blinky clyde", nil