mirror of
https://github.com/spf13/viper
synced 2024-11-14 09:07:01 +00:00
fix: code optimization (#1557)
* fix: code optimization * fix: golangci-lint
This commit is contained in:
parent
c5102bdba0
commit
cb9b2bffc2
6 changed files with 15 additions and 15 deletions
|
@ -24,9 +24,9 @@ func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{},
|
||||||
}
|
}
|
||||||
for k, val := range m {
|
for k, val := range m {
|
||||||
fullKey := prefix + k
|
fullKey := prefix + k
|
||||||
switch val.(type) {
|
switch val := val.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
m2 = val.(map[string]interface{})
|
m2 = val
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
m2 = cast.ToStringMap(val)
|
m2 = cast.ToStringMap(val)
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -57,9 +57,9 @@ func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{},
|
||||||
}
|
}
|
||||||
for k, val := range m {
|
for k, val := range m {
|
||||||
fullKey := prefix + k
|
fullKey := prefix + k
|
||||||
switch val.(type) {
|
switch val := val.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
m2 = val.(map[string]interface{})
|
m2 = val
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
m2 = cast.ToStringMap(val)
|
m2 = cast.ToStringMap(val)
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -57,9 +57,9 @@ func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{},
|
||||||
}
|
}
|
||||||
for k, val := range m {
|
for k, val := range m {
|
||||||
fullKey := prefix + k
|
fullKey := prefix + k
|
||||||
switch val.(type) {
|
switch val := val.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
m2 = val.(map[string]interface{})
|
m2 = val
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
m2 = cast.ToStringMap(val)
|
m2 = cast.ToStringMap(val)
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -156,11 +156,11 @@ func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string,
|
||||||
}
|
}
|
||||||
|
|
||||||
// deep scan of the map to get the final value
|
// deep scan of the map to get the final value
|
||||||
switch val.(type) {
|
switch val := val.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
m = cast.ToStringMap(val)
|
m = cast.ToStringMap(val)
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
m = val.(map[string]interface{})
|
m = val
|
||||||
default:
|
default:
|
||||||
assert.Fail(fmt.Sprintf("%s is not a map[string]interface{}", ms))
|
assert.Fail(fmt.Sprintf("%s is not a map[string]interface{}", ms))
|
||||||
return
|
return
|
||||||
|
|
6
util.go
6
util.go
|
@ -70,17 +70,17 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
func insensitiviseVal(val interface{}) interface{} {
|
func insensitiviseVal(val interface{}) interface{} {
|
||||||
switch val.(type) {
|
switch v := val.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
// nested map: cast and recursively insensitivise
|
// nested map: cast and recursively insensitivise
|
||||||
val = cast.ToStringMap(val)
|
val = cast.ToStringMap(val)
|
||||||
insensitiviseMap(val.(map[string]interface{}))
|
insensitiviseMap(val.(map[string]interface{}))
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
// nested map: recursively insensitivise
|
// nested map: recursively insensitivise
|
||||||
insensitiviseMap(val.(map[string]interface{}))
|
insensitiviseMap(v)
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
// nested array: recursively insensitivise
|
// nested array: recursively insensitivise
|
||||||
insensitiveArray(val.([]interface{}))
|
insensitiveArray(v)
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
8
viper.go
8
viper.go
|
@ -672,13 +672,13 @@ func (v *Viper) searchMap(source map[string]interface{}, path []string) interfac
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nested case
|
// Nested case
|
||||||
switch next.(type) {
|
switch next := next.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
return v.searchMap(cast.ToStringMap(next), path[1:])
|
return v.searchMap(cast.ToStringMap(next), path[1:])
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
// Type assertion is safe here since it is only reached
|
// Type assertion is safe here since it is only reached
|
||||||
// if the type of `next` is the same as the type being asserted
|
// if the type of `next` is the same as the type being asserted
|
||||||
return v.searchMap(next.(map[string]interface{}), path[1:])
|
return v.searchMap(next, path[1:])
|
||||||
default:
|
default:
|
||||||
// got a value but nested key expected, return "nil" for not found
|
// got a value but nested key expected, return "nil" for not found
|
||||||
return nil
|
return nil
|
||||||
|
@ -2057,9 +2057,9 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac
|
||||||
}
|
}
|
||||||
for k, val := range m {
|
for k, val := range m {
|
||||||
fullKey := prefix + k
|
fullKey := prefix + k
|
||||||
switch val.(type) {
|
switch val := val.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
m2 = val.(map[string]interface{})
|
m2 = val
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
m2 = cast.ToStringMap(val)
|
m2 = cast.ToStringMap(val)
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue