Fix ToMap for tables in mixed-type arrays (#453)

This commit is contained in:
Micah Stetson
2020-11-14 18:15:35 -08:00
committed by GitHub
parent 5b4e7e5dcc
commit 1bd9461acb
2 changed files with 35 additions and 1 deletions
+20 -1
View File
@@ -510,8 +510,27 @@ func (t *Tree) ToMap() map[string]interface{} {
case *Tree:
result[k] = node.ToMap()
case *tomlValue:
result[k] = node.value
result[k] = node.toValue()
}
}
return result
}
// toValue converts a tomlValue to a built-in Go type.
func (t *tomlValue) toValue() interface{} {
switch v := t.value.(type) {
case []interface{}:
s := make([]interface{}, len(v))
for i := range s {
switch e := v[i].(type) {
case *Tree:
s[i] = e.ToMap()
default:
s[i] = e
}
}
return s
default:
return v
}
}
+15
View File
@@ -295,6 +295,21 @@ func TestTreeWriteToMapWithArrayOfInlineTables(t *testing.T) {
testMaps(t, treeMap, expected)
}
func TestTreeWriteToMapWithTableInMixedArray(t *testing.T) {
tree, _ := Load(`a = ["foo", {bar = "baz"}]`)
expected := map[string]interface{}{
"a": []interface{}{
"foo",
map[string]interface{}{
"bar": "baz",
},
},
}
treeMap := tree.ToMap()
testMaps(t, treeMap, expected)
}
func TestTreeWriteToFloat(t *testing.T) {
tree, err := Load(`a = 3.0`)
if err != nil {