Skip to content

Commit

Permalink
Tree.Marshal returns the TOML encoding of Tree
Browse files Browse the repository at this point in the history
The Tree.Marshal tried to marshal the Tree struct itself rather than the nodes being part of the tree.

Fixes #295
  • Loading branch information
mbialon committed Sep 25, 2019
1 parent 091e2dc commit b1cca1f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,11 @@ func (t *Tree) Unmarshal(v interface{}) error {
// See Marshal() documentation for types mapping table.
func (t *Tree) Marshal() ([]byte, error) {
var buf bytes.Buffer
err := NewEncoder(&buf).Encode(t)
return buf.Bytes(), err
_, err := t.WriteTo(&buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// Unmarshal parses the TOML-encoded data and stores the result in the value
Expand Down
24 changes: 24 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1675,3 +1675,27 @@ func TestUnmarshalPreservesUnexportedFields(t *testing.T) {
}
})
}

func TestTreeMarshal(t *testing.T) {
cases := [][]byte{
basicTestToml,
marshalTestToml,
emptyTestToml,
pointerTestToml,
}
for _, expected := range cases {
t.Run("", func(t *testing.T) {
tree, err := LoadBytes(expected)
if err != nil {
t.Fatal(err)
}
result, err := tree.Marshal()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(result, expected) {
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result)
}
})
}
}

0 comments on commit b1cca1f

Please sign in to comment.