support unregister alias

This commit is contained in:
yanyandenuonuo 2018-12-28 17:41:52 +08:00
parent 6d33b5a963
commit 3421df1f9b
3 changed files with 27 additions and 10 deletions

1
.gitignore vendored
View file

@ -24,6 +24,7 @@ _testmain.go
*.bench
.vscode
.idea
# exclude dependencies in the `/vendor` folder
vendor

View file

@ -1143,14 +1143,30 @@ func (v *Viper) registerAlias(alias string, key string) {
}
func (v *Viper) realKey(key string) string {
newkey, exists := v.aliases[key]
newKey, exists := v.aliases[key]
if exists {
jww.DEBUG.Println("Alias", key, "to", newkey)
return v.realKey(newkey)
jww.DEBUG.Println("Alias", key, "to", newKey)
return v.realKey(newKey)
}
return key
}
// Remove the alias.
// This enables one to remove a alias and then override the alias
func UnregisterAlias(alias string) { v.UnregisterAlias(alias) }
func (v *Viper) UnregisterAlias(alias string) {
v.unregisterAlias(alias)
}
func (v *Viper) unregisterAlias(alias string) {
alias = strings.ToLower(alias)
if _, exists := v.aliases[alias]; exists {
delete(v.aliases, alias)
} else {
jww.DEBUG.Println("Alias already unregister alias", alias)
}
}
// InConfig checks to see if the given key (or an alias) is in the config file.
func InConfig(key string) bool { return v.InConfig(key) }
func (v *Viper) InConfig(key string) bool {
@ -1715,7 +1731,7 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac
func (v *Viper) mergeFlatMap(shadow map[string]bool, m map[string]interface{}) map[string]bool {
// scan keys
outer:
for k, _ := range m {
for k := range m {
path := strings.Split(k, v.keyDelim)
// scan intermediate paths
var parentKey string

View file

@ -472,7 +472,7 @@ func TestAllKeys(t *testing.T) {
ks := sort.StringSlice{"title", "newkey", "owner.organization", "owner.dob", "owner.bio", "name", "beard", "ppu", "batters.batter", "hobbies", "clothing.jacket", "clothing.trousers", "clothing.pants.size", "age", "hacker", "id", "type", "eyes", "p_id", "p_ppu", "p_batters.batter.type", "p_type", "p_name", "foos"}
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[string]interface{}{"trousers": "denim", "jacket": "leather", "pants": map[string]interface{}{"size": "large"}}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters": map[string]interface{}{"batter": map[string]interface{}{"type": "Regular"}}, "p_type": "donut", "foos": []map[string]interface{}{map[string]interface{}{"foo": []map[string]interface{}{map[string]interface{}{"key": 1}, map[string]interface{}{"key": 2}, map[string]interface{}{"key": 3}, map[string]interface{}{"key": 4}}}}}
all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[string]interface{}{"trousers": "denim", "jacket": "leather", "pants": map[string]interface{}{"size": "large"}}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters": map[string]interface{}{"batter": map[string]interface{}{"type": "Regular"}}, "p_type": "donut", "foos": []map[string]interface{}{{"foo": []map[string]interface{}{{"key": 1}, {"key": 2}, {"key": 3}, {"key": 4}}}}}
var allkeys sort.StringSlice
allkeys = AllKeys()
@ -798,18 +798,18 @@ func TestFindsNestedKeys(t *testing.T) {
"owner.dob": dob,
"beard": true,
"foos": []map[string]interface{}{
map[string]interface{}{
{
"foo": []map[string]interface{}{
map[string]interface{}{
{
"key": 1,
},
map[string]interface{}{
{
"key": 2,
},
map[string]interface{}{
{
"key": 3,
},
map[string]interface{}{
{
"key": 4,
},
},