mirror of
https://github.com/spf13/viper
synced 2025-05-05 19:57:18 +00:00
Add method to retrieve map[string]map[string]string types
This commit is contained in:
parent
537f03e453
commit
8a095685e4
1 changed files with 71 additions and 30 deletions
45
viper.go
45
viper.go
|
@ -510,6 +510,7 @@ func (v *Viper) searchMapWithPathPrefixes(source map[string]interface{}, path []
|
||||||
// isPathShadowedInDeepMap makes sure the given path is not shadowed somewhere
|
// isPathShadowedInDeepMap makes sure the given path is not shadowed somewhere
|
||||||
// on its path in the map.
|
// on its path in the map.
|
||||||
// e.g., if "foo.bar" has a value in the given map, it “shadows”
|
// e.g., if "foo.bar" has a value in the given map, it “shadows”
|
||||||
|
//
|
||||||
// "foo.bar.baz" in a lower-priority map
|
// "foo.bar.baz" in a lower-priority map
|
||||||
func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string {
|
func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string {
|
||||||
var parentVal interface{}
|
var parentVal interface{}
|
||||||
|
@ -535,6 +536,7 @@ func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{})
|
||||||
// isPathShadowedInFlatMap makes sure the given path is not shadowed somewhere
|
// isPathShadowedInFlatMap makes sure the given path is not shadowed somewhere
|
||||||
// in a sub-path of the map.
|
// in a sub-path of the map.
|
||||||
// e.g., if "foo.bar" has a value in the given map, it “shadows”
|
// e.g., if "foo.bar" has a value in the given map, it “shadows”
|
||||||
|
//
|
||||||
// "foo.bar.baz" in a lower-priority map
|
// "foo.bar.baz" in a lower-priority map
|
||||||
func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string {
|
func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string {
|
||||||
// unify input map
|
// unify input map
|
||||||
|
@ -560,6 +562,7 @@ func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string {
|
||||||
// isPathShadowedInAutoEnv makes sure the given path is not shadowed somewhere
|
// isPathShadowedInAutoEnv makes sure the given path is not shadowed somewhere
|
||||||
// in the environment, when automatic env is on.
|
// in the environment, when automatic env is on.
|
||||||
// e.g., if "foo.bar" has a value in the environment, it “shadows”
|
// e.g., if "foo.bar" has a value in the environment, it “shadows”
|
||||||
|
//
|
||||||
// "foo.bar.baz" in a lower-priority map
|
// "foo.bar.baz" in a lower-priority map
|
||||||
func (v *Viper) isPathShadowedInAutoEnv(path []string) string {
|
func (v *Viper) isPathShadowedInAutoEnv(path []string) string {
|
||||||
var parentKey string
|
var parentKey string
|
||||||
|
@ -827,6 +830,45 @@ func (v *Viper) GetStringMapStringE(key string) (map[string]string, error) {
|
||||||
return cast.ToStringMapStringE(v.GetRaw(key))
|
return cast.ToStringMapStringE(v.GetRaw(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetStringMapStringMapString returns the value associated with the key as a map of strings.
|
||||||
|
func GetStringMapStringMapString(key string) map[string]map[string]string {
|
||||||
|
return v.GetStringMapStringMapString(key)
|
||||||
|
}
|
||||||
|
func (v *Viper) GetStringMapStringMapString(key string) map[string]map[string]string {
|
||||||
|
result := map[string]map[string]string{}
|
||||||
|
|
||||||
|
interfacesVal := cast.ToStringMap(v.Get(key))
|
||||||
|
|
||||||
|
for k, v := range interfacesVal {
|
||||||
|
result[k] = cast.ToStringMapString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringMapStringMapStringE is the same as GetStringMapStringMapString but also returns parsing errors.
|
||||||
|
func GetStringMapStringMapStringE(key string) (map[string]map[string]string, error) {
|
||||||
|
return v.GetStringMapStringMapStringE(key)
|
||||||
|
}
|
||||||
|
func (v *Viper) GetStringMapStringMapStringE(key string) (map[string]map[string]string, error) {
|
||||||
|
result := map[string]map[string]string{}
|
||||||
|
|
||||||
|
interfacesVal, err := cast.ToStringMapE(v.Get(key))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range interfacesVal {
|
||||||
|
result[k], err = cast.ToStringMapStringE(v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
|
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
|
||||||
func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
|
func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
|
||||||
func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
|
func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
|
||||||
|
@ -957,7 +999,6 @@ func (v *Viper) BindPFlags(flags *pflag.FlagSet) error {
|
||||||
//
|
//
|
||||||
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
||||||
// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
|
// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
|
||||||
//
|
|
||||||
func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
|
func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
|
||||||
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
|
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
|
||||||
return v.BindFlagValue(key, pflagValue{flag})
|
return v.BindFlagValue(key, pflagValue{flag})
|
||||||
|
@ -980,7 +1021,6 @@ func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
|
||||||
//
|
//
|
||||||
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
||||||
// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
|
// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
|
||||||
//
|
|
||||||
func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) }
|
func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) }
|
||||||
func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
|
func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
|
||||||
if flag == nil {
|
if flag == nil {
|
||||||
|
@ -1809,6 +1849,7 @@ func (v *Viper) AllKeys() []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 (= ".")
|
||||||
// - if a path is shadowed by an earlier value in the initial shadow map,
|
// - if a path is shadowed by an earlier value in the initial shadow map,
|
||||||
// it is skipped.
|
// it is skipped.
|
||||||
|
//
|
||||||
// The resulting set of paths is merged to the given shadow set at the same time.
|
// The resulting set of paths is merged to the given shadow set at the same time.
|
||||||
func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool {
|
func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool {
|
||||||
if shadow != nil && prefix != "" && shadow[prefix] {
|
if shadow != nil && prefix != "" && shadow[prefix] {
|
||||||
|
|
Loading…
Add table
Reference in a new issue