-
-
Notifications
You must be signed in to change notification settings - Fork 530
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
Parsing different structures depending on a field #125
Comments
Use package main
import (
"fmt"
"log"
"github.com/BurntSushi/toml"
)
type ConfigHTTP struct {
URL string
ForceSSL bool
}
type ConfigIP struct {
Host string
Port int
}
const input0 = `
[connection]
type = "http"
[connection.config]
url = "https://foobar.com/api"
forcessl = true
`
const input1 = `
[connection]
type = "tcp"
[connection.config]
host = "12.12.12.12"
port = 1234
`
type connection struct {
Connection struct {
Type string
Config toml.Primitive
}
}
func parseConfig(s string) (interface{}, error) { // or return a more specific interface
var conn connection
md, err := toml.Decode(s, &conn)
if err != nil {
return nil, err
}
var conf interface{}
switch conn.Connection.Type {
case "http":
conf = new(ConfigHTTP)
case "tcp":
conf = new(ConfigIP)
default:
return nil, fmt.Errorf("unknown type %q", conn.Connection.Type)
}
if err := md.PrimitiveDecode(conn.Connection.Config, conf); err != nil {
return nil, err
}
return conf, nil
}
func main() {
for _, s := range []string{input0, input1} {
conf, err := parseConfig(s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", conf)
}
}
|
Thanks, I'm trying this approach now. I found out that, if the config section is not present,
Is this behavior expected? |
Interesting, that should work. I filed #126. In the meantime, you can work around this by using a |
Not needed anymore after workaround was proposed: BurntSushi#125 This reverts commit cb8d1d1.
Hey @rasky, I also fixed the empty Primitive case so now my original code works if you don't have any config section. |
Thanks! BTW I suggest you add this or a similar example of delayed encoding to the README |
Updates BurntSushi#125. Updates BurntSushi#126.
Hi, I would like to parse a TOML made like this:
or:
so basically the
config
table changes its keys depending onconnection.type
. It looks like this library allows me to define a structure like this:and then the
config
table is read into amap[string]interface{}
. Instead, I would be able to decode those configuration keys into their own structures, such as:So, I have two questions:
map[string]interface{}
into the actual structure? I couldn't find an API to do this.map[string]interface{}
is generated, but I ignore it. I then instantiate the correct configuration structure and put it into theconfig
field, eg:conn.Config = ConfigHttp{}
and proceed to run a second decoding, but unfortunately it seems to be ignored by the library, because the field's content is discarded and overwritten with a newmap[string]interface{}
instance. BTW this behavior doesn't match what I would expect; for instance, when I useencoding/json
to decode a JSON into a structure that contains aninterface{}
with a reference to an actual object, the library "sees through" theinterface{}
and decodes into the actual structure.Would you accept a patch that implements the roadmap presented in option 2? That is, if an
interface{}
field is non-nil and is a struct, decodes into it rather than just overwriting it with themap[string]interface{}
instance?The text was updated successfully, but these errors were encountered: