New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
viper.GetStringMapString(key) doesn't work with AutomaticEnv
#911
Comments
Correction, the fix would need to be at https://github.com/spf13/cast/blob/1ffadf551085444af981432dd0f6d1160c11ec64/caste.go#L855-L880 |
I'm not sure this needs fixing. In environment variables Viper already accepts JSON strings which is more flexible than the key-value pairs supported by pflags. |
Hmm. That seems confusing. I prioritize for users' ease of use. It would be confusing for some flags via Also, to provide some context, my project is an open source tool paired with Docker. Docker uses For anybody who is interested, I handle this myself.
I can add a PR for the above code. But maybe it doesn't make sense to have JSON and key value format. |
Well, whether it's confusing or not, this is the current behavior. Breaking it is not an option at the moment, but I agree it would be nice to have a key-value pair like format for environment variables. This is exactly why I don't really like the current getter-based fetching, because it doesn't give you enough flexibility. You would assume that an env var should behave the same way as this type of flag, others need the flexibility of JSON. (Keep in mind that flags are registered for each type whereas env vars are just key-value pairs, so flags can support both styles at the same time, env vars can't.) I usually suggest everyone to create a config struct with customized types, for example: type config struct{
filter filterType
}
type filterType map[string]string Then for this filter type you can create your own mapstructure decoding hook, which can decode both strings (env vars) and maps (flags). After that, you can unmarshal your config onto that struct: var c config
viper.Unmarshal(&c)
// or even
viper.UnmarshalKey("filter", &c.filter) Don't think of it as a workaround, for me this is the de facto solution for these (and basically all) use cases. |
Interesting, I hadn't pursued creating my own config struct. I started with only a few flags which then ended up needing more. I'll look at it again. |
IMHO using a config struct is always superior to everything else. Here is an example: https://github.com/sagikazarmark/modern-go-application/blob/master/cmd/modern-go-application/config.go |
Thanks! I was about to ping you for an example :) |
You can also find a couple decoding hook examples here: |
As mentioned in below upstream issue, there is a discrepancy in viper while reading string map string data type i.e. kv pair format was not supported, only `{"k":"v"}` format is allowed. This commit is to wrap GetStringMapString implementation to handle such case. Also, during the bootstrap, if there is any flag having invalid value, fatal log will be printed for early detection and awareness. Relates spf13/viper#911 Fixes cilium#18328 Signed-off-by: Tam Mach <tam.mach@isovalent.com>
As mentioned in below upstream issue, there is a discrepancy in viper while reading string map string data type i.e. kv pair format was not supported, only `{"k":"v"}` format is allowed. This commit is to wrap GetStringMapString implementation to handle such case. Also, during the bootstrap, if there is any flag having invalid value, fatal log will be printed for early detection and awareness. Relates spf13/viper#911 Fixes #18328 Signed-off-by: Tam Mach <tam.mach@isovalent.com>
[ upstream commit 768659f ] As mentioned in below upstream issue, there is a discrepancy in viper while reading string map string data type i.e. kv pair format was not supported, only `{"k":"v"}` format is allowed. This commit is to wrap GetStringMapString implementation to handle such case. Also, during the bootstrap, if there is any flag having invalid value, fatal log will be printed for early detection and awareness. Relates spf13/viper#911 Fixes cilium#18328 Signed-off-by: Tam Mach <tam.mach@isovalent.com> Signed-off-by: Jussi Maki <jussi@isovalent.com>
[ upstream commit 768659f ] As mentioned in below upstream issue, there is a discrepancy in viper while reading string map string data type i.e. kv pair format was not supported, only `{"k":"v"}` format is allowed. This commit is to wrap GetStringMapString implementation to handle such case. Also, during the bootstrap, if there is any flag having invalid value, fatal log will be printed for early detection and awareness. Relates spf13/viper#911 Fixes cilium#18328 Signed-off-by: Tam Mach <tam.mach@isovalent.com> Signed-off-by: Jussi Maki <jussi@isovalent.com>
[ upstream commit 768659f ] As mentioned in below upstream issue, there is a discrepancy in viper while reading string map string data type i.e. kv pair format was not supported, only `{"k":"v"}` format is allowed. This commit is to wrap GetStringMapString implementation to handle such case. Also, during the bootstrap, if there is any flag having invalid value, fatal log will be printed for early detection and awareness. Relates spf13/viper#911 Fixes #18328 Signed-off-by: Tam Mach <tam.mach@isovalent.com> Signed-off-by: Jussi Maki <jussi@isovalent.com>
[ upstream commit 768659f ] As mentioned in below upstream issue, there is a discrepancy in viper while reading string map string data type i.e. kv pair format was not supported, only `{"k":"v"}` format is allowed. This commit is to wrap GetStringMapString implementation to handle such case. Also, during the bootstrap, if there is any flag having invalid value, fatal log will be printed for early detection and awareness. Relates spf13/viper#911 Fixes #18328 Signed-off-by: Tam Mach <tam.mach@isovalent.com> Signed-off-by: Jussi Maki <jussi@isovalent.com>
why no merge? |
I currently use Viper to configure a Docker service. It doesn't make sense for people to change the entrypoint. So I rely on ENV vars heavily.
Following up with #608, using
AutomaticEnv()
withGetStringMapString()
still doesn't work.Looking through the source code, Viper branches based on the source. I debugged and found the function below gets an exception because it can't parse
foo=bar
style values with JSON. It also unexpectedly swallows and exception here :/As a user, I expect
viper.Get***()
to work regardless of the source.I believe the chang would actually have to be in https://github.com/spf13/cast/blob/master/cast.go#L108-L110.
The text was updated successfully, but these errors were encountered: