remove insensitive methods

This commit is contained in:
swu1 2018-04-09 14:56:51 -07:00
parent 8dc2790b02
commit a5650e80e9
2 changed files with 17 additions and 55 deletions

24
util.go
View file

@ -52,7 +52,7 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
nm := make(map[string]interface{}) nm := make(map[string]interface{})
for key, val := range m { for key, val := range m {
lkey := strings.ToLower(key) lkey := key
switch v := val.(type) { switch v := val.(type) {
case map[interface{}]interface{}: case map[interface{}]interface{}:
nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v)) nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
@ -66,28 +66,6 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
return nm return nm
} }
func insensitiviseMap(m map[string]interface{}) {
for key, val := range m {
switch val.(type) {
case map[interface{}]interface{}:
// nested map: cast and recursively insensitivise
val = cast.ToStringMap(val)
insensitiviseMap(val.(map[string]interface{}))
case map[string]interface{}:
// nested map: recursively insensitivise
insensitiviseMap(val.(map[string]interface{}))
}
lower := strings.ToLower(key)
if key != lower {
// remove old key (not lower-cased)
delete(m, key)
}
// update map
m[lower] = val
}
}
func absPathify(inPath string) string { func absPathify(inPath string) string {
jww.INFO.Println("Trying to resolve absolute path to", inPath) jww.INFO.Println("Trying to resolve absolute path to", inPath)

View file

@ -481,7 +481,7 @@ func (v *Viper) searchMapWithPathPrefixes(source map[string]interface{}, path []
// search for path prefixes, starting from the longest one // search for path prefixes, starting from the longest one
for i := len(path); i > 0; i-- { for i := len(path); i > 0; i-- {
prefixKey := strings.ToLower(strings.Join(path[0:i], v.keyDelim)) prefixKey := strings.Join(path[0:i], v.keyDelim)
next, ok := source[prefixKey] next, ok := source[prefixKey]
if ok { if ok {
@ -611,7 +611,7 @@ func GetViper() *Viper {
// Get returns an interface. For a specific value use one of the Get____ methods. // Get returns an interface. For a specific value use one of the Get____ methods.
func Get(key string) interface{} { return v.Get(key) } func Get(key string) interface{} { return v.Get(key) }
func (v *Viper) Get(key string) interface{} { func (v *Viper) Get(key string) interface{} {
lcaseKey := strings.ToLower(key) lcaseKey := key
val := v.find(lcaseKey) val := v.find(lcaseKey)
if val == nil { if val == nil {
return nil return nil
@ -747,8 +747,6 @@ func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
return err return err
} }
v.insensitiviseMaps()
return nil return nil
} }
@ -762,8 +760,6 @@ func (v *Viper) Unmarshal(rawVal interface{}) error {
return err return err
} }
v.insensitiviseMaps()
return nil return nil
} }
@ -802,8 +798,6 @@ func (v *Viper) UnmarshalExact(rawVal interface{}) error {
return err return err
} }
v.insensitiviseMaps()
return nil return nil
} }
@ -848,7 +842,7 @@ func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
if flag == nil { if flag == nil {
return fmt.Errorf("flag for %q is nil", key) return fmt.Errorf("flag for %q is nil", key)
} }
v.pflags[strings.ToLower(key)] = flag v.pflags[key] = flag
return nil return nil
} }
@ -863,7 +857,7 @@ func (v *Viper) BindEnv(input ...string) error {
return fmt.Errorf("BindEnv missing key to bind to") return fmt.Errorf("BindEnv missing key to bind to")
} }
key = strings.ToLower(input[0]) key = input[0]
if len(input) == 1 { if len(input) == 1 {
envkey = v.mergeWithEnvPrefix(key) envkey = v.mergeWithEnvPrefix(key)
@ -1013,7 +1007,7 @@ func readAsCSV(val string) ([]string, error) {
// IsSet is case-insensitive for a key. // IsSet is case-insensitive for a key.
func IsSet(key string) bool { return v.IsSet(key) } func IsSet(key string) bool { return v.IsSet(key) }
func (v *Viper) IsSet(key string) bool { func (v *Viper) IsSet(key string) bool {
lcaseKey := strings.ToLower(key) lcaseKey := key
val := v.find(lcaseKey) val := v.find(lcaseKey)
return val != nil return val != nil
} }
@ -1037,11 +1031,11 @@ func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
// This enables one to change a name without breaking the application // This enables one to change a name without breaking the application
func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) } func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
func (v *Viper) RegisterAlias(alias string, key string) { func (v *Viper) RegisterAlias(alias string, key string) {
v.registerAlias(alias, strings.ToLower(key)) v.registerAlias(alias, key)
} }
func (v *Viper) registerAlias(alias string, key string) { func (v *Viper) registerAlias(alias string, key string) {
alias = strings.ToLower(alias)
if alias != key && alias != v.realKey(key) { if alias != key && alias != v.realKey(key) {
_, exists := v.aliases[alias] _, exists := v.aliases[alias]
@ -1097,11 +1091,11 @@ func (v *Viper) InConfig(key string) bool {
func SetDefault(key string, value interface{}) { v.SetDefault(key, value) } func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
func (v *Viper) SetDefault(key string, value interface{}) { func (v *Viper) SetDefault(key string, value interface{}) {
// If alias passed in, then set the proper default // If alias passed in, then set the proper default
key = v.realKey(strings.ToLower(key)) key = v.realKey(key)
value = toCaseInsensitiveValue(value) value = toCaseInsensitiveValue(value)
path := strings.Split(key, v.keyDelim) path := strings.Split(key, v.keyDelim)
lastKey := strings.ToLower(path[len(path)-1]) lastKey := path[len(path)-1]
deepestMap := deepSearch(v.defaults, path[0:len(path)-1]) deepestMap := deepSearch(v.defaults, path[0:len(path)-1])
// set innermost value // set innermost value
@ -1115,11 +1109,11 @@ func (v *Viper) SetDefault(key string, value interface{}) {
func Set(key string, value interface{}) { v.Set(key, value) } func Set(key string, value interface{}) { v.Set(key, value) }
func (v *Viper) Set(key string, value interface{}) { func (v *Viper) Set(key string, value interface{}) {
// If alias passed in, then set the proper override // If alias passed in, then set the proper override
key = v.realKey(strings.ToLower(key)) key = v.realKey(key)
value = toCaseInsensitiveValue(value) value = toCaseInsensitiveValue(value)
path := strings.Split(key, v.keyDelim) path := strings.Split(key, v.keyDelim)
lastKey := strings.ToLower(path[len(path)-1]) lastKey := path[len(path)-1]
deepestMap := deepSearch(v.override, path[0:len(path)-1]) deepestMap := deepSearch(v.override, path[0:len(path)-1])
// set innermost value // set innermost value
@ -1312,14 +1306,13 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
value, _ := v.properties.Get(key) value, _ := v.properties.Get(key)
// recursively build nested maps // recursively build nested maps
path := strings.Split(key, ".") path := strings.Split(key, ".")
lastKey := strings.ToLower(path[len(path)-1]) lastKey := path[len(path)-1]
deepestMap := deepSearch(c, path[0:len(path)-1]) deepestMap := deepSearch(c, path[0:len(path)-1])
// set innermost value // set innermost value
deepestMap[lastKey] = value deepestMap[lastKey] = value
} }
} }
insensitiviseMap(c)
return nil return nil
} }
@ -1390,10 +1383,8 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
} }
func keyExists(k string, m map[string]interface{}) string { func keyExists(k string, m map[string]interface{}) string {
lk := strings.ToLower(k)
for mk := range m { for mk := range m {
lmk := strings.ToLower(mk) if mk == k {
if lmk == lk {
return mk return mk
} }
} }
@ -1501,13 +1492,6 @@ func (v *Viper) WatchRemoteConfigOnChannel() error {
return v.watchKeyValueConfigOnChannel() return v.watchKeyValueConfigOnChannel()
} }
func (v *Viper) insensitiviseMaps() {
insensitiviseMap(v.config)
insensitiviseMap(v.defaults)
insensitiviseMap(v.override)
insensitiviseMap(v.kvstore)
}
// Retrieve the first found remote configuration. // Retrieve the first found remote configuration.
func (v *Viper) getKeyValueConfig() error { func (v *Viper) getKeyValueConfig() error {
if RemoteConfig == nil { if RemoteConfig == nil {
@ -1623,7 +1607,7 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac
m2 = cast.ToStringMap(val) m2 = cast.ToStringMap(val)
default: default:
// immediate value // immediate value
shadow[strings.ToLower(fullKey)] = true shadow[fullKey] = true
continue continue
} }
// recursively merge to shadow map // recursively merge to shadow map
@ -1649,7 +1633,7 @@ outer:
} }
} }
// add key // add key
shadow[strings.ToLower(k)] = true shadow[k] = true
} }
return shadow return shadow
} }
@ -1667,7 +1651,7 @@ func (v *Viper) AllSettings() map[string]interface{} {
continue continue
} }
path := strings.Split(k, v.keyDelim) path := strings.Split(k, v.keyDelim)
lastKey := strings.ToLower(path[len(path)-1]) lastKey := path[len(path)-1]
deepestMap := deepSearch(m, path[0:len(path)-1]) deepestMap := deepSearch(m, path[0:len(path)-1])
// set innermost value // set innermost value
deepestMap[lastKey] = value deepestMap[lastKey] = value