Fix ToMap for tables in nested mixed-type arrays (#461)
Co-authored-by: Micah Stetson <micah@schoolsplp.com>
This commit is contained in:
+15
-16
@@ -510,27 +510,26 @@ func (t *Tree) ToMap() map[string]interface{} {
|
||||
case *Tree:
|
||||
result[k] = node.ToMap()
|
||||
case *tomlValue:
|
||||
result[k] = node.toValue()
|
||||
result[k] = tomlValueToGo(node.value)
|
||||
}
|
||||
}
|
||||
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:
|
||||
func tomlValueToGo(v interface{}) interface{} {
|
||||
if tree, ok := v.(*Tree); ok {
|
||||
return tree.ToMap()
|
||||
}
|
||||
|
||||
rv := reflect.ValueOf(v)
|
||||
|
||||
if rv.Kind() != reflect.Slice {
|
||||
return v
|
||||
}
|
||||
values := make([]interface{}, rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
item := rv.Index(i).Interface()
|
||||
values[i] = tomlValueToGo(item)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
+24
-3
@@ -296,12 +296,33 @@ func TestTreeWriteToMapWithArrayOfInlineTables(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTreeWriteToMapWithTableInMixedArray(t *testing.T) {
|
||||
tree, _ := Load(`a = ["foo", {bar = "baz"}]`)
|
||||
tree, _ := Load(`a = [
|
||||
"foo",
|
||||
[
|
||||
"bar",
|
||||
{baz = "quux"},
|
||||
],
|
||||
[
|
||||
{a = "b"},
|
||||
{c = "d"},
|
||||
],
|
||||
]`)
|
||||
expected := map[string]interface{}{
|
||||
"a": []interface{}{
|
||||
"foo",
|
||||
map[string]interface{}{
|
||||
"bar": "baz",
|
||||
[]interface{}{
|
||||
"bar",
|
||||
map[string]interface{}{
|
||||
"baz": "quux",
|
||||
},
|
||||
},
|
||||
[]interface{}{
|
||||
map[string]interface{}{
|
||||
"a": "b",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"c": "d",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user