commit 71d7c1466e23057e3af569b1ded8fadcf0a96981
parent 9e6ece0959f4650df00b1d621906b1cf7e6b5b71
Author: lash <dev@holbrook.no>
Date: Sun, 2 Apr 2023 10:11:09 +0100
Remove duplicate rendertemplate code in vm test
Diffstat:
9 files changed, 109 insertions(+), 45 deletions(-)
diff --git a/go/dev/interactive.go b/go/dev/interactive.go
@@ -0,0 +1,49 @@
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "context"
+ "flag"
+ "fmt"
+ "strings"
+ "os"
+
+ "git.defalsify.org/festive/engine"
+)
+
+func main() {
+ var dir string
+ var root string
+ flag.StringVar(&dir, "d", ".", "resource dir to read from")
+ flag.StringVar(&root, "root", "root", "entry point symbol")
+ flag.Parse()
+ fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
+
+ ctx := context.Background()
+ en := engine.NewDefaultEngine(dir)
+ err := en.Init("root", ctx)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "cannot init: %v\n", err)
+ os.Exit(1)
+ }
+
+ running := true
+ for running {
+ reader := bufio.NewReader(os.Stdin)
+ in, err := reader.ReadString('\n')
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "cannot read input: %v", err)
+ os.Exit(1)
+ }
+ in = strings.TrimSpace(in)
+ err = en.Exec([]byte(in), ctx)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "execution terminated: %v", err)
+ os.Exit(1)
+ }
+ b := bytes.NewBuffer(nil)
+ en.WriteResult(b)
+ fmt.Println(b.String())
+ }
+}
diff --git a/go/dev/generate_testdata.go b/go/dev/testdata/generate_testdata.go
diff --git a/go/engine/default.go b/go/engine/default.go
@@ -0,0 +1,12 @@
+package engine
+
+import (
+ "git.defalsify.org/festive/state"
+ "git.defalsify.org/festive/resource"
+)
+
+func NewDefaultEngine(dir string) Engine {
+ st := state.NewState(0)
+ rs := resource.NewFsResource(dir)
+ return NewEngine(&st, &rs)
+}
diff --git a/go/engine/engine.go b/go/engine/engine.go
@@ -43,6 +43,9 @@ func(en *Engine) Init(sym string, ctx context.Context) error {
if err != nil {
return err
}
+ if len(code) == 0 {
+ return fmt.Errorf("no code found at resource %s", en.rs)
+ }
return en.st.AppendCode(code)
}
diff --git a/go/engine/engine_test.go b/go/engine/engine_test.go
@@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"path"
- "text/template"
"testing"
"git.defalsify.org/festive/resource"
@@ -24,8 +23,8 @@ type FsWrapper struct {
st *state.State
}
-func NewFsWrapper(path string, st *state.State, ctx context.Context) FsWrapper {
- rs := resource.NewFsResource(path, ctx)
+func NewFsWrapper(path string, st *state.State) FsWrapper {
+ rs := resource.NewFsResource(path)
return FsWrapper {
&rs,
st,
@@ -33,21 +32,7 @@ func NewFsWrapper(path string, st *state.State, ctx context.Context) FsWrapper {
}
func (r FsWrapper) RenderTemplate(sym string, values map[string]string) (string, error) {
- v, err := r.GetTemplate(sym)
- 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
+ return resource.DefaultRenderTemplate(r, sym, values)
}
func(fs FsWrapper) one(ctx context.Context) (string, error) {
@@ -84,7 +69,7 @@ func TestEngineInit(t *testing.T) {
st := state.NewState(17).WithCacheSize(1024)
generateTestData(t)
ctx := context.TODO()
- rs := NewFsWrapper(dataDir, &st, ctx)
+ rs := NewFsWrapper(dataDir, &st)
en := NewEngine(&st, &rs)
err := en.Init("root", ctx)
if err != nil {
diff --git a/go/resource/fs.go b/go/resource/fs.go
@@ -1,22 +1,24 @@
package resource
import (
- "context"
"fmt"
"io/ioutil"
"path"
+ "path/filepath"
"strings"
)
type FsResource struct {
Path string
- ctx context.Context
}
-func NewFsResource(path string, ctx context.Context) (FsResource) {
+func NewFsResource(path string) (FsResource) {
+ absPath, err := filepath.Abs(path)
+ if err != nil {
+ panic(err)
+ }
return FsResource{
- Path: path,
- ctx: ctx,
+ Path: absPath,
}
}
@@ -28,13 +30,19 @@ func(fs FsResource) GetTemplate(sym string) (string, error) {
}
func(fs FsResource) RenderTemplate(sym string, values map[string]string) (string, error) {
- return "", nil
+ return DefaultRenderTemplate(&fs, sym, values)
}
func(fs FsResource) GetCode(sym string) ([]byte, error) {
- return []byte{}, nil
+ fb := sym + ".bin"
+ fp := path.Join(fs.Path, fb)
+ return ioutil.ReadFile(fp)
}
func(fs FsResource) FuncFor(sym string) (EntryFunc, error) {
return nil, fmt.Errorf("not implemented")
}
+
+func(rs FsResource) String() string {
+ return fmt.Sprintf("fs resource at path: %s", rs.Path)
+}
diff --git a/go/resource/fs_test.go b/go/resource/fs_test.go
@@ -1,11 +1,10 @@
package resource
import (
- "context"
"testing"
)
func TestNewFs(t *testing.T) {
- n := NewFsResource("./testdata", context.TODO())
+ n := NewFsResource("./testdata")
_ = n
}
diff --git a/go/resource/render.go b/go/resource/render.go
@@ -0,0 +1,24 @@
+package resource
+
+import (
+ "bytes"
+ "text/template"
+)
+
+func DefaultRenderTemplate(r Resource, sym string, values map[string]string) (string, error) {
+ v, err := r.GetTemplate(sym)
+ 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/vm/vm_test.go b/go/vm/vm_test.go
@@ -6,10 +6,8 @@ import (
"fmt"
"log"
"testing"
- "text/template"
"git.defalsify.org/festive/resource"
-// "git.defalsify.org/festive/router"
"git.defalsify.org/festive/state"
)
@@ -53,21 +51,7 @@ func (r *TestResource) GetTemplate(sym string) (string, error) {
}
func (r *TestResource) RenderTemplate(sym string, values map[string]string) (string, error) {
- v, err := r.GetTemplate(sym)
- 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
+ return resource.DefaultRenderTemplate(r, sym, values)
}
func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) {