You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the bottom is a test that I would expect to pass, it doesn't. The crux seem to be that with AutomaticEnv, viper.Get does some magic to call into the environment, but that magic is not used during Unmarhsal, so unless you explicitly call BindEnv, the respective environment variables aren't queried.
package config
import (
"os""strings""testing""github.com/spf13/viper"
)
funcTestViperUnmarshalAutoEnv(t*testing.T) {
typeconfigstruct {
Namestring
}
name:="foobar"iferr:=os.Setenv("VIPERTEST_NAME", name); err!=nil {
t.Fatal(err)
}
v:=viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.SetEnvPrefix("VIPERTEST")
v.AutomaticEnv()
// if you uncomment this line, the test will pass, otherwise it'll fail.// I would not expect to have to call bind if I'm using AutomaticEnv.// v.BindEnv("name")c:=config{}
iferr:=v.Unmarshal(&c); err!=nil {
t.Fatal(err)
}
ifv.GetString("name") !=c.Name {
t.Fatalf("expected name to be %q but got %q", v.GetString("name"), c.Name)
}
}
The text was updated successfully, but these errors were encountered:
I think it's pretty clear that if someone calls viper.AutomaticEnv() and then calls Unmarshal, they expect everything in the target value to query the environment. That's what automatic means.
At the bottom is a test that I would expect to pass, it doesn't. The crux seem to be that with AutomaticEnv, viper.Get does some magic to call into the environment, but that magic is not used during Unmarhsal, so unless you explicitly call BindEnv, the respective environment variables aren't queried.
The text was updated successfully, but these errors were encountered: