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 e0ca51e79c266245d8d30bb99c6ac3c5a366d2f0
parent c95e5b6c7580432b54a4aa9d9a660d0355271dda
Author: gearsix <gearsix@tuta.io>
Date:   Sun, 12 Feb 2023 14:42:33 +0000

minor improvements, better variable names, updated LICENSE dates, etc

Diffstat:
MTODO | 1+
Mcmd/dati.go | 5+++--
Mdata.go | 34+++++++++++++++++-----------------
Mdata_test.go | 2+-
Mfile.go | 2+-
Mfile_test.go | 17+++++++++++++++++
Mtemplate.go | 47++++++++++++++++++++++++-----------------------
Mtemplate_test.go | 6+++---
8 files changed, 67 insertions(+), 47 deletions(-)

diff --git a/TODO b/TODO @@ -11,3 +11,4 @@ v2.. Aim to make api conformative - rename Read* -> Decode* - rename Write* -> Encode* + - remove file.go ? not used anymore diff --git a/cmd/dati.go b/cmd/dati.go @@ -1,7 +1,7 @@ package main /* - Copyright (C) 2021 gearsix <gearsix@tuta.io> + Copyright (C) 2023 gearsix <gearsix@tuta.io> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,10 +21,11 @@ import ( "bufio" "bytes" "fmt" - "notabug.org/gearsix/dati" "os" "path/filepath" "strings" + + "notabug.org/gearsix/dati" ) // Data is just a generic map for key/value data diff --git a/data.go b/data.go @@ -1,7 +1,7 @@ package dati /* -Copyright (C) 2021 gearsix <gearsix@tuta.io> +Copyright (C) 2023 gearsix <gearsix@tuta.io> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -72,43 +72,43 @@ func ReadDataFormat(path string) DataFormat { } // LoadData attempts to load all data from `in` as `format` and writes -// the result in the pointer `outp`. -func LoadData(format DataFormat, in io.Reader, outp interface{}) error { - inbuf, e := ioutil.ReadAll(in) - if e != nil { - return e +// the result in the pointer `out`. +func LoadData(format DataFormat, in io.Reader, out interface{}) error { + inbuf, err := ioutil.ReadAll(in) + if err != nil { + return err } else if len(inbuf) == 0 { return nil } switch format { case JSON: - e = json.Unmarshal(inbuf, outp) + err = json.Unmarshal(inbuf, out) case YAML: - e = yaml.Unmarshal(inbuf, outp) + err = yaml.Unmarshal(inbuf, out) case TOML: - e = toml.Unmarshal(inbuf, outp) + err = toml.Unmarshal(inbuf, out) default: - e = fmt.Errorf("'%s' is not a supported data language", format) + err = fmt.Errorf("'%s' is not a supported data language", format) } - return e + return err } // 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 { - f, e := os.Open(path) - defer f.Close() + file, err := os.Open(path) + defer file.Close() - if e == nil { - if e = LoadData(ReadDataFormat(path), f, outp); e != nil { - e = fmt.Errorf("failed to load data '%s': %s", path, e.Error()) + if err == nil { + if err = LoadData(ReadDataFormat(path), file, outp); err != nil { + err = fmt.Errorf("failed to load data '%s': %s", path, err.Error()) } } - return e + return err } // WriteData attempts to write `data` as `format` to `outp`. diff --git a/data_test.go b/data_test.go @@ -1,7 +1,7 @@ package dati /* -Copyright (C) 2021 gearsix <gearsix@tuta.io> +Copyright (C) 2023 gearsix <gearsix@tuta.io> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/file.go b/file.go @@ -1,7 +1,7 @@ package dati /* -Copyright (C) 2021 gearsix <gearsix@tuta.io> +Copyright (C) 2023 gearsix <gearsix@tuta.io> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/file_test.go b/file_test.go @@ -1,5 +1,22 @@ package dati +/* +Copyright (C) 2023 gearsix <gearsix@tuta.io> + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <https://www.gnu.org/licenses/>. +*/ + import ( "os" "path/filepath" diff --git a/template.go b/template.go @@ -1,7 +1,7 @@ package dati /* -Copyright (C) 2021 gearsix <gearsix@tuta.io> +Copyright (C) 2023 gearsix <gearsix@tuta.io> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,7 +20,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. import ( "bytes" "fmt" - mst "github.com/cbroglie/mustache" hmpl "html/template" "io" "io/ioutil" @@ -29,6 +28,8 @@ import ( "reflect" "strings" tmpl "text/template" + + mst "github.com/cbroglie/mustache" ) // TemplateLanguage provides a list of supported languages for @@ -85,7 +86,7 @@ type Template struct { // Execute executes `t` against `d`. Reflection is used to determine // the template type and call it's execution fuction. -func (t *Template) Execute(d interface{}) (result bytes.Buffer, err error) { +func (t *Template) Execute(data interface{}) (result bytes.Buffer, err error) { var funcName string var params []reflect.Value tType := reflect.TypeOf(t.T) @@ -96,10 +97,10 @@ func (t *Template) Execute(d interface{}) (result bytes.Buffer, err error) { switch tType.String() { case "*template.Template": // golang templates funcName = "Execute" - params = []reflect.Value{reflect.ValueOf(&result), reflect.ValueOf(d)} + params = []reflect.Value{reflect.ValueOf(&result), reflect.ValueOf(data)} case "*mustache.Template": funcName = "FRender" - params = []reflect.Value{reflect.ValueOf(&result), reflect.ValueOf(d)} + params = []reflect.Value{reflect.ValueOf(&result), reflect.ValueOf(data)} default: err = fmt.Errorf("unable to infer template type '%s'", reflect.TypeOf(t.T).String()) } @@ -117,12 +118,12 @@ func (t *Template) Execute(d interface{}) (result bytes.Buffer, err error) { // LoadTemplateFilepath loads a Template from file `root`. All files in `partials` // that have the same template type (identified by file extension) are also // parsed and associated with the parsed root template. -func LoadTemplateFile(rootPath string, partialPaths ...string) (t Template, e error) { +func LoadTemplateFile(rootPath string, partialPaths ...string) (t Template, err error) { var stat os.FileInfo - if stat, e = os.Stat(rootPath); e != nil { + if stat, err = os.Stat(rootPath); err != nil { return } else if stat.IsDir() { - e = fmt.Errorf("rootPath path must be a file, not a directory: %s", rootPath) + err = fmt.Errorf("rootPath path must be a file, not a directory: %s", rootPath) return } @@ -131,7 +132,7 @@ func LoadTemplateFile(rootPath string, partialPaths ...string) (t Template, e er rootName := strings.TrimSuffix(filepath.Base(rootPath), filepath.Ext(rootPath)) var root *os.File - if root, e = os.Open(rootPath); e != nil { + if root, err = os.Open(rootPath); err != nil { return } defer root.Close() @@ -143,16 +144,16 @@ func LoadTemplateFile(rootPath string, partialPaths ...string) (t Template, e er name = strings.TrimSuffix(name, filepath.Ext(name)) } - if stat, e = os.Stat(path); e != nil { + if _, err = os.Stat(path); err != nil { return } - var p *os.File - if p, e = os.Open(path); e != nil { + var partial *os.File + if partial, err = os.Open(path); err != nil { return } - defer p.Close() - partials[name] = p + defer partial.Close() + partials[name] = partial } return LoadTemplate(lang, rootName, root, partials) @@ -176,18 +177,18 @@ func LoadTemplateString(lang TemplateLanguage, rootName string, root string, par // `root` should be a string of template, with syntax matching that of // `lang`. `partials` should be a string of template, with syntax // matching that of `lang`. -func LoadTemplate(lang TemplateLanguage, rootName string, root io.Reader, partials map[string]io.Reader) (t Template, e error) { +func LoadTemplate(lang TemplateLanguage, rootName string, root io.Reader, partials map[string]io.Reader) (t Template, err error) { t.Name = rootName switch TemplateLanguage(lang) { case TMPL: - t.T, e = loadTemplateTmpl(rootName, root, partials) + t.T, err = loadTemplateTmpl(rootName, root, partials) case HMPL: - t.T, e = loadTemplateHmpl(rootName, root, partials) + t.T, err = loadTemplateHmpl(rootName, root, partials) case MST: - t.T, e = loadTemplateMst(rootName, root, partials) + t.T, err = loadTemplateMst(rootName, root, partials) default: - e = fmt.Errorf("'%s' is not a supported template language", lang) + err = fmt.Errorf("'%s' is not a supported template language", lang) } return @@ -236,19 +237,19 @@ 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) + mstprv := new(mst.StaticProvider) + mstprv.Partials = make(map[string]string) for name, partial := range partials { if buf, err := ioutil.ReadAll(partial); err != nil { return nil, err } else { - mstpp.Partials[name] = string(buf) + mstprv.Partials[name] = string(buf) } } if buf, err := ioutil.ReadAll(root); err != nil { return nil, err - } else if template, err = mst.ParseStringPartials(string(buf), mstpp); err != nil { + } else if template, err = mst.ParseStringPartials(string(buf), mstprv); err != nil { return nil, err } diff --git a/template_test.go b/template_test.go @@ -1,7 +1,7 @@ package dati /* -Copyright (C) 2021 gearsix <gearsix@tuta.io> +Copyright (C) 2023 gearsix <gearsix@tuta.io> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -55,7 +55,7 @@ var templateExts = []string{ func TestIsTemplateLanguage(t *testing.T) { for i, ext := range templateExts { var target bool - + if i < 12 { target = true } @@ -70,7 +70,7 @@ func TestIsTemplateLanguage(t *testing.T) { func TestReadTemplateLanguage(t *testing.T) { for i, ext := range templateExts { var target TemplateLanguage - + if i < 4 { target = TMPL } else if i < 8 {