Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with interfaces using Viper language

Tags:

go

viper-go

I am building a little app using Viper and Cobra. At the moment, I have a yaml file like this:

hosts:
  - name: host1
    port: 90
    key: my_key
  - name: host2
    port: 90
    key: prompt

And I've read in the config file using Viper.

When I run viper.Get("hosts") it returns an interface (or a slice of interfaces?). This is the data structure I end up with:

([]interface {}) (len=2 cap=2) {
 (map[interface {}]interface {}) (len=3) {
  (string) (len=4) "name": (string) (len=20) "host1",
  (string) (len=4) "port": (int) 90,
  (string) (len=3) "key": (string) (len=6) "my_key"
 },
 (map[interface {}]interface {}) (len=3) {
  (string) (len=3) "key": (string) (len=6) "prompt",
  (string) (len=4) "name": (string) (len=20) "host2",
  (string) (len=4) "port": (int) 90
 }
}

What I'd like to do here is loop over each of the array elements and perform an operation using the values of name, port and key.

I'm completely new to interfaces in Golang, so this isn't very clear, and the literature on this is extremely confusing :(

Any help is appreciated.

like image 802
jaxxstorm Avatar asked Mar 12 '17 11:03

jaxxstorm


1 Answers

By defining the configuration file type and using viper.Unmarshal you can cast the interface to the specific type you need. Here is an example:

main.go

package main

import (
    "fmt"

    "github.com/spf13/viper"
)

type Host struct {
    Name string
    Port int
    Key  string
}

type Config struct {
    Hosts []Host
}

func main() {
    viper.AddConfigPath("./")
    viper.SetConfigName("test")
    viper.ReadInConfig()
    var config Config
    err := viper.Unmarshal(&config)
    if err != nil {
        panic("Unable to unmarshal config")
    }
    for _, h := range config.Hosts {
        fmt.Printf("Name: %s, Port: %d, Key: %s\n", h.Name, h.Port, h.Key)
    }
}

test.yml

hosts:
  - name: host1
    port: 90
    key: my_key
  - name: host2
    port: 90
    key: prompt

Run:

$ go run main.go
Name: host1, Port: 90, Key: my_key
Name: host2, Port: 90, Key: prompt

If you want to decode only some keys, not the entire configuration file, use viper.UnmarshalKey.

main.go

package main

import (
    "fmt"

    "github.com/spf13/viper"
)

type Host struct {
    Name string
    Port int
    Key  string
}

func main() {
    viper.AddConfigPath("./")
    viper.SetConfigName("test")
    viper.ReadInConfig()
    var hosts []Host
    err := viper.UnmarshalKey("hosts", &hosts)
    if err != nil {
        panic("Unable to unmarshal hosts")
    }
    for _, h := range hosts {
        fmt.Printf("Name: %s, Port: %d, Key: %s\n", h.Name, h.Port, h.Key)
    }
}

Run:

$ go run main.go
Name: host1, Port: 90, Key: my_key
Name: host2, Port: 90, Key: prompt
like image 100
minamijoyo Avatar answered Nov 18 '22 22:11

minamijoyo