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:
|
case *Tree:
|
||||||
result[k] = node.ToMap()
|
result[k] = node.ToMap()
|
||||||
case *tomlValue:
|
case *tomlValue:
|
||||||
result[k] = node.toValue()
|
result[k] = tomlValueToGo(node.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// toValue converts a tomlValue to a built-in Go type.
|
func tomlValueToGo(v interface{}) interface{} {
|
||||||
func (t *tomlValue) toValue() interface{} {
|
if tree, ok := v.(*Tree); ok {
|
||||||
switch v := t.value.(type) {
|
return tree.ToMap()
|
||||||
case []interface{}:
|
}
|
||||||
s := make([]interface{}, len(v))
|
|
||||||
for i := range s {
|
rv := reflect.ValueOf(v)
|
||||||
switch e := v[i].(type) {
|
|
||||||
case *Tree:
|
if rv.Kind() != reflect.Slice {
|
||||||
s[i] = e.ToMap()
|
|
||||||
default:
|
|
||||||
s[i] = e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
default:
|
|
||||||
return v
|
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) {
|
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{}{
|
expected := map[string]interface{}{
|
||||||
"a": []interface{}{
|
"a": []interface{}{
|
||||||
"foo",
|
"foo",
|
||||||
map[string]interface{}{
|
[]interface{}{
|
||||||
"bar": "baz",
|
"bar",
|
||||||
|
map[string]interface{}{
|
||||||
|
"baz": "quux",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[]interface{}{
|
||||||
|
map[string]interface{}{
|
||||||
|
"a": "b",
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"c": "d",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user