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 empty bytes without any errors #295

Closed
yukithm opened this issue Aug 29, 2019 · 3 comments
Closed

tree.Marshal() returns empty bytes without any errors #295

yukithm opened this issue Aug 29, 2019 · 3 comments
Labels
bug Issues describing a bug in go-toml.

Comments

@yukithm
Copy link

yukithm commented Aug 29, 2019

Describe the bug
tree.Marshal() returns empty bytes without any errors.

To Reproduce

package main

import (
	"fmt"
	"os"

	"github.com/pelletier/go-toml"
)

func main() {
	data := map[string]interface{}{
		"section": map[string]string{
			"name": "foo",
		},
	}

	tree, err := toml.TreeFromMap(data)
	if err != nil {
		panic(err)
	}

	fmt.Println(tree.Marshal()) //=> [] <nil>

	fmt.Println(tree.String())       // worked perfectly
	fmt.Println(tree.ToTomlString()) // worked perfectly
	tree.WriteTo(os.Stdout)          // worked perfectly
}

Expected behavior
It returns TOML content.

[section]
  name = "foo"

Versions

  • go-toml: v1.4.1-0.20190827035702-3ded2e09ee1b
  • go: go1.12.9 darwin/amd64
  • operating system: macOS
@pelletier pelletier added the bug Issues describing a bug in go-toml. label Sep 6, 2019
@pelletier
Copy link
Owner

Thanks for the bug report! That definitely looks wrong.

@mbialon
Copy link
Contributor

mbialon commented Sep 24, 2019

I don't understand why the Marshal method tries to process the Tree struct. The struct doesn't have exported fields so Marshal returns an empty result.

func (t *Tree) Marshal() ([]byte, error) {
	var buf bytes.Buffer
	err := NewEncoder(&buf).Encode(t)
	return buf.Bytes(), err
}

What do you think about replacing the encoding part with tree.WriteTo, similarly to ToTomlString?

func (t *Tree) Marshal() ([]byte, error) {
	var buf bytes.Buffer
	_, err := t.WriteTo(&buf)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

@pelletier
Copy link
Owner

That makes sense. Looks like Tree.Marshal() should just call ToTomlString and cast to bytes.

This is the kind of function that I regret having published. There are 4 ways to emit a TOML document:

  • Tree.ToTomlString()
  • Tree.Marshal()
  • Marshal()
  • Encoder.Encode()

The last two are here to mirror the standard json library. The first two serve the same purpose (turn a tree into a TOML string/bytes). Clearly there should be just one.

mbialon added a commit to mbialon/go-toml that referenced this issue Sep 25, 2019
The Tree.Marshal tried to marshal the Tree struct itself rather than the nodes being part of the tree.

Fixes pelletier#295
mbialon added a commit to mbialon/go-toml that referenced this issue Sep 26, 2019
The Tree.Marshal tried to marshal the Tree struct itself rather than the nodes being part of the tree.

Fixes pelletier#295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues describing a bug in go-toml.
Projects
None yet
Development

No branches or pull requests

3 participants