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 24618687363e96a18523ac2d3e2815e862d14e93
parent d41aa38337e414708ef2e48fccb184b6844aa9e7
Author: gearsix <gearsix@tuta.io>
Date:   Mon,  1 Mar 2021 20:50:18 +0000

fixing in data.go; added support for glob paths

- improved data_test.go tests (now checks for non-loaded data)
- spotted bug in loading options in suti.go

Diffstat:
Msrc/data.go | 106++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/data_test.go | 48+++++++++++++++++++++++++++++++++++-------------
Msrc/suti.go | 8++++++--
3 files changed, 98 insertions(+), 64 deletions(-)

diff --git a/src/data.go b/src/data.go @@ -16,46 +16,74 @@ func getDataType(path string) string { return strings.TrimPrefix(filepath.Ext(path), ".") } +// LoadData TODO +func LoadData(lang string, in io.Reader) (d Data, e error) { + var fbuf []byte + if fbuf, e = ioutil.ReadAll(in); e != nil { + return + } + + if lang == "json" { + if json.Valid(fbuf) { + e = json.Unmarshal(fbuf, &d) + } else { + e = fmt.Errorf("invalid json %s", fbuf) + } + } else { + e = fmt.Errorf("'%s' is not a supported data language", lang) + } + + return +} + +// LoadDataFile TODO +func LoadDataFile(path string) (Data, error) { + if f, e := os.Open(path); e != nil { + warn("could not load data file '%s' (%s)", path, e) + return nil, e + } else { + defer f.Close() + return LoadData(getDataType(path), f) + } +} + // LoadDataFiles TODO func LoadDataFiles(paths ...string) map[string]Data { var err error var stat os.FileInfo var d Data - var dtype string - var f *os.File loaded := make(map[string]Data) - for _, path := range paths { - if stat, err = os.Stat(path); err != nil { - warn("skipping data file '%s' (%s)", path, err) - continue - } - if f, err = os.Open(path); err != nil { - warn("skipping data file '%s' (%s)", path, err) - continue + for p, path := range paths { + if strings.Contains(path, "*") { + if glob, e := filepath.Glob(path); e == nil { + paths = append(paths, glob...) + paths = append(paths[:p], paths[p+1:]...) + } else { + warn("error parsing glob '%s': %s", path, err) + } } - defer f.Close() + } - if stat.IsDir() { - _ = filepath.Walk(path, - func(p string, fi os.FileInfo, e error) error { - if e == nil && !fi.IsDir() { - dtype = getDataType(p) - if d, e = LoadData(dtype, f); e == nil { - loaded[p] = d + for _, path := range paths { + err = nil + stat, err = os.Stat(path) + if err == nil { + if stat.IsDir() { + err = filepath.Walk(path, + func(p string, fi os.FileInfo, e error) error { + if e == nil && !fi.IsDir() { + if d, e = LoadDataFile(p); e == nil { + loaded[p] = d + } else { + warn("skipping data file '%s' (%s)", p, e) + e = nil + } } - } - - if e != nil { - warn("skipping data file '%s' (%s)", p, e) - } - - return e - }) - } else { - dtype = getDataType(path) - if d, err = LoadData(dtype, f); err == nil { + return e + }) + } else if d, err = LoadDataFile(path); err == nil { loaded[path] = d } else { warn("skipping data file '%s' (%s)", path, err) @@ -65,23 +93,3 @@ func LoadDataFiles(paths ...string) map[string]Data { return loaded } - -// LoadData TODO -func LoadData(lang string, in io.Reader) (d Data, e error) { - var fbuf []byte - if fbuf, e = ioutil.ReadAll(in); e != nil { - return - } - - if lang == "json" { - if json.Valid(fbuf) { - e = json.Unmarshal(fbuf, &d) - } else { - e = fmt.Errorf("invalid json") - } - } else { - e = fmt.Errorf("'%s' is not a supported data language", lang) - } - - return -} diff --git a/src/data_test.go b/src/data_test.go @@ -23,6 +23,26 @@ func writeTestFile(path string, Data string) (e error) { return } +func TestLoadData(t *testing.T) { + var d Data + var e error + + if d, e = LoadData("json", strings.NewReader(goodJson)); e != nil { + t.Error(e) + } + if len(d) == 0 { + t.Fail() + } else { + t.Log(d) + } + + if d, e = LoadData("json", strings.NewReader(badJson)); e == nil { + t.Error("bad.json passed") + } + + return +} + func TestLoadDataFiles(t *testing.T) { var e error var p []string @@ -33,30 +53,31 @@ func TestLoadDataFiles(t *testing.T) { if e = writeTestFile(p[0], goodJson); e != nil { t.Skip("setup failure:", e) } + p = append(p, tdir+"/good.json") + if e = writeTestFile(p[1], goodJson); e != nil { + t.Skip("setup failure:", e) + } p = append(p, tdir+"/bad.json") - if e = writeTestFile(p[1], badJson); e != nil { + if e = writeTestFile(p[2], badJson); e != nil { t.Skip("setup failure:", e) } d = LoadDataFiles(tdir) if len(d) == len(p) { t.Error("bad.json passed") + } else if len(d) == 0 { + t.Error("no data loaded") } d = LoadDataFiles(p...) if len(d) == len(p) { t.Error("bad.json passed") + } else if len(d) == 0 { + t.Error("no data loaded") } -} - -func TestLoadData(t *testing.T) { - var e error - - if _, e = LoadData("json", strings.NewReader(goodJson)); e != nil { - t.Error(e) - } - if _, e = LoadData("json", strings.NewReader(badJson)); e == nil { + d = LoadDataFiles(tdir+"/*") + if len(d) == len(p) { t.Error("bad.json passed") + } else if len(d) == 0 { + t.Error("no data loaded") } - - return -} +} +\ No newline at end of file diff --git a/src/suti.go b/src/suti.go @@ -22,13 +22,17 @@ func init() { print("nothing to do") os.Exit(0) } + + options = parseArgs(os.Args[1:]) + if len(options.SortData) == 0 { + options.SortData = "filename" + } } func main() { - options := parseArgs(os.Args[1:]) - _ = LoadDataFiles(options.DataPaths...) _ = LoadDataFiles(options.GlobalDataPaths...) + _ = LoadDataFiles(options.DataPaths...) templates := make([]Template, 0) for _, r := range options.RootPaths {