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 cf16a5f0c2b5a76c2762d1bf521d07e4cdfb0d45
parent 8d450acadd93f08c72b3f28b26ac35b8f0b17f8a
Author: gearsix <gearsix@tuta.io>
Date:   Thu,  7 Oct 2021 13:37:25 +0100

cleanup for v0.7.0 release

- Fixes from gofmt, gocyclo, ineffassign.
- Updated CHANGELOG
- Removed TODO (emptied)

Diffstat:
MCHANGELOG | 14+++++++++++---
DTODO | 1-
Mfile.go | 2+-
Mtemplate.go | 35+++++++++++++++++------------------
Mtemplate_test.go | 6+++---
5 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,10 +1,18 @@ # CHANGELOG -## v0.3.0 +## v0.7.0 -- added LoadTemplate, loads templates from io.Reader params, template language and name must be specified. -This function is the template.go counterpart to data.go#LoadData +- added LoadTemplate, loads templates from io.Reader params. + - Template language and name must be specified. + - This function is the template.go counterpart to data.go#LoadData - added LoadTemplateStrings loads templates from string params (calls LoadTemplate) +- renamed LoadTemplateFile -> LoadTemplateFilepath +- added []SupportedTemplateLangs, IsSupportedTemplateLang(), []SupportedDataLangs, IsSupportedDataLang() +- renamed suti.Template.Source -> suti.Template.Name + - The .Name value assigned to templates when LoadTemplateFilpath() is called will + be the **base name minus the file extension**. +- Lots of refactors to tidyup, improve consistency & error handling/catches +- Testing is all upto date :) ## v0.2.2 diff --git a/TODO b/TODO @@ -1 +0,0 @@ -- Make `loadTemplateFileX` public diff --git a/file.go b/file.go @@ -97,7 +97,7 @@ func sortFileListByMod(direction string, paths []string) ([]string, error) { } } if len(sorted) != len(paths) { - fmt.Errorf("sorted length invalid") + return nil, fmt.Errorf("sorted length invalid") } return sorted, nil diff --git a/template.go b/template.go @@ -157,7 +157,7 @@ func loadTemplateTmpl(rootName string, root io.Reader, partials map[string]io.Re return nil, err } - for name, partial := range partials { + for name, partial := range partials { if buf, err := io.ReadAll(partial); err != nil { return nil, err } else if _, err = template.New(name).Parse(string(buf)); err != nil { @@ -177,7 +177,7 @@ func loadTemplateHmpl(rootName string, root io.Reader, partials map[string]io.Re return nil, err } - for name, partial := range partials { + for name, partial := range partials { if buf, err := io.ReadAll(partial); err != nil { return nil, err } else if _, err = template.New(name).Parse(string(buf)); err != nil { @@ -189,25 +189,25 @@ func loadTemplateHmpl(rootName string, root io.Reader, partials map[string]io.Re } func loadTemplateMst(rootName string, root io.Reader, partials map[string]io.Reader) (*mst.Template, error) { - var template *mst.Template - - mstpp := new(mst.StaticProvider) - mstpp.Partials = make(map[string]string) - for name, partial := range partials { - if buf, err := io.ReadAll(partial); err != nil { - return nil, err - } else { - mstpp.Partials[name] = string(buf) - } - } + var template *mst.Template - if buf, err := io.ReadAll(root); err != nil { - return nil, err - } else if template, err = mst.ParseStringPartials(string(buf), mstpp); err != nil { + mstpp := new(mst.StaticProvider) + mstpp.Partials = make(map[string]string) + for name, partial := range partials { + if buf, err := io.ReadAll(partial); err != nil { return nil, err + } else { + mstpp.Partials[name] = string(buf) } + } - return template, nil + if buf, err := io.ReadAll(root); err != nil { + return nil, err + } else if template, err = mst.ParseStringPartials(string(buf), mstpp); err != nil { + return nil, err + } + + return template, nil } // LoadTemplate loads a Template from `root` of type `lang`, named @@ -236,4 +236,3 @@ func LoadTemplate(lang string, rootName string, root io.Reader, partials map[str return } - diff --git a/template_test.go b/template_test.go @@ -193,15 +193,15 @@ func TestLoadTemplateString(t *testing.T) { name := "test" templateType = "tmpl" - if template, err = LoadTemplateString(templateType, name, tmplRootGood, + if _, err = LoadTemplateString(templateType, name, tmplRootGood, map[string]string{"tmplPartialGood": tmplPartialGood}); err != nil { t.Fatalf("'%s' template failed to load", templateType) } - if template, err = LoadTemplateString(templateType, name, tmplRootBad, + if _, err = LoadTemplateString(templateType, name, tmplRootBad, map[string]string{"tmplPartialGood": tmplPartialGood}); err == nil { testInvalid(templateType, template) } - if template, err = LoadTemplateString(templateType, name, tmplRootGood, + if _, err = LoadTemplateString(templateType, name, tmplRootGood, map[string]string{"tmplPartialGood": tmplPartialBad}); err == nil { testInvalid(templateType, template) }