dati

data and template interface
git clone git://git.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit f958e1e299b225cedf281d263e380888ead822f4
parent e1d08e3b4a9f7b6fc96860c7cbacb06e153031b0
Author: gearsix <gearsix@tuta.io>
Date:   Tue,  5 Oct 2021 18:47:01 +0100

suti.Template.Source -> suti.Template.Name; updated cmd/suti_test.sh

The rest is just refactor updates for prev. commits (*File(..) ->
*Filepath(..)).

Diffstat:
Mcmd/suti.go | 6+++---
Mcmd/suti_test.sh | 9+++++----
Mdata.go | 2+-
Mdata_test.go | 10+++++-----
Mtemplate.go | 20+++++++++++---------
Mtemplate_test.go | 10+++++-----
6 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/cmd/suti.go b/cmd/suti.go @@ -96,7 +96,7 @@ func main() { opts.GlobalDataPaths = loadFilePaths(opts.GlobalDataPaths...) for _, path := range opts.GlobalDataPaths { var d Data - err = suti.LoadDataFile(path, &d) + err = suti.LoadDataFilepath(path, &d) assert(err, "failed to load global data '%s'", path) data = append(data, d) } @@ -110,13 +110,13 @@ func main() { data = make([]Data, 0) for _, path := range opts.DataPaths { var d Data - err = suti.LoadDataFile(path, &d) + err = suti.LoadDataFilepath(path, &d) assert(err, "failed to load data '%s'", path) data = append(data, d) } global[opts.DataKey] = data - template, err = suti.LoadTemplateFile(opts.RootPath, opts.PartialPaths...) + template, err = suti.LoadTemplateFilepath(opts.RootPath, opts.PartialPaths...) assert(err, "unable to load templates") out, err = template.Execute(global) diff --git a/cmd/suti_test.sh b/cmd/suti_test.sh @@ -1,20 +1,21 @@ -#!/bin/bash +#!/bin/sh # if nothing prints, the test passed diff="diff -bs" +fail=0 go build -o suti suti.go if [ -e suti ]; then ./suti -cfg ../examples/suti.cfg -r ../examples/template/html.hmpl > out.html $diff out.html ../examples/out.html - rm out.html + if [ $? -ne 0 ]; then fail=1; else rm out.html; fi ./suti -cfg ../examples/suti.cfg -r ../examples/template/txt.mst > out.txt $diff out.txt ../examples/out.txt - rm out.txt + if [ $? -ne 0 ]; then fail=1; else rm out.txt; fi rm suti - echo "if files are identical, TEST PASS" + if [ $fail -eq 1 ]; then echo "TEST FAIL"; else echo "TEST PASS"; fi fi diff --git a/data.go b/data.go @@ -77,7 +77,7 @@ func LoadData(lang string, in io.Reader, outp interface{}) error { // LoadDataFile loads all the data from the file found at `path` into the the // format of that files extension (e.g. "x.json" will be loaded as a json). // The result is written to the value pointed at by `outp`. -func LoadDataFile(path string, outp interface{}) error { +func LoadDataFilepath(path string, outp interface{}) error { f, e := os.Open(path) defer f.Close() diff --git a/data_test.go b/data_test.go @@ -126,7 +126,7 @@ func TestLoadData(t *testing.T) { return } -func TestLoadDataFile(t *testing.T) { +func TestLoadDataFilepath(t *testing.T) { var d interface{} var e error var p string @@ -135,25 +135,25 @@ func TestLoadDataFile(t *testing.T) { for lang, data := range good { p = tdir + "/good." + lang writeTestFile(t, p, data) - e = LoadDataFile(p, &d) + e = LoadDataFilepath(p, &d) validateData(t, d, e, lang) } p = tdir + "/bad.json" writeTestFile(t, p, badData) - e = LoadDataFile(p, &d) + e = LoadDataFilepath(p, &d) if e == nil { t.Fatalf("bad data passed") } p = tdir + "/empty.json" writeTestFile(t, p, "") - e = LoadDataFile(p, &d) + e = LoadDataFilepath(p, &d) if e != nil { t.Fatalf("empty file failed: %s", e) } - if e = LoadDataFile("non-existing-file.toml", &d); e == nil { + if e = LoadDataFilepath("non-existing-file.toml", &d); e == nil { t.Fatalf("non-existing file passed: %s, %s", d, e) } diff --git a/template.go b/template.go @@ -53,10 +53,12 @@ func getTemplateType(path string) string { return strings.TrimPrefix(filepath.Ext(path), ".") } -// Template is a generic interface to any template parsed from LoadTemplateFile +// Template is a wrapper to interface with any template parsed by suti. +// Ideally it would have just been an interface{} that defines Execute but +// the libaries being used aren't that uniform. type Template struct { - Source string - Template interface{} + Name string + T interface{} } // Execute executes `t` against `d`. Reflection is used to determine @@ -64,7 +66,7 @@ type Template struct { func (t *Template) Execute(d interface{}) (result bytes.Buffer, err error) { var funcName string var params []reflect.Value - switch (reflect.TypeOf(t.Template).String()) { + switch (reflect.TypeOf(t.T).String()) { case "*template.Template": // golang templates funcName = "Execute" params = []reflect.Value{reflect.ValueOf(&result), reflect.ValueOf(d)} @@ -72,11 +74,11 @@ func (t *Template) Execute(d interface{}) (result bytes.Buffer, err error) { funcName = "FRender" params = []reflect.Value{reflect.ValueOf(&result), reflect.ValueOf(d)} default: - err = fmt.Errorf("unable to infer template type '%s'", reflect.TypeOf(t.Template).String()) + err = fmt.Errorf("unable to infer template type '%s'", reflect.TypeOf(t.T).String()) } if err == nil { - rval := reflect.ValueOf(t.Template).MethodByName(funcName).Call(params) + rval := reflect.ValueOf(t.T).MethodByName(funcName).Call(params) if !rval[0].IsNil() { // err != nil err = rval[0].Interface().(error) } @@ -175,7 +177,7 @@ func LoadTemplate(lang string, rootName string, root io.Reader, partials map[str break } } - t.Template = template + t.T = template case "hmpl": var template *hmpl.Template if buf, e = io.ReadAll(root); e != nil { @@ -192,7 +194,7 @@ func LoadTemplate(lang string, rootName string, root io.Reader, partials map[str break } } - t.Template = template + t.T = template case "mst": var template *mst.Template mstpp := new(mst.StaticProvider) @@ -208,7 +210,7 @@ func LoadTemplate(lang string, rootName string, root io.Reader, partials map[str break } template, e = mst.ParseStringPartials(string(buf), mstpp) - t.Template = template + t.T = template } default: e = fmt.Errorf("'%s' is not a supported template language", lang) diff --git a/template_test.go b/template_test.go @@ -81,22 +81,22 @@ func validateTemplate(t *testing.T, template Template, templateType string, root "mst": "*mustache.Template", } - rt := reflect.TypeOf(template.Template).String() + rt := reflect.TypeOf(template.T).String() if rt != types[templateType] { t.Fatalf("invalid template type '%s' loaded, should be '%s' (%s)", rt, types[templateType], templateType) } - if types[templateType] == "*template.Template" { + if types[templateType] == "*template.T" { var rv []reflect.Value for _, p := range partialNames { - rv := reflect.ValueOf(template.Template).MethodByName("Lookup").Call([]reflect.Value{reflect.ValueOf(p)}) + rv := reflect.ValueOf(template.T).MethodByName("Lookup").Call([]reflect.Value{reflect.ValueOf(p)}) if rv[0].IsNil() { t.Fatalf("missing defined template '%s'", p) - rv = reflect.ValueOf(template.Template).MethodByName("DefinedTemplates").Call([]reflect.Value{}) + rv = reflect.ValueOf(template.T).MethodByName("DefinedTemplates").Call([]reflect.Value{}) t.Log(rv) } } - rv = reflect.ValueOf(template.Template).MethodByName("Name").Call([]reflect.Value{}) + rv = reflect.ValueOf(template.T).MethodByName("Name").Call([]reflect.Value{}) if rv[0].String() != rootName { t.Fatalf("invalid template name: %s does not match %s", rv[0].String(), rootName) }