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

Marshalling fails for nested arrays of tables #369

Closed
x-hgg-x opened this issue Apr 24, 2020 · 5 comments · Fixed by #395
Closed

Marshalling fails for nested arrays of tables #369

x-hgg-x opened this issue Apr 24, 2020 · 5 comments · Fixed by #395
Labels
bug Issues describing a bug in go-toml.

Comments

@x-hgg-x
Copy link
Contributor

x-hgg-x commented Apr 24, 2020

Describe the bug
Marshalling produces an error:

marshalling error: unsupported value type *toml.Tree: key = "Control"

To Reproduce

tree, err := toml.Load(`
combinations = [
	[{ key = "Control" }, { key = "Q" }],
	[{ key = "Escape" }],
]
`)

if err != nil {
	fmt.Println("loading error:", err)
}

_, err = tree.Marshal()
if err != nil {
	fmt.Println("marshalling error:", err)
}

Expected behavior
There is no error when loading TOML data, so marshalling should work.

Versions

  • go-toml: last version of master (323fe5d)
  • go: 1.14
  • operating system: Linux

Additional context
Unmarshalling works:

type Button struct {
	Key string
}

type Action struct {
	Combinations [][]Button
}

var action Action
err = tree.Unmarshal(&action)
if err != nil {
	fmt.Println("unmarshalling error:", err)
}

fmt.Println(action)

Output:

{[[{Control} {Q}] [{Escape}]]}
@pelletier pelletier added the bug Issues describing a bug in go-toml. label Apr 25, 2020
@pelletier
Copy link
Owner

Thank you for the bug report! That's definitely wrong.

@x-hgg-x
Copy link
Contributor Author

x-hgg-x commented Apr 25, 2020

For information, the BurntSushi library also fails with this error:
toml: TOML array element cannot contain a table.

I think nested arrays of table might only be expressed with inline tables in TOML (see toml-lang/toml#353 (comment)).

@pelletier
Copy link
Owner

The latest spec calls out

points = [ { x = 1, y = 2, z = 3 },
           { x = 7, y = 8, z = 9 },
           { x = 2, y = 4, z = 8 } ]

And that arrays can contain any other type including arrays. I don’t see why your example shouldn’t work.

@pelletier
Copy link
Owner

I wonder if your changes at f1ba638 fix this 🤔

@x-hgg-x
Copy link
Contributor Author

x-hgg-x commented May 4, 2020

My changes don't fix this. Since nested arrays of tables can only be represented as inline tables, we should add an inline tree representation in the tomlValueStringRepresentation method. I will try to make a PR for this.

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

Successfully merging a pull request may close this issue.

2 participants