go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 9a7c7d1aacaa47d29d2dd91d01d74680c82d0a0a
parent 4e98f1a54ce96c72c66d1f85b2f9d756ece6f298
Author: lash <dev@holbrook.no>
Date:   Fri, 21 Apr 2023 21:05:54 +0100

Implement language in fs template getter

Diffstat:
Mresource/fs.go | 62+++++++++++++++++++++++++++++++++++++++++---------------------
Mresource/fs_test.go | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
2 files changed, 98 insertions(+), 48 deletions(-)

diff --git a/resource/fs.go b/resource/fs.go @@ -2,8 +2,10 @@ package resource import ( "context" + "errors" "fmt" "io/ioutil" + "os" "path" "path/filepath" "strings" @@ -27,55 +29,73 @@ func NewFsResource(path string) (FsResource) { } } -func(fs FsResource) GetTemplate(sym string, ctx context.Context) (string, error) { - fp := path.Join(fs.Path, sym) - r, err := ioutil.ReadFile(fp) +func(fsr FsResource) GetTemplate(sym string, ctx context.Context) (string, error) { + v := ctx.Value("Language") + fp := path.Join(fsr.Path, sym) + fpl := fp + if v != nil { + lang := v.(lang.Language) + fpl += "_" + lang.Code + } + var r []byte + var err error + r, err = ioutil.ReadFile(fpl) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + if fpl != fp { + r, err = ioutil.ReadFile(fp) + if err != nil { + return "", err + } + } + } + } s := string(r) return strings.TrimSpace(s), err } -func(fs FsResource) GetCode(sym string) ([]byte, error) { +func(fsr FsResource) GetCode(sym string) ([]byte, error) { fb := sym + ".bin" - fp := path.Join(fs.Path, fb) + fp := path.Join(fsr.Path, fb) return ioutil.ReadFile(fp) } -func(fs *FsResource) AddLocalFunc(sym string, fn EntryFunc) { - if fs.fns == nil { - fs.fns = make(map[string]EntryFunc) +func(fsr *FsResource) AddLocalFunc(sym string, fn EntryFunc) { + if fsr.fns == nil { + fsr.fns = make(map[string]EntryFunc) } - fs.fns[sym] = fn + fsr.fns[sym] = fn } -func(fs FsResource) FuncFor(sym string) (EntryFunc, error) { - fn, ok := fs.fns[sym] +func(fsr FsResource) FuncFor(sym string) (EntryFunc, error) { + fn, ok := fsr.fns[sym] if ok { return fn, nil } - _, err := fs.getFuncNoCtx(sym, nil, nil) + _, err := fsr.getFuncNoCtx(sym, nil, nil) if err != nil { return nil, fmt.Errorf("unknown sym: %s", sym) } - return fs.getFunc, nil + return fsr.getFunc, nil } -func(fs FsResource) String() string { - return fmt.Sprintf("fs resource at path: %s", fs.Path) +func(fsr FsResource) String() string { + return fmt.Sprintf("fs resource at path: %s", fsr.Path) } -func(fs FsResource) getFunc(sym string, input []byte, ctx context.Context) (Result, error) { +func(fsr FsResource) getFunc(sym string, input []byte, ctx context.Context) (Result, error) { v := ctx.Value("language") if v == nil { - return fs.getFuncNoCtx(sym, input, nil) + return fsr.getFuncNoCtx(sym, input, nil) } language := v.(*lang.Language) - return fs.getFuncNoCtx(sym, input, language) + return fsr.getFuncNoCtx(sym, input, language) } -func(fs FsResource) getFuncNoCtx(sym string, input []byte, language *lang.Language) (Result, error) { +func(fsr FsResource) getFuncNoCtx(sym string, input []byte, language *lang.Language) (Result, error) { fb := sym + ".txt" - fp := path.Join(fs.Path, fb) - Logg.Debugf("getfunc search dir", "dir", fs.Path, "path", fp, "sym", sym, "language", language) + fp := path.Join(fsr.Path, fb) + Logg.Debugf("getfunc search dir", "dir", fsr.Path, "path", fp, "sym", sym, "language", language) r, err := ioutil.ReadFile(fp) if err != nil { return Result{}, fmt.Errorf("failed getting data for sym '%s': %v", sym, err) diff --git a/resource/fs_test.go b/resource/fs_test.go @@ -1,37 +1,67 @@ package resource import ( + "context" + "os" + "path" "testing" + + "git.defalsify.org/vise.git/lang" ) func TestNewFs(t *testing.T) { n := NewFsResource("./testdata") _ = n } -// -//func TestResourceLanguage(t *testing.T) { -// var err error -// generateTestData(t) -// ctx := context.TODO() -// st := state.NewState(0) -// rs := NewFsWrapper(dataDir, &st) -// ca := cache.NewCache() -// -// cfg := Config{ -// Root: "root", -// } -// -// en := NewEngine(cfg, &st, &rs, ca, ctx) -// _, err = en.Init(ctx) -// if err == nil { -// t.Fatalf("expected error") -// } -// cfg = Config{ -// Root: "root", -// } -// en = NewEngine(cfg, &st, &rs, ca, ctx) -// _, err = en.Init(ctx) -// if err != nil { -// t.Fatal(err) -// } -//} + +func TestResourceLanguage(t *testing.T) { + lang, err := lang.LanguageFromCode("nor") + if err != nil { + t.Fatal(err) + } + ctx := context.TODO() + + dir, err := os.MkdirTemp("", "vise_fsresource") + if err != nil { + t.Fatal(err) + } + + fp := path.Join(dir, "foo") + tmpl := "one two three" + err = os.WriteFile(fp, []byte(tmpl), 0600) + if err != nil { + t.Fatal(err) + } + + rs := NewFsResource(dir) + r, err := rs.GetTemplate("foo", ctx) + if err != nil { + t.Fatal(err) + } + if r != tmpl { + t.Fatalf("expected '%s', got '%s'", tmpl, r) + } + + ctx = context.WithValue(ctx, "Language", lang) + rs = NewFsResource(dir) + r, err = rs.GetTemplate("foo", ctx) + if err != nil { + t.Fatal(err) + } + if r != tmpl { + t.Fatalf("expected '%s', got '%s'", tmpl, r) + } + + tmpl = "en to tre" + err = os.WriteFile(fp + "_nor", []byte(tmpl), 0600) + if err != nil { + t.Fatal(err) + } + r, err = rs.GetTemplate("foo", ctx) + if err != nil { + t.Fatal(err) + } + if r != tmpl { + t.Fatalf("expected '%s', got '%s'", tmpl, r) + } +}