Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnmarshalKey normalizes all map keys to lower case #545

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)

Expand Down
48 changes: 16 additions & 32 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -747,8 +747,6 @@ func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
return err
}

v.insensitiviseMaps()

return nil
}

Expand All @@ -762,8 +760,6 @@ func (v *Viper) Unmarshal(rawVal interface{}) error {
return err
}

v.insensitiviseMaps()

return nil
}

Expand Down Expand Up @@ -802,8 +798,6 @@ func (v *Viper) UnmarshalExact(rawVal interface{}) error {
return err
}

v.insensitiviseMaps()

return nil
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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]

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -1649,7 +1633,7 @@ outer:
}
}
// add key
shadow[strings.ToLower(k)] = true
shadow[k] = true
}
return shadow
}
Expand All @@ -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
Expand Down