Skip to content

Commit

Permalink
Fix ToMap for tables in nested mixed-type arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mstetson committed Nov 19, 2020
1 parent 1bd9461 commit c1c4b98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
22 changes: 14 additions & 8 deletions tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,18 +518,24 @@ func (t *Tree) ToMap() map[string]interface{} {

// 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) {
var doSlice func([]interface{}) []interface{}
doSlice = func(s1 []interface{}) []interface{} {
s2 := make([]interface{}, len(s1))
for i := range s1 {
switch v := s1[i].(type) {
case *Tree:
s[i] = e.ToMap()
s2[i] = v.ToMap()
case []interface{}:
s2[i] = doSlice(v)
default:
s[i] = e
s2[i] = v
}
}
return s
return s2
}
switch v := t.value.(type) {
case []interface{}:
return doSlice(v)
default:
return v
}
Expand Down
9 changes: 6 additions & 3 deletions tomltree_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,15 @@ func TestTreeWriteToMapWithArrayOfInlineTables(t *testing.T) {
}

func TestTreeWriteToMapWithTableInMixedArray(t *testing.T) {
tree, _ := Load(`a = ["foo", {bar = "baz"}]`)
tree, _ := Load(`a = ["foo", ["bar", {baz = "quux"}]]`)
expected := map[string]interface{}{
"a": []interface{}{
"foo",
map[string]interface{}{
"bar": "baz",
[]interface{}{
"bar",
map[string]interface{}{
"baz": "quux",
},
},
},
}
Expand Down

0 comments on commit c1c4b98

Please sign in to comment.