Remove duplicate rendertemplate code in vm test

This commit is contained in:
lash 2023-04-02 10:11:09 +01:00
parent 9e6ece0959
commit 71d7c1466e
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 109 additions and 45 deletions

49
go/dev/interactive.go Normal file
View File

@ -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())
}
}

12
go/engine/default.go Normal file
View File

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

View File

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

View File

@ -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 {

View File

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

View File

@ -1,11 +1,10 @@
package resource
import (
"context"
"testing"
)
func TestNewFs(t *testing.T) {
n := NewFsResource("./testdata", context.TODO())
n := NewFsResource("./testdata")
_ = n
}

24
go/resource/render.go Normal file
View File

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

View File

@ -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) {