dati

A Go library/binary to parse & execute data against template langauges.
git clone git://src.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 89cbba6fe81ccaf1b8ce7cbdc3891611807c59d5
parent 40334f2ac36e907b6b8c1a54462046d94cbf25f1
Author: gearsix <gearsix@tuta.io>
Date:   Mon, 15 Mar 2021 13:19:42 +0000

minor improv to err handling in LoadTemplateFile

Diffstat:
Dsrc/_test.go | 20--------------------
Msrc/template.go | 52+++++++++++++++++++++++-----------------------------
Msrc/template_test.go | 9++++-----
3 files changed, 27 insertions(+), 54 deletions(-)

diff --git a/src/_test.go b/src/_test.go @@ -1,20 +0,0 @@ -package main - -import ( - "os" - "testing" -) - -func writeFile(path string, data string) (e error) { - var f *os.File - - if f, e = os.Create(path); e != nil { - return - } - if _, e = f.WriteString(data); e != nil { - return - } - f.Close() - - return -} diff --git a/src/template.go b/src/template.go @@ -19,58 +19,52 @@ func getTemplateType(path string) string { func LoadTemplateFile(root string, partials ...string) (t Template, e error) { if len(root) == 0 { - e = fmt.Errorf("no root template specified") + return nil, fmt.Errorf("no root tempslate specified") } + if stat, err := os.Stat(root); err != nil { - e = err + return nil, err } else if stat.IsDir() { - e = fmt.Errorf("root path must be a file, not a directory: %s", root) - } - if e != nil { - return + return nil, fmt.Errorf("root path must be a file, not a directory: %s", root) } ttype := getTemplateType(root) - if ttype == "tmpl" || ttype == "gotmpl" { var gotmpl *tmpl.Template - if gotmpl, e = tmpl.ParseFiles(root); e != nil { - return nil, e - } - for _, p := range partials { - ptype := getTemplateType(p) - if ptype == "tmpl" || ptype == "gotmpl" { - gotmpl, e = gotmpl.ParseFiles(p) + if gotmpl, e = tmpl.ParseFiles(root); e == nil { + for _, p := range partials { + ptype := getTemplateType(p) + if ptype == "tmpl" || ptype == "gotmpl" { + if gotmpl, e = gotmpl.ParseFiles(p); e != nil { + warn("failed to parse partial '%s': %s", p, e) + } + } } + t = gotmpl } - t = gotmpl } else if ttype == "hmpl" || ttype == "gohmpl" { var gohmpl *hmpl.Template - if gohmpl, e = hmpl.ParseFiles(root); e != nil { - return nil, e - } - for _, p := range partials { - ptype := getTemplateType(p) - if ptype == "tmpl" || ptype == "gotmpl" { - gohmpl, e = gohmpl.ParseFiles(p) + if gohmpl, e = hmpl.ParseFiles(root); e == nil { + for _, p := range partials { + ptype := getTemplateType(p) + if ptype == "tmpl" || ptype == "gotmpl" { + if gohmpl, e = gohmpl.ParseFiles(p); e != nil { + warn("failed to parse partial '%s': %s", p, e) + } + } } + t = gohmpl } - t = gohmpl } else { e = fmt.Errorf("'%s' is not a supported template language", ttype) } - return } func ExecuteTemplate(t Template, d Data) (result bytes.Buffer, err error) { - if t == nil || d == nil { - err = fmt.Errorf("missing parameters") - return - } - tv := reflect.ValueOf(t) tt := reflect.TypeOf(t) + if tt.String() == "*template.Template" { // tmpl or hmpl rval := tv.MethodByName("Execute").Call([]reflect.Value{ reflect.ValueOf(&result), reflect.ValueOf(&d), diff --git a/src/template_test.go b/src/template_test.go @@ -62,18 +62,17 @@ func TestLoadTemplateFile(t *testing.T) { } for _, root := range gr { // good root, bad partials if _, e := LoadTemplateFile(root, bp...); e == nil { - t.Errorf("good template with bad partials passed\n") + t.Errorf("no error for good template with bad partials\n") } } for _, root := range br { // bad root, good partials - if tmp, e := LoadTemplateFile(root, gp...); e == nil { - t.Log(tmp) - t.Errorf("bad template with good partials passed\n") + if _, e := LoadTemplateFile(root, gp...); e == nil { + t.Errorf("no error for bad template with good partials\n") } } for _, root := range br { // bad root, bad partials if _, e := LoadTemplateFile(root, bp...); e == nil { - t.Errorf("bad template with bad partials passed\n") + t.Errorf("no error for bad template with bad partials\n") } } }