Skip to content

Commit

Permalink
Write empty tables as well (#169)
Browse files Browse the repository at this point in the history
Empty tables are allowed by the spec, so they should not be removed:

  [[empty-tables]]
  [[empty-tables]]

is perfectly valid.

Fixes #163
  • Loading branch information
pelletier authored May 31, 2017
1 parent 40ecdac commit 5ccdfb1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
22 changes: 10 additions & 12 deletions tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,16 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64) (
}
case []*Tree:
for _, subTree := range node {
if len(subTree.values) > 0 {
tableArrayName := fmt.Sprintf("\n%s[[%s]]\n", indent, combinedKey)
writtenBytesCount, err := w.Write([]byte(tableArrayName))
bytesCount += int64(writtenBytesCount)
if err != nil {
return bytesCount, err
}

bytesCount, err = subTree.writeTo(w, indent+" ", combinedKey, bytesCount)
if err != nil {
return bytesCount, err
}
tableArrayName := fmt.Sprintf("\n%s[[%s]]\n", indent, combinedKey)
writtenBytesCount, err := w.Write([]byte(tableArrayName))
bytesCount += int64(writtenBytesCount)
if err != nil {
return bytesCount, err
}

bytesCount, err = subTree.writeTo(w, indent+" ", combinedKey, bytesCount)
if err != nil {
return bytesCount, err
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tomltree_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ func assertErrorString(t *testing.T, expected string, err error) {
}
}

func TestTreeWriteToEmptyTable(t *testing.T) {
doc := `[[empty-tables]]
[[empty-tables]]`

toml, err := Load(doc)
if err != nil {
t.Fatal("Unexpected Load error:", err)
}
tomlString, err := toml.ToTomlString()
if err != nil {
t.Fatal("Unexpected ToTomlString error:", err)
}

expected := `
[[empty-tables]]
[[empty-tables]]
`

if tomlString != expected {
t.Fatalf("Expected:\n%s\nGot:\n%s", expected, tomlString)
}
}

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

0 comments on commit 5ccdfb1

Please sign in to comment.