From 32aa3191878d98bdb37f331dff0856f553d756c9 Mon Sep 17 00:00:00 2001 From: cmohrb Date: Fri, 13 Jul 2018 15:55:31 +0200 Subject: [PATCH 1/3] if config type is already set, ignore file extension and use the saved config type e.g. if viper.SetConfigType("yaml") was previously called, yaml will be used as config format, even if the file is called XXX.conf, instead of XXXX.yaml --- viper.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 907a102..db2d6d6 100644 --- a/viper.go +++ b/viper.go @@ -1245,7 +1245,14 @@ func (v *Viper) writeConfig(filename string, force bool) error { if len(ext) <= 1 { return fmt.Errorf("Filename: %s requires valid extension.", filename) } - configType := ext[1:] + + var configType string + if v.configType == "" { + configType = ext[1:] + } else { + configType = v.configType + } + if !stringInSlice(configType, SupportedExts) { return UnsupportedConfigError(configType) } From 8e90aaafac34db186dc5628fc83c62595c545a58 Mon Sep 17 00:00:00 2001 From: cmohrb Date: Tue, 21 Aug 2018 18:23:30 +0200 Subject: [PATCH 2/3] add method subSlice to get an array/table/list of config options as slice of "sub-vipers" --- viper.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/viper.go b/viper.go index db2d6d6..ca02fa6 100644 --- a/viper.go +++ b/viper.go @@ -1782,3 +1782,26 @@ func (v *Viper) Debug() { fmt.Printf("Config:\n%#v\n", v.config) fmt.Printf("Defaults:\n%#v\n", v.defaults) } + +func SubSlice(key string) (out []*Viper) { return v.SubSlice(key) } +func (v *Viper) SubSlice(key string) (out []*Viper) { + data := v.Get(key) + + if data == nil { + return nil + } + + if reflect.TypeOf(data).Kind() != reflect.Slice { + return nil + } + + for _, elem := range data.([]interface{}) { + subv := New() + if reflect.TypeOf(elem).Kind() == reflect.Map { + subv.config = cast.ToStringMap(elem) + out = append(out, subv) + } + } + + return +} From 4f738d6f3f12193b509ba28f6dd79766be34944e Mon Sep 17 00:00:00 2001 From: cmohrb Date: Wed, 22 Aug 2018 11:28:42 +0200 Subject: [PATCH 3/3] insensitivise slices of maps when reading from config file --- util.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/util.go b/util.go index 952cad4..76b147f 100644 --- a/util.go +++ b/util.go @@ -76,6 +76,13 @@ func insensitiviseMap(m map[string]interface{}) { case map[string]interface{}: // nested map: recursively insensitivise insensitiviseMap(val.(map[string]interface{})) + case []interface{}: + for _, x := range val.([]interface{}) { + y, ok := x.(map[string]interface{}) + if ok { + insensitiviseMap(y) + } + } } lower := strings.ToLower(key)