Merge pull request #1 from lnashier/swu1/removeInsensitiveMethods

remove insensitive methods
This commit is contained in:
lnashier 2018-04-09 15:30:19 -07:00 committed by GitHub
commit 74d425cf73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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{})
for key, val := range m {
lkey := strings.ToLower(key)
lkey := key
switch v := val.(type) {
case map[interface{}]interface{}:
nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
@ -66,28 +66,6 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
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 {
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
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]
if ok {
@ -611,7 +611,7 @@ func GetViper() *Viper {
// Get returns an interface. For a specific value use one of the Get____ methods.
func Get(key string) interface{} { return v.Get(key) }
func (v *Viper) Get(key string) interface{} {
lcaseKey := strings.ToLower(key)
lcaseKey := key
val := v.find(lcaseKey)
if val == nil {
return nil
@ -747,8 +747,6 @@ func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
return err
}
v.insensitiviseMaps()
return nil
}
@ -762,8 +760,6 @@ func (v *Viper) Unmarshal(rawVal interface{}) error {
return err
}
v.insensitiviseMaps()
return nil
}
@ -802,8 +798,6 @@ func (v *Viper) UnmarshalExact(rawVal interface{}) error {
return err
}
v.insensitiviseMaps()
return nil
}
@ -848,7 +842,7 @@ func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
if flag == nil {
return fmt.Errorf("flag for %q is nil", key)
}
v.pflags[strings.ToLower(key)] = flag
v.pflags[key] = flag
return nil
}
@ -863,7 +857,7 @@ func (v *Viper) BindEnv(input ...string) error {
return fmt.Errorf("BindEnv missing key to bind to")
}
key = strings.ToLower(input[0])
key = input[0]
if len(input) == 1 {
envkey = v.mergeWithEnvPrefix(key)
@ -1013,7 +1007,7 @@ func readAsCSV(val string) ([]string, error) {
// IsSet is case-insensitive for a key.
func IsSet(key string) bool { return v.IsSet(key) }
func (v *Viper) IsSet(key string) bool {
lcaseKey := strings.ToLower(key)
lcaseKey := key
val := v.find(lcaseKey)
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
func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
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) {
alias = strings.ToLower(alias)
if alias != key && alias != v.realKey(key) {
_, 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 (v *Viper) SetDefault(key string, value interface{}) {
// If alias passed in, then set the proper default
key = v.realKey(strings.ToLower(key))
key = v.realKey(key)
value = toCaseInsensitiveValue(value)
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])
// 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 (v *Viper) Set(key string, value interface{}) {
// If alias passed in, then set the proper override
key = v.realKey(strings.ToLower(key))
key = v.realKey(key)
value = toCaseInsensitiveValue(value)
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])
// set innermost value
@ -1312,14 +1306,13 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
value, _ := v.properties.Get(key)
// recursively build nested maps
path := strings.Split(key, ".")
lastKey := strings.ToLower(path[len(path)-1])
lastKey := path[len(path)-1]
deepestMap := deepSearch(c, path[0:len(path)-1])
// set innermost value
deepestMap[lastKey] = value
}
}
insensitiviseMap(c)
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 {
lk := strings.ToLower(k)
for mk := range m {
lmk := strings.ToLower(mk)
if lmk == lk {
if mk == k {
return mk
}
}
@ -1501,13 +1492,6 @@ func (v *Viper) WatchRemoteConfigOnChannel() error {
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.
func (v *Viper) getKeyValueConfig() error {
if RemoteConfig == nil {
@ -1623,7 +1607,7 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac
m2 = cast.ToStringMap(val)
default:
// immediate value
shadow[strings.ToLower(fullKey)] = true
shadow[fullKey] = true
continue
}
// recursively merge to shadow map
@ -1649,7 +1633,7 @@ outer:
}
}
// add key
shadow[strings.ToLower(k)] = true
shadow[k] = true
}
return shadow
}
@ -1667,7 +1651,7 @@ func (v *Viper) AllSettings() map[string]interface{} {
continue
}
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])
// set innermost value
deepestMap[lastKey] = value