Skip to content

Commit

Permalink
Fix inline tables parsing
Browse files Browse the repository at this point in the history
Inline tables were wrapped inside a TomlValue, although they should
just be part of the tree.
  • Loading branch information
pelletier committed Apr 22, 2016
1 parent 8d9c606 commit 8e6ab94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,15 @@ func (p *tomlParser) parseAssign() tomlParserStateFn {
p.raiseError(key, "The following key was defined twice: %s",
strings.Join(finalKey, "."))
}
targetNode.values[keyVal] = &tomlValue{value, key.Position}
var toInsert interface{}

switch value.(type) {
case *TomlTree:
toInsert = value
default:
toInsert = &tomlValue{value, key.Position}
}
targetNode.values[keyVal] = toInsert
return p.parseStart
}

Expand Down
22 changes: 22 additions & 0 deletions tomltree_conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ import (
"time"
)

func TestTomlTreeConversionToString(t *testing.T) {
toml, err := Load(`name = { first = "Tom", last = "Preston-Werner" }
points = { x = 1, y = 2 }`)

if err != nil {
t.Fatal("Unexpected error:", err)
}

reparsedTree, err := Load(toml.ToString())

assertTree(t, reparsedTree, err, map[string]interface{}{
"name": map[string]interface{}{
"first": "Tom",
"last": "Preston-Werner",
},
"points": map[string]interface{}{
"x": int64(1),
"y": int64(2),
},
})
}

func testMaps(t *testing.T, actual, expected map[string]interface{}) {
if !reflect.DeepEqual(actual, expected) {
t.Fatal("trees aren't equal.\n", "Expected:\n", expected, "\nActual:\n", actual)
Expand Down

0 comments on commit 8e6ab94

Please sign in to comment.