Add top level key support

This commit is contained in:
Matt Spaulding 2016-11-07 16:01:35 +00:00
parent 651d9d916a
commit 4c6d781515
1 changed files with 27 additions and 0 deletions

View File

@ -30,6 +30,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/deckarep/golang-set"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/spf13/afero" "github.com/spf13/afero"
@ -1336,6 +1337,32 @@ func (v *Viper) AllKeys() []string {
return a return a
} }
// Get the set of all top level keys.
func TopLevelKeys() []string { return v.TopLevelKeys() }
func (v *Viper) TopLevelKeys() []string {
s := mapset.NewSetFromSlice(v.getMapKeys(castMapStringToMapInterface(v.aliases)))
s = s.Union(mapset.NewSetFromSlice(v.getMapKeys(v.override)))
s = s.Union(mapset.NewSetFromSlice(v.getMapKeys(castMapFlagToMapInterface(v.pflags))))
s = s.Union(mapset.NewSetFromSlice(v.getMapKeys(castMapStringToMapInterface(v.env))))
s = s.Union(mapset.NewSetFromSlice(v.getMapKeys(v.config)))
s = s.Union(mapset.NewSetFromSlice(v.getMapKeys(v.defaults)))
// convert interface{} to string
keys := []string{}
for _, k := range s.ToSlice() {
keys = append(keys, k.(string))
}
return keys
}
func (v *Viper) getMapKeys(m map[string]interface{}) []interface{} {
keys := []interface{}{}
for key := range m {
keys = append(keys, key)
}
return keys
}
// flattenAndMergeMap recursively flattens the given map into a map[string]bool // flattenAndMergeMap recursively flattens the given map into a map[string]bool
// of key paths (used as a set, easier to manipulate than a []string): // of key paths (used as a set, easier to manipulate than a []string):
// - each path is merged into a single key string, delimited with v.keyDelim (= ".") // - each path is merged into a single key string, delimited with v.keyDelim (= ".")