dati

data and template interface
git clone git://git.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 084a5db7ea5d47e5312043cb2084e7c7613e7184
parent 89cbba6fe81ccaf1b8ce7cbdc3891611807c59d5
Author: gearsix <gearsix@tuta.io>
Date:   Mon, 15 Mar 2021 14:45:22 +0000

added toml support to data parsing

Diffstat:
Msrc/data.go | 13++++++-------
Msrc/data_test.go | 216+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
2 files changed, 129 insertions(+), 100 deletions(-)

diff --git a/src/data.go b/src/data.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "github.com/pelletier/go-toml" "gopkg.in/yaml.v3" "io" "io/ioutil" @@ -30,13 +31,11 @@ func LoadData(lang string, in io.Reader) (d Data, e error) { } if lang == "json" { - if json.Valid(fbuf) { - e = json.Unmarshal(fbuf, &d) - } else { - e = fmt.Errorf("invalid json %s", fbuf) - } + e = json.Unmarshal(fbuf, &d) } else if lang == "yaml" { e = yaml.Unmarshal(fbuf, &d) + } else if lang == "toml" { + e = toml.Unmarshal(fbuf, &d) } else { e = fmt.Errorf("'%s' is not a supported data language", lang) } @@ -87,7 +86,7 @@ func LoadDataFiles(order string, paths ...string) []Data { if d, e = LoadDataFile(p); e == nil { loaded[p] = d } else { - warn("skipping data file '%s'", p) + warn("skipping data file '%s': %s", p, e) e = nil } } @@ -221,7 +220,7 @@ func MergeData(data ...Data) Data { if merged[k] == nil { merged[k] = v } else { - warn("merge conflict for data key '%s'\n", k) + warn("merge conflict for data key '%s'", k) } } } diff --git a/src/data_test.go b/src/data_test.go @@ -2,21 +2,20 @@ package main import ( "encoding/json" + "github.com/pelletier/go-toml" "gopkg.in/yaml.v3" "os" "strings" "testing" ) -const goodJson1 = `{"json":0}` +const goodJson = `{"json":0}` const goodJson2 = `{"json":1}` -const badJson = `{"json":2:]}}` - -const goodYaml1 = `yaml: 0 +const goodYaml = `yaml: 0 ` -const goodYaml2 = `yaml: "1" +const goodToml = `toml = 0 ` -const badYaml = `"yaml--: '2` +const badData = `{"json"!:2:]}}` func writeTestFile(path string, Data string) (e error) { var f *os.File @@ -37,42 +36,68 @@ func TestLoadData(t *testing.T) { var e error var b []byte - if d, e = LoadData("json", strings.NewReader(goodJson1)); e != nil { + // json + if d, e = LoadData("json", strings.NewReader(goodJson)); e != nil { t.Error(e) } else if len(d) == 0 { t.Error("no data loaded") } else { if b, e = json.Marshal(d); e != nil { t.Error(e) - } else if string(b) != goodJson1 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson1) + } else if string(b) != goodJson { + t.Errorf("incorrect json: %s does not match %s", b, goodJson) } } - if d, e = LoadData("json", strings.NewReader(badJson)); e == nil { - t.Error("bad.json passed") - } - if d, e = LoadData("json", strings.NewReader("")); e != nil || len(d) > 0 { - t.Errorf("empty file failed: %s, %s", d, e) + if d, e = LoadData("json", strings.NewReader(badData)); e == nil || len(d) > 0 { + t.Error("bad json passed") } - if d, e = LoadData("yaml", strings.NewReader(goodYaml1)); e != nil { + // yaml + if d, e = LoadData("yaml", strings.NewReader(goodYaml)); e != nil { t.Error(e) } else if len(d) == 0 { t.Error("no data loaded") } else { if b, e = yaml.Marshal(d); e != nil { t.Error(e) - } else if string(b) != goodYaml1 { - t.Errorf("incorrect yaml: %s does not match %s", b, goodYaml1) + } else if string(b) != goodYaml { + t.Errorf("incorrect yaml: %s does not match %s", b, goodYaml) } } - if d, e = LoadData("yaml", strings.NewReader(badYaml)); e == nil { - t.Error("bad.yaml passed") - } - if d, e = LoadData("yaml", strings.NewReader("")); e != nil || len(d) > 0 { - t.Errorf("empty file failed: %s, %s", d, e) + if d, e = LoadData("yaml", strings.NewReader(badData)); e == nil || len(d) > 0 { + t.Error("bad yaml passed") } + // toml + if d, e = LoadData("toml", strings.NewReader(goodToml)); e != nil { + t.Error(e) + } else if len(d) == 0 { + t.Error("no data loaded") + } else { + if b, e = toml.Marshal(d); e != nil { + t.Error(e) + } else if string(b) != goodToml { + t.Errorf("incorrect toml: %s does not match %s", b, goodToml) + } + } + if d, e = LoadData("toml", strings.NewReader(badData)); e == nil || len(d) > 0 { + t.Error("bad toml passed") + } + + // misc + if d, e = LoadData("json", strings.NewReader("")); e != nil { + t.Errorf("empty file failed for json: %s, %s", d, e) + } + if d, e = LoadData("yaml", strings.NewReader("")); e != nil { + t.Errorf("empty file failed for yaml: %s, %s", d, e) + } + if d, e = LoadData("toml", strings.NewReader("")); e != nil { + t.Errorf("empty file failed toml: %s, %s", d, e) + } + if d, e = LoadData("ebrgji", strings.NewReader(goodJson)); e == nil || len(d) > 0 { + t.Errorf("invalid data language passed: %s, %s", d, e) + } + return } @@ -83,16 +108,20 @@ func TestLoadDataFiles(t *testing.T) { var d []Data tdir := t.TempDir() - p = append(p, tdir+"/good2.json") - if e = writeTestFile(p[0], goodJson2); e != nil { + p = append(p, tdir+"/good.json") + if e = writeTestFile(p[len(p)-1], goodJson); e != nil { + t.Skip("setup failure:", e) + } + p = append(p, tdir+"/1.yaml") + if e = writeTestFile(p[len(p)-1], goodYaml); e != nil { t.Skip("setup failure:", e) } - p = append(p, tdir+"/good1.yaml") - if e = writeTestFile(p[1], goodYaml1); e != nil { + p = append(p, tdir+"/good.toml") + if e = writeTestFile(p[len(p)-1], goodToml); e != nil { t.Skip("setup failure:", e) } p = append(p, tdir+"/bad.json") - if e = writeTestFile(p[2], badJson); e != nil { + if e = writeTestFile(p[len(p)-1], badData); e != nil { t.Skip("setup failure:", e) } @@ -102,15 +131,19 @@ func TestLoadDataFiles(t *testing.T) { } else if len(d) == 0 { t.Error("no data loaded") } else { - if b, e = json.Marshal(d[0]); e != nil { + if b, e = yaml.Marshal(d[0]); e != nil { t.Error(e) - } else if string(b) == goodJson2 { + } else if string(b) != goodYaml { t.Error("data returned out of order") } - - if b, e = yaml.Marshal(d[1]); e != nil { + if b, e = json.Marshal(d[1]); e != nil { + t.Error(e) + } else if string(b) != goodJson { + t.Error("data returned out of order") + } + if b, e = toml.Marshal(d[2]); e != nil { t.Error(e) - } else if string(b) == goodYaml1 { + } else if string(b) != goodToml { t.Error("data returned out of order") } } @@ -121,15 +154,19 @@ func TestLoadDataFiles(t *testing.T) { } else if len(d) == 0 { t.Error("no data loaded") } else { - if b, e = yaml.Marshal(d[0]); e != nil { + if b, e = toml.Marshal(d[0]); e != nil { t.Error(e) - } else if string(b) == goodYaml1 { + } else if string(b) != goodToml { t.Error("data returned out of order") } - if b, e = json.Marshal(d[1]); e != nil { t.Error(e) - } else if string(b) == goodJson2 { + } else if string(b) != goodJson { + t.Error("data returned out of order") + } + if b, e = yaml.Marshal(d[2]); e != nil { + t.Error(e) + } else if string(b) != goodYaml { t.Error("data returned out of order") } } @@ -140,15 +177,19 @@ func TestLoadDataFiles(t *testing.T) { } else if len(d) == 0 { t.Error("no data loaded") } else { - if b, e = yaml.Marshal(d[0]); e != nil { + if b, e = json.Marshal(d[0]); e != nil { t.Error(e) - } else if string(b) == goodYaml1 { + } else if string(b) != goodJson { t.Error("data returned out of order") } - - if b, e = json.Marshal(d[1]); e != nil { + if b, e = yaml.Marshal(d[1]); e != nil { t.Error(e) - } else if string(b) == goodJson2 { + } else if string(b) != goodYaml { + t.Error("data returned out of order") + } + if b, e = toml.Marshal(d[2]); e != nil { + t.Error(e) + } else if string(b) != goodToml { t.Error("data returned out of order") } } @@ -159,50 +200,21 @@ func TestLoadDataFiles(t *testing.T) { } else if len(d) == 0 { t.Error("no data loaded") } else { - if b, e = json.Marshal(d[0]); e != nil { + if b, e = toml.Marshal(d[0]); e != nil { t.Error(e) - } else if string(b) == goodJson2 { + } else if string(b) != goodToml { t.Error("data returned out of order") } - if b, e = yaml.Marshal(d[1]); e != nil { t.Error(e) - } else if string(b) == goodYaml1 { + } else if string(b) != goodYaml { + t.Error("data returned out of order") + } + if b, e = json.Marshal(d[2]); e != nil { + t.Error(e) + } else if string(b) != goodJson { t.Error("data returned out of order") } - } -} - -func TestMergeData(t *testing.T) { - var e error - var d []Data - var m Data - - if m, e = LoadData("json", strings.NewReader(goodJson1)); e == nil { - d = append(d, m) - } else { - t.Skip("setup failure:", e) - } - if m, e = LoadData("json", strings.NewReader(goodJson2)); e == nil { - d = append(d, m) - } else { - t.Skip("setup failure:", e) - } - if m, e = LoadData("json", strings.NewReader(goodYaml1)); e == nil { - d = append(d, m) - } else { - t.Skip("setup failure:", e) - } - if m, e = LoadData("json", strings.NewReader(goodYaml2)); e == nil { - d = append(d, m) - } else { - t.Skip("setup failure:", e) - } - - m = nil - m = MergeData(d...) - if m["json"] == nil || m["yaml"] == nil { - t.Error("missing global keys") } } @@ -213,27 +225,17 @@ func TestGenerateSuperData(t *testing.T) { var d []Data var sd Data - if data, e = LoadData("json", strings.NewReader(goodJson1)); e == nil { + if data, e = LoadData("json", strings.NewReader(goodJson)); e == nil { gd = append(gd, data) } else { t.Skip("setup failure:", e) } - if data, e = LoadData("json", strings.NewReader(goodJson1)); e == nil { + if data, e = LoadData("json", strings.NewReader(goodJson)); e == nil { gd = append(gd, data) } else { t.Skip("setup failure:", e) } - if data, e = LoadData("yaml", strings.NewReader(goodYaml2)); e == nil { - gd = append(gd, data) - } else { - t.Skip("setup failure:", e) - } - if data, e = LoadData("yaml", strings.NewReader(goodYaml1)); e == nil { - d = append(d, data) - } else { - t.Skip("setup failure:", e) - } - if data, e = LoadData("json", strings.NewReader(goodJson2)); e == nil { + if data, e = LoadData("yaml", strings.NewReader(goodYaml)); e == nil { d = append(d, data) } else { t.Skip("setup failure:", e) @@ -244,11 +246,39 @@ func TestGenerateSuperData(t *testing.T) { t.Log(sd) t.Error("datakey is empty") } - if v, ok := sd["testdata"].([]interface{}); ok { + if v, ok := sd["testdata"].([]Data); ok == false { t.Log(sd) t.Error("unable to infer datakey 'testdata'") - } else if len(v) == 2 { + } else if len(v) < len(data) { t.Log(sd) t.Error("datakey is missing data") } } + +func TestMergeData(t *testing.T) { + var e error + var d []Data + var m Data + + if m, e = LoadData("json", strings.NewReader(goodJson)); e == nil { + d = append(d, m) + } else { + t.Skip("setup failure:", e) + } + if m, e = LoadData("json", strings.NewReader(goodJson2)); e == nil { + d = append(d, m) + } else { + t.Skip("setup failure:", e) + } + if m, e = LoadData("yaml", strings.NewReader(goodYaml)); e == nil { + d = append(d, m) + } else { + t.Skip("setup failure:", e) + } + + m = nil + m = MergeData(d...) + if m["json"] == nil || m["yaml"] == nil { + t.Error("missing global keys") + } +}