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
Unmarshaling nested keys does not read from env #1509
Comments
👋 Thanks for reporting! A maintainer will take a look at your issue shortly. 👀 In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues. ⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9 📣 If you've already given us your feedback, you can still help by spreading the news, https://twitter.com/sagikazarmark/status/1306904078967074816 Thank you! ❤️ |
Update: even specifically binding the env key does not help:
Actually it does when using dot style:
Seems there are two issues here:
|
Finally, to workaround, I'm now pre-registering all env keys for the config struct like this:
|
I think that I recently just hit the same issue as you but in a different form. package main
import (
"bytes"
"fmt"
"strings"
"github.com/spf13/viper"
)
type (
NestedConfig struct {
Token string `mapstructure:"token"`
Thing string `mapstructure:"thing"`
}
Config struct {
Nested NestedConfig `mapstructure:"nested",omitempty`
}
ConfigWrapper struct {
Config Config `mapstructure:"wrapper_config_attr"`
}
)
func main() {
viper.SetConfigType("yaml")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
var yamlExample = []byte(`
wrapper_config_attr:
nested:
thing: weeee
`)
viper.ReadConfig(bytes.NewBuffer(yamlExample))
viper.AutomaticEnv()
var wrapper ConfigWrapper
if err := viper.Unmarshal(&wrapper); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", wrapper)
fmt.Printf("wrapper_config_attr.nested.token=%+v\n", viper.Get("wrapper_config_attr.nested.token"))
} Note that in the initial YAML provided to viper to read,
The way that I got around this was to add a line somewhere before the viper.SetDefault("wrapper_config_attr.nested.token", "<replace-me>")
If I had to guess, this has something to do with the dynamic nature in which viper reads environment variables. But that's speculation, I did not dive into the code to find a root cause. |
I've just come across #761 (comment) but didn't get time to test that yet. I assume this would do some sort of double roundtrip to fetch all the keys first and then register them explicitly. In fact, I think I should close this issue given #761. |
ahhhh good find @andig , thank you! |
Preflight Checklist
Viper Version
1.8.1
Go Version
1.20.1
Config Source
Environment variables
Format
Dotenv
Repl.it link
https://replit.com/@andig1/nested-env?v=1
Code reproducing the issue
Expected Behavior
Nested key
foo.bar
should be read from env.Actual Behavior
Only root key
id
is read from env.Steps To Reproduce
Run test
Additional Information
It seems the repl doesn't read either key from env. Not sure why.
The text was updated successfully, but these errors were encountered: