naoina/toml

[Servers] should be a map, not a struct

Closed this issue · 4 comments

Hi,

I really like your toml parser very much, as it is really simple to use.
I have just this issue that bothers me:

In https://github.com/toml-lang/toml, it is stated:

"Tables (also known as hash tables or dictionaries) are collections of key/value pairs."

Many times, the keys of the map can be represented as fields in a struct.

type tomlConfig struct {
    ...
    Servers struct {  <------ in your example, Servers is a struct
        Alpha Server
        Beta  Server
    }
    ...
}

type Server struct {
    IP string
    DC string
}

You consider that "Servers" is a struct, with "Alpha" and "Beta" as field names.

But in this case, how do you add new servers in the TOML config files ?
If you add more servers, like [servers.gamma], it means that you must change the definition of the struct in your program, and you must know the names of all servers beforehand:

type tomlConfig struct {
    ...
    Servers struct {
        Alpha Server
        Beta  Server
        Gamma Server
    }
    ...
}

I think that "Alpha" and "Beta" are not field names of a struct, but keys of a map.

type tomlConfig struct {
    ...
    Servers map[string]Server     <-------- I think that Servers should be a map, and not a struct
    ...
}

type Server struct {
    IP string
    DC string
}

It would be good if the parser could write in map, as well as in struct.

What is your optinion ?

That's true. I will fix it.

I've fix and improve it in commit d1af5be.

@rin01 Thank you for report the issue!

It works perfectly, now ;-))) ありがとうございます !!!