Skip to content

Commit

Permalink
Return non-zero for empty trees from getTreeArrayLine
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed May 11, 2021
1 parent 2a1df71 commit f2e1486
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
}

func getTreeArrayLine(trees []*Tree) (line int) {
// get lowest line number that is not 0
// Prevent returning 0 for empty trees
line = int(^uint(0) >> 1)
// get lowest line number >= 0
for _, tv := range trees {
if tv.position.Line < line || line == 0 {
line = tv.position.Line
Expand Down
23 changes: 23 additions & 0 deletions tomltree_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,29 @@ c = nan`
}
}

func TestOrderedEmptyTrees(t *testing.T) {
type val struct {
Key string `toml:"key"`
}
type structure struct {
First val `toml:"first"`
Empty []val `toml:"empty"`
}
input := structure{First: val{Key: "value"}}
buf := new(bytes.Buffer)
err := NewEncoder(buf).Order(OrderPreserve).Encode(input)
if err != nil {
t.Fatal("failed to encode input")
}
expected := `
[first]
key = "value"
`
if expected != buf.String() {
t.Fatal("expected and encoded body aren't equal: ", expected, buf.String())
}
}

func TestIssue290(t *testing.T) {
tomlString :=
`[table]
Expand Down

0 comments on commit f2e1486

Please sign in to comment.