Reflect actual slice type in TreeFromMap (#145)

* Reflect actual slice type in TreeFromMap
* Fix writeTo for slices tomlValues

Fixes #143
This commit is contained in:
Thomas Pelletier
2017-03-23 11:20:46 +01:00
committed by GitHub
parent 25e50242f6
commit f6e7596e8d
4 changed files with 36 additions and 20 deletions
+24 -3
View File
@@ -3,6 +3,7 @@ package toml
import (
"testing"
"time"
"strconv"
)
type customString string
@@ -21,20 +22,19 @@ func validate(t *testing.T, path string, object interface{}) {
}
case []*TomlTree:
for index, tree := range o {
validate(t, path+"."+string(index), tree)
validate(t, path+"."+strconv.Itoa(index), tree)
}
case *tomlValue:
switch o.value.(type) {
case int64, uint64, bool, string, float64, time.Time,
[]int64, []uint64, []bool, []string, []float64, []time.Time:
return // ok
default:
t.Fatalf("tomlValue at key %s containing incorrect type %T", path, o.value)
}
default:
t.Fatalf("value at key %s is of incorrect type %T", path, object)
}
t.Log("validation ok", path)
t.Logf("validation ok %s as %T", path, object)
}
func validateTree(t *testing.T, tree *TomlTree) {
@@ -103,3 +103,24 @@ func TestTomlTreeCreateToTreeInvalidTableGroupType(t *testing.T) {
t.Fatalf("expected error %s, got %s", expected, err.Error())
}
}
func TestRoundTripArrayOfTables(t *testing.T) {
orig := "\n[[stuff]]\n name = \"foo\"\n things = [\"a\",\"b\"]\n"
tree, err := Load(orig)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
m := tree.ToMap()
tree, err = TreeFromMap(m)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
want := orig
got := tree.String()
if got != want {
t.Errorf("want:\n%s\ngot:\n%s", want, got)
}
}