This commit is contained in:
cmohrb 2020-01-25 10:56:22 -05:00 committed by GitHub
commit 7af5276560
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View file

@ -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)

View file

@ -1417,7 +1417,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)
}
@ -2006,3 +2013,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
}