mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
Moved indexing function to utils and added docs
This commit is contained in:
parent
b3a443f0b9
commit
59bc322d1a
3 changed files with 34 additions and 19 deletions
|
@ -235,6 +235,12 @@ Example:
|
||||||
fmt.Println("verbose enabled")
|
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
|
### Marshaling
|
||||||
|
|
||||||
You also have the option of Marshaling all or a specific value to a struct, map, etc.
|
You also have the option of Marshaling all or a specific value to a struct, map, etc.
|
||||||
|
|
21
util.go
21
util.go
|
@ -17,6 +17,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
@ -184,3 +185,23 @@ func parseSizeInBytes(sizeStr string) uint {
|
||||||
|
|
||||||
return safeMul(uint(size), multiplier)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
26
viper.go
26
viper.go
|
@ -598,27 +598,13 @@ func (v *Viper) find(key string) interface{} {
|
||||||
return nil
|
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() {
|
func (v *Viper) buildIndex() {
|
||||||
v.index = make(map[string]interface{})
|
v.index = make(map[string]interface{})
|
||||||
v.indexMap(v.AllSettings(), "")
|
indexMap(v.AllSettings(), "", v.index)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to see if the key has been set in any of the data locations
|
// 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 a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return all keys found in the index, including the deeply
|
||||||
|
// nested keys.
|
||||||
func AllIndexes() []string { return v.AllIndexes() }
|
func AllIndexes() []string { return v.AllIndexes() }
|
||||||
func (v *Viper) AllIndexes() []string {
|
func (v *Viper) AllIndexes() []string {
|
||||||
v.buildIndex()
|
v.buildIndex()
|
||||||
|
|
Loading…
Reference in a new issue