Open mdaffin opened 5 years ago
I just ran into that as well. It would be really nice if the configmap supported all the various overrides as well.
Up!
I pushed PR #1337 to fix this, but until it's merged as a workaround I'm using a helper function that seems to work okay.
func GetStringMapFixed(conf *viper.Viper, key string) map[string]interface{} {
out := make(map[string]interface{})
for _, k := range conf.AllKeys() {
if strings.HasPrefix(k, key) {
out[strings.TrimPrefix(k, key+".")] = conf.Get(k)
}
}
return out
}
@rikkuness Thanks Very Much , +1 I found that if key=abc then if k = abcdefg will also match
/* TODO GetStringMap error */
key := "group"
out := make(map[string]interface{})
v1 := viper.AllKeys()
for _, k := range v1 {
fmt.Println(k)
if !strings.Contains(k, ".") {
continue
}
lastInd := strings.LastIndex(k, ".")
if k[:lastInd] == key {
//if strings.HasPrefix(, key) {
out[strings.TrimPrefix(k, key+".")] = viper.Get(k)
}
}
//out := viper.GetStringMap("group")
/* https://github.com/spf13/viper/issues/708 */
why? what happened
When getting a value with GetStringMapString it produced a different value to AllSettings and Get/GetString when the value is set with an environment variable.
When executing produces:
When I expected the
GetStringMapString
to output the same value asGetString
,Get
andAllSettings
, instead it uses the default value.When you throw a config into the mix you get the value from the config rather than the default: