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 68ba018e9ca48705fb28466b378491fa1365bc94
parent 49f66e430c521eb9a2af3b32ffd3aa4c10724520
Author: gearsix <gearsix@tuta.io>
Date:   Thu,  4 Mar 2021 17:42:11 +0000

added sort order options to sortData (-asc/-desc)

Diffstat:
Msrc/data.go | 38+++++++++++++++++++++++++++-----------
Msrc/data_test.go | 47+++++++++++++++++++++++++++++++++++++++++------
Msrc/suti.go | 5+----
Msuti.txt | 10+++++-----
4 files changed, 74 insertions(+), 26 deletions(-)

diff --git a/src/data.go b/src/data.go @@ -99,20 +99,31 @@ func LoadDataFiles(order string, paths ...string) []Data { func sortFileData(data map[string]Data, order string) []Data { sorted := make([]Data, 0, len(data)) - if order == "filename" { + if order == "filename-desc" || order == "filename-asc" || order == "filename" { fnames := make([]string, 0, len(data)) for fpath, _ := range data { fnames = append(fnames, filepath.Base(fpath)) } sort.Strings(fnames) - for _, fname := range fnames { - for fpath, d := range data { - if fname == filepath.Base(fpath) { - sorted = append(sorted, d) + + if order == "filename-desc" { + for i := len(fnames)-1; i >= 0; i-- { + for fpath, d := range data { + if fnames[i] == filepath.Base(fpath) { + sorted = append(sorted, d) + } + } + } + } else { + for _, fname := range fnames { + for fpath, d := range data { + if fname == filepath.Base(fpath) { + sorted = append(sorted, d) + } } - } + } } - } else if order == "modified" { + } else if order == "modified-desc" || order == "modified-asc" || order == "modified" { stats := make(map[string]os.FileInfo) for fpath, _ := range data { if stat, err := os.Stat(fpath); err != nil { @@ -126,9 +137,15 @@ func sortFileData(data map[string]Data, order string) []Data { for _, stat := range stats { modtimes = append(modtimes, stat.ModTime()) } - sort.Slice(modtimes, func(i, j int) bool { - return modtimes[i].Before(modtimes[j]) - }) + if order == "modified-desc" { + sort.Slice(modtimes, func(i, j int) bool { + return modtimes[i].After(modtimes[j]) + }) + } else { + sort.Slice(modtimes, func(i, j int) bool { + return modtimes[i].Before(modtimes[j]) + }) + } for _, t := range modtimes { for fpath, stat := range stats { @@ -138,7 +155,6 @@ func sortFileData(data map[string]Data, order string) []Data { } } } else { - warn("unrecognised sort option '%s', data will be unsorted", order) for _, d := range data { sorted = append(sorted, d) } diff --git a/src/data_test.go b/src/data_test.go @@ -61,12 +61,8 @@ func TestLoadDataFiles(t *testing.T) { if e = writeTestFile(p[1], goodJson1); e != nil { t.Skip("setup failure:", e) } - p = append(p, tdir+"/good1.json") - if e = writeTestFile(p[2], goodJson1); e != nil { - t.Skip("setup failure:", e) - } p = append(p, tdir+"/bad.json") - if e = writeTestFile(p[3], badJson); e != nil { + if e = writeTestFile(p[2], badJson); e != nil { t.Skip("setup failure:", e) } @@ -93,6 +89,29 @@ func TestLoadDataFiles(t *testing.T) { } } + d = LoadDataFiles("filename-desc", tdir + "/*") + if len(d) == len(p) { + t.Error("bad.json passed") + } else if len(d) == 0 { + t.Error("no data loaded") + } else { + if b, e = json.Marshal(d[0]); e != nil { + t.Error(e) + } else if string(b) == goodJson1 { + t.Error("data returned out of order") + } else if string(b) != goodJson2 { + t.Errorf("incorrect json: %s does not match %s", b, goodJson1) + } + + if b, e = json.Marshal(d[1]); e != nil { + t.Error(e) + } else if string(b) == goodJson2 { + t.Error("data returned out of order") + } else if string(b) != goodJson1 { + t.Errorf("incorrect json: %s does not match %s", b, goodJson2) + } + } + d = LoadDataFiles("modified", p...) if len(d) == len(p) { t.Error("bad.json passed") @@ -116,10 +135,26 @@ func TestLoadDataFiles(t *testing.T) { } } - d = LoadDataFiles("", tdir + "/*") + d = LoadDataFiles("modified-desc", p...) if len(d) == len(p) { t.Error("bad.json passed") } else if len(d) == 0 { t.Error("no data loaded") + } else { + if b, e = json.Marshal(d[0]); e != nil { + t.Error(e) + } else if string(b) == goodJson2 { + t.Error("data returned out of order") + } else if string(b) != goodJson1 { + t.Errorf("incorrect json: %s does not match %s", b, goodJson1) + } + + if b, e = json.Marshal(d[1]); e != nil { + t.Error(e) + } else if string(b) == goodJson1 { + t.Error("data returned out of order") + } else if string(b) != goodJson2 { + t.Errorf("incorrect json: %s does not match %s", b, goodJson2) + } } } diff --git a/src/suti.go b/src/suti.go @@ -32,10 +32,7 @@ func init() { func main() { _ = LoadDataFiles("", options.GlobalDataPaths...) - d := LoadDataFiles(options.SortData, options.DataPaths...) - for k, v := range d { - fmt.Println(k, v) - } + _ = LoadDataFiles(options.SortData, options.DataPaths...) templates := make([]Template, 0) for _, r := range options.RootPaths { diff --git a/suti.txt b/suti.txt @@ -45,12 +45,12 @@ OPTIONS "data") -sd attribute, -sort-data attribute - The file attribute to order data files by. - Accepted values: "filename", "modified". (default: "filename"). + The file attribute to order data files by. If no value is provided, the data + will be provided in the order it's loaded. + Accepted values: "filename", "modified". A suffix can be appended to each value to set the sort order: "-asc" (for - ascending), "-desc" (for descending). - By default, the sort order will be "filename-desc". - + ascending), "-desc" (for descending). If not specified, this defaults to + "-asc". -cfg file, -config file A data file to provide default values for the above options (see CONFIG).