Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree.Marshal returns the TOML encoding of Tree #306

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is now basically the same code as ToTomlString(), mind replacing it with something like

func (t *Tree) ToTomlString() (string, error) {
	b, err := t.Marshal()
	if err != nil {
		return "", err
	}
	return string(b), nil
}

to avoid duplication, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

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 {
pelletier marked this conversation as resolved.
Show resolved Hide resolved
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)
}
})
}
}
5 changes: 2 additions & 3 deletions tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,11 @@ func (t *Tree) WriteTo(w io.Writer) (int64, error) {
// Output spans multiple lines, and is suitable for ingest by a TOML parser.
// If the conversion cannot be performed, ToString returns a non-nil error.
func (t *Tree) ToTomlString() (string, error) {
var buf bytes.Buffer
_, err := t.WriteTo(&buf)
b, err := t.Marshal()
if err != nil {
return "", err
}
return buf.String(), nil
return string(b), nil
}

// String generates a human-readable representation of the current tree.
Expand Down