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

Encoder reorders arrays in structs #42

Closed
egonelbre opened this issue May 28, 2014 · 4 comments
Closed

Encoder reorders arrays in structs #42

egonelbre opened this issue May 28, 2014 · 4 comments

Comments

@egonelbre
Copy link

The toml.Encoder reorders arrays in structs. I can understand why the main values need to be above, but not why the arrays need to be reordered?

type Conf struct {
    V string
    A Alpha
    B []Beta
}

type Alpha struct{ V string }
type Beta struct{ V string }

func main() {
    conf := &Conf{
        V: "main",
        A: Alpha{"Alpha"},
        B: []Beta{{"1"}},
    }

    var buf bytes.Buffer
    toml.NewEncoder(&buf).Encode(conf)
    fmt.Println(buf.String())
}

This prints:

V = "main"

[[B]]
  V = "1"

[A]
  V = "Alpha"

Instead of the expected:

V = "main"

[A]
  V = "Alpha"

[[B]]
  V = "1"

(Also, as a side note, I tried to use an empty table name, should it work? e.g. [servers.], to get an entry of servers: { "": { ... }})

@BurntSushi
Copy link
Owner

It turns out this was fixed in commit 44d4af0, which in turn fixed #43. Killed two birds with one stone!

I've added your code as a regression test. Thanks!

@BurntSushi
Copy link
Owner

(Also, as a side note, I tried to use an empty table name, should it work? e.g. [servers.], to get an entry of servers: { "": { ... }})

As of now, no, it shouldn't work. And frankly, I hope it never works.

It's not specifically clarified in the TOML specification as far as I can tell, but I seem to have declared it invalid with tests in the toml-test test suite. I am really not inclined to change it.

(If you want to discuss it further in case I'm missing something, I suggest opening a new issue.)

@BurntSushi
Copy link
Owner

Oh. Did you mean, should the encoder work with empty table names? Indeed it shouldn't, but it does.

Now it doesn't. Fixed in commit 05d026b.

@egonelbre
Copy link
Author

Thanks.

I was trying to use the empty table name as a special name, but after thinking about it found a better solution. I just wasn't sure whether it was allowed with TOML. I can see either behavior acceptable: i.e. empty string is just another value, so it shouldn't be treated differently; vs. you shouldn't use empty table names in configuration, it causes confusion.

mitszo pushed a commit to accense/toml that referenced this issue Jul 19, 2016
This was actually fixed in commit 44d4af for BurntSushi#43, but this adds a
regression test from BurntSushi#42.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants