Moved indexing function to utils and added docs

This commit is contained in:
Kiril Zvezdarov 2015-03-27 17:03:12 -04:00
parent b3a443f0b9
commit 59bc322d1a
3 changed files with 34 additions and 19 deletions

View file

@ -235,6 +235,12 @@ Example:
fmt.Println("verbose enabled")
}
You can get deeply nested values by providing the expected key path.
Example:
viper.Get("log.verbose") // case-insensitive path <root key>.<subkey1>.<subkey2>...etc
### Marshaling
You also have the option of Marshaling all or a specific value to a struct, map, etc.

21
util.go
View file

@ -17,6 +17,7 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"unicode"
@ -184,3 +185,23 @@ func parseSizeInBytes(sizeStr string) uint {
return safeMul(uint(size), multiplier)
}
// Recursively walks through the source map, populating the index with keys representing
// the nesting of the value and the value itself.
func indexMap(source map[string]interface{}, prefix string, index map[string]interface{}) {
if len(prefix) > 0 {
prefix = prefix + INDEX_DELIM
}
for key, val := range source {
indexPath := strings.ToLower(prefix + key)
v.index[indexPath] = val
if reflect.TypeOf(val).Kind() == reflect.Map {
indexMap(cast.ToStringMap(val), indexPath, index)
}
}
}

View file

@ -598,27 +598,13 @@ func (v *Viper) find(key string) interface{} {
return nil
}
// Recursively walks through the structure returned by
// AllSettings, indexing deeply nested values.
// It uses AllSettings in order to get a properly
// prioritized config structure.
func (v *Viper) buildIndex() {
v.index = make(map[string]interface{})
v.indexMap(v.AllSettings(), "")
}
func (v *Viper) indexMap(source map[string]interface{}, prefix string) {
if len(prefix) > 0 {
prefix = prefix + INDEX_DELIM
}
for key, val := range source {
indexPath := strings.ToLower(prefix + key)
v.index[indexPath] = val
if reflect.TypeOf(val).Kind() == reflect.Map {
v.indexMap(cast.ToStringMap(val), indexPath)
}
}
indexMap(v.AllSettings(), "", v.index)
}
// Check to see if the key has been set in any of the data locations
@ -840,6 +826,8 @@ func (v *Viper) AllKeys() []string {
return a
}
// Return all keys found in the index, including the deeply
// nested keys.
func AllIndexes() []string { return v.AllIndexes() }
func (v *Viper) AllIndexes() []string {
v.buildIndex()