commit 9aed9631e29c0d17768c8b36f3143ee80c2eec36
parent 514c02e9a0510f7912ef79225215d64b03ee8eb8
Author: gearsix <gearsix@tuta.io>
Date: Sun, 14 Feb 2021 23:54:58 +0000
merged LoadDataFile into LoadDataFiles
- also added getDataType(path string)
Diffstat:
2 files changed, 25 insertions(+), 47 deletions(-)
diff --git a/src/data.go b/src/data.go
@@ -12,40 +12,53 @@ import (
type data interface{}
+func getDataType(path string) string {
+ return strings.TrimPrefix(filepath.Ext(path), ".")
+}
+
// 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 _, dpath := range paths {
- if stat, err = os.Stat(dpath); err != nil {
- warn("skipping data file '%s' (%s)", dpath, err)
+ 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
+ }
+ defer f.Close()
if stat.IsDir() {
- _ = filepath.Walk(dpath,
- func(path string, info os.FileInfo, e error) error {
- if e == nil && !info.IsDir() {
- if d, e = LoadDataFile(path); e == nil {
- loaded[path] = d
+ _ = 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
}
}
if e != nil {
- warn("skipping data file '%s' (%s)", path, e)
+ warn("skipping data file '%s' (%s)", p, e)
}
return e
})
} else {
- if d, err = LoadDataFile(dpath); err == nil {
- loaded[dpath] = d
+ dtype = getDataType(path)
+ if d, err = LoadData(dtype, f); err == nil {
+ loaded[path] = d
} else {
- warn("skipping data file '%s' (%s)", dpath, err)
+ warn("skipping data file '%s' (%s)", path, err)
}
}
}
@@ -53,20 +66,6 @@ func LoadDataFiles(paths ...string) map[string]data {
return loaded
}
-// LoadDataFile TODO
-func LoadDataFile(path string) (d data, e error) {
- var f *os.File
-
- f, e = os.Open(path)
- defer f.Close()
- if e == nil {
- dtype := strings.TrimPrefix(filepath.Ext(path), ".")
- d, e = LoadData(dtype, f)
- }
-
- return d, e
-}
-
// LoadData TODO
func LoadData(lang string, in io.Reader) (d data, e error) {
var fbuf []byte
diff --git a/src/data_test.go b/src/data_test.go
@@ -48,27 +48,6 @@ func TestLoadDataFiles(t *testing.T) {
}
}
-func TestLoadDataFile(t *testing.T) {
- var e error
- tdir := t.TempDir()
-
- if e = writeTestFile(tdir+"good.json", goodJson); e != nil {
- t.Skip("setup failure:", e)
- }
- if e = writeTestFile(tdir+"bad.json", badJson); e != nil {
- t.Skip("setup failure:", e)
- }
-
- if _, e = LoadDataFile(tdir+"good.json"); e != nil {
- t.Error(e)
- }
- if _, e = LoadDataFile(tdir+"bad.json"); e == nil {
- t.Error("bad.json passed")
- }
-
- return
-}
-
func TestLoadData(t *testing.T) {
var e error