Merge pull request #1 from xurwxj/f_lower_nzg

去掉lower
This commit is contained in:
Victor 2022-09-29 18:07:39 +08:00 committed by GitHub
commit 01e104428a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 32 deletions

15
util.go
View file

@ -51,14 +51,13 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
nm := make(map[string]interface{})
for key, val := range m {
lkey := strings.ToLower(key)
switch v := val.(type) {
case map[interface{}]interface{}:
nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
nm[key] = copyAndInsensitiviseMap(cast.ToStringMap(v))
case map[string]interface{}:
nm[lkey] = copyAndInsensitiviseMap(v)
nm[key] = copyAndInsensitiviseMap(v)
default:
nm[lkey] = v
nm[key] = v
}
}
@ -76,14 +75,8 @@ func insensitiviseMap(m 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
m[key] = val
}
}

View file

@ -477,7 +477,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 {
@ -632,15 +632,14 @@ func Get(key string) interface{} { return v.Get(key) }
//
// Get returns an interface. For a specific value use one of the Get____ methods.
func (v *Viper) Get(key string) interface{} {
lcaseKey := strings.ToLower(key)
val := v.find(lcaseKey, true)
val := v.find(key, true)
if val == nil {
return nil
}
if v.typeByDefValue {
valType := val
path := strings.Split(lcaseKey, v.keyDelim)
path := strings.Split(key, v.keyDelim)
defVal := v.searchMap(v.defaults, path)
if defVal != nil {
valType = defVal
@ -1015,7 +1014,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
}
@ -1035,7 +1034,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)
@ -1232,8 +1231,7 @@ func IsSet(key string) bool { return v.IsSet(key) }
// IsSet checks to see if the key has been set in any of the data locations.
// IsSet is case-insensitive for a key.
func (v *Viper) IsSet(key string) bool {
lcaseKey := strings.ToLower(key)
val := v.find(lcaseKey, false)
val := v.find(key, false)
return val != nil
}
@ -1266,11 +1264,10 @@ func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
// RegisterAlias provide another accessor for the same key.
// This enables one to change a name without breaking the application
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]
@ -1332,11 +1329,11 @@ func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
// Default only used when no value is provided by the user via flag, config or ENV.
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
@ -1355,11 +1352,11 @@ func Set(key string, value interface{}) { v.Set(key, value) }
// flags, config file, ENV, default, or key/value store.
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
@ -1556,7 +1553,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
buf := new(bytes.Buffer)
buf.ReadFrom(in)
switch strings.ToLower(v.getConfigType()) {
switch v.getConfigType() {
case "json":
if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
@ -1589,10 +1586,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
}
}
@ -1742,7 +1737,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
@ -1768,7 +1763,7 @@ outer:
}
}
// add key
shadow[strings.ToLower(k)] = true
shadow[k] = true
}
return shadow
}
@ -1788,7 +1783,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