spf13 / viper

Go configuration with fangs
MIT License
27.03k stars 2.01k forks source link

`Sub` operator with array #504

Open PeerXu opened 6 years ago

PeerXu commented 6 years ago
package main

import (
    "bytes"
    "fmt"

    "github.com/spf13/viper"
)

func main() {
    var data = []byte(`
devices:
- name: rpi 0
- name: rpi 2
- name: rpi 3
`)

    v := viper.New()
    v.SetConfigType("yaml")
    v.ReadConfig(bytes.NewBuffer(data))
    dev1 := v.Sub("devices[1]")
    fmt.Println(dev1.GetString("name"))
}

There is my code, I want to get the second device into *viper.Viper object (In fact, need all devices into *viper.Viper).

But, got error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x135195a]

goroutine 1 [running]:
github.com/spf13/viper.(*Viper).find(0x0, 0x1425b36, 0x4, 0x4, 0x1730d90)
        /Users/peer/.golang/src/github.com/spf13/viper/viper.go:889 +0x3a
github.com/spf13/viper.(*Viper).Get(0x0, 0x1425b36, 0x4, 0x0, 0x0)
        /Users/peer/.golang/src/github.com/spf13/viper/viper.go:615 +0x7c
github.com/spf13/viper.(*Viper).GetString(0x0, 0x1425b36, 0x4, 0x0, 0x0)
        /Users/peer/.golang/src/github.com/spf13/viper/viper.go:670 +0x3f
main.main()
        /tmp/test.go:22 +0x229
exit status 2
cflee commented 5 years ago

This seems to be related to #171

andig commented 4 years ago

509 should provide a solution using SubList