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

Can I get an array of tables using TOML? #213

Closed
TheHackerDev opened this issue Jul 26, 2016 · 3 comments
Closed

Can I get an array of tables using TOML? #213

TheHackerDev opened this issue Jul 26, 2016 · 3 comments

Comments

@TheHackerDev
Copy link

Here is an example config file in TOML:

[[user]]
name = "John"
nicknames = ["Johnny","Jim"]

[[user]]
name = "Kim"
nicknames = ["Kimmy"]

How can I get these values properly, so that I can iterate over them using something like a for x := range y statement, where y is a slice of users?

@bep
Copy link
Collaborator

bep commented Aug 5, 2016

This isn't a support forum. Please confer with the docs, or, maybe try the http://discuss.gohugo.io/ forum (Hugo uses viper).

@bep bep closed this as completed Aug 5, 2016
@krak3n
Copy link
Contributor

krak3n commented Nov 2, 2016

@bep tad harsh. I came here looking for the same thing since I couldn't see anything in the docs, I wouldn't think of going to hugo's support discuss for questions relating to this library.

Anyway, incase anyone comes here for this, given this example:

[lights]
address = "127.0.0.1"
[[lights.presets]]
relay = 1
name = "Night"
[[lights.presets]]
relay = 2
name = "Day"
[[lights.presets]]
relay = 3
name = "Low Intensity"
[[lights.presets]]
relay = 8
name = "Blackout"

You can access it with a type assertion on viper.Get since this returns an interface{}:

    presets, ok := viper.Get("lights.presets").([]map[string]interface{})
    if !ok {
        return nil, errors.New("incorrectly configured light presents")
    }

This will give you a slice of maps. The value of the map key is an interface{} so you'll need to type assert that as well to what you are expecting, like a slice of strings or what not.

@sgamerw
Copy link

sgamerw commented Mar 15, 2017

@krak3n

presets, ok := viper.Get("lights.presets").([]map[string]interface{})
if !ok {
    return nil, errors.New("incorrectly configured light presents")
}

The type assert always be false.

presets, ok := viper.Get("lights.presets").([]interface{}) // type assert to '[]interface{}'
if !ok {
    return nil, errors.New("incorrectly configured light presents")
}else{
    for _, table := range presets {
        if m, ok := table.(map[string]interface{}); ok { // type assert here
            fmt.Println(cast.ToInt(m["relay"])) // need cast, "github.com/spf13/cast"
        }
    }
}

This could be ok.

Another example:

[[lights]]
relay = 1
name = "Night"
[[lights]]
relay = 2
name = "Day"
[[lights]]
relay = 3
name = "Low Intensity"
[[lights]]
relay = 8
name = "Blackout"

Get lights directly.

presets, ok := viper.Get("lights").([]interface{})
if !ok {
    return nil, errors.New("incorrectly configured light presents")
}else{
    for _, table := range presets {
        if m, ok := table.(map[string]interface{}); ok { // type assert here
            fmt.Println(cast.ToInt(m["relay"])) // need cast, "github.com/spf13/cast"
            // cast 'name' too
        }
    }
}

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

4 participants