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

Converting a tree to a string sometimes does not give the expected string #107

Closed
mike182uk opened this issue Nov 8, 2016 · 2 comments
Closed
Labels
bug Issues describing a bug in go-toml.

Comments

@mike182uk
Copy link

Hey there,

I'm having an issue when converting a map to a tree, then to a string. Using the following example:

package main

import (
	"fmt"
	"strings"
	toml "github.com/pelletier/go-toml"
)

func main(){
	var m1 = map[string]interface{}{
		"foo": 1,
		"bar": "baz",
		"qux": map[string]interface{}{
			"foo": 1,
			"bar": "baz",
		},
		"foobar": true,
	}

	// generate a tree from m1, then convert to a string

	tree := toml.TreeFromMap(m1)
	r := strings.NewReader(tree.ToString())

	// load in the generated toml string

	toml, err := toml.LoadReader(r)

	if err != nil {
		panic(err)
	}

	// convert to a map

	tmap := toml.ToMap()

	// debug

	fmt.Print(tree.ToString())
	fmt.Println(tmap)
}

I would expect to get:

foo = 1
bar = "baz"

[qux]
  foo = 1
  bar = "baz"

foobar = true
map[foo:1 bar:baz qux:map[foo:1 bar:baz] foobar:true]

But i seem to be getting back the toml in a random order, i.e

foobar = true
bar = "baz"
foo = 1

[qux]
  foo = 1
  bar = "baz"
map[foobar:true bar:baz foo:1 qux:map[bar:baz foo:1]]

The random order is fine, but every now and again, i get back something like:

bar = "baz"

[qux]
  foo = 1
  bar = "baz"
foobar = true
foo = 1
map[qux:map[foo:1 bar:baz foobar:true] foo:1 bar:baz]

When this string is turned back into a tree (and subsequently a map) foobar is now incorrectly part of qux.

I'm not sure if it's down to how i am using the library or if theres a bug somewhere when converting to a string. Any ideas?

@pelletier
Copy link
Owner

That's definitely a bug. We didn't think about that when we wrote the ToString implementation. The toTomlValue needs to be made smarter, and actually care about the orders of the keys, otherwise it ends up in situations you described.

@pelletier pelletier added the bug Issues describing a bug in go-toml. label Nov 23, 2016
@pelletier
Copy link
Owner

Fixed in #111. Feel free to reopen if it still does not work for you!

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

2 participants