Adding the ability to get into a struct
diff --git a/viper.go b/viper.go
index e88dec9..a56472f 100644
--- a/viper.go
+++ b/viper.go
@@ -20,6 +20,7 @@
"github.com/BurntSushi/toml"
"github.com/kr/pretty"
+ "github.com/mitchellh/mapstructure"
"github.com/spf13/cast"
jww "github.com/spf13/jwalterweatherman"
"gopkg.in/yaml.v1"
@@ -93,6 +94,28 @@
return cast.ToStringMapString(Get(key))
}
+// takes a map and uses reflection to convert it into the given Go native structure.
+// rawVal must be a pointer to a struct.
+func GetIntoStruct(key string, rawVal interface{}) error {
+ return mapstructure.Decode(Get(key), rawVal)
+}
+
+func GetAllIntoStruct(rawVal interface{}) (err error) {
+ err = mapstructure.Decode(defaults, rawVal)
+ if err != nil {
+ return
+ }
+ err = mapstructure.Decode(config, rawVal)
+ if err != nil {
+ return
+ }
+ err = mapstructure.Decode(override, rawVal)
+ if err != nil {
+ return
+ }
+ return
+}
+
func find(key string) interface{} {
var val interface{}
var exists bool