-
Notifications
You must be signed in to change notification settings - Fork 2k
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
UnmarshalKey / Sub only uses read config, neglects environment variables #1012
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! ❤️ |
Ran into the same issue |
The error seems to come from the Get() method. If I do the same but change Sub to Get, I also "miss" the env values. But only in "nested" values. Get only works if you give the full path, but then it works correctly! |
Example expanded with Get also code package main
import (
"bytes"
"fmt"
"os"
"github.com/spf13/viper"
)
func main() {
v := viper.New()
type Bar struct {
Val string `mapstructure:"val"`
}
type Foo struct {
Bar Bar `mapstructure:"bar"`
}
foo := Foo{
Bar: Bar{
Val: "A",
},
}
var yamlExample = []byte(`
foo:
bar:
val: "B"
`)
// Read config
fmt.Println("Reading config")
v.SetConfigType("yaml")
v.ReadConfig(bytes.NewBuffer(yamlExample))
fmt.Printf("%#v\n\n", v.AllSettings())
// BindEnv
fmt.Println("Setting env")
os.Setenv("FOO_BAR_VAL", "C")
v.BindEnv("foo.bar.val", "FOO_BAR_VAL")
fmt.Printf("%#v\n\n", v.AllSettings())
fmt.Println("Get")
val := v.Get("foo")
if val == nil {
panic("missing key")
}
fmt.Printf("%#v\n\n", val)
fmt.Println("Unmarshal")
if err := v.Sub("foo").Unmarshal(&foo); err != nil {
panic(err)
}
fmt.Printf("%#v\n\n", foo)
} Output:
Expected:
|
Simple workaround until fixed (mileage may vary) func Sub(v *viper.Viper, name string) *viper.Viper {
r := viper.New()
for _, key := range v.AllKeys() {
if strings.Index(key, name+".") == 0 {
r.Set(key[len(name)+1:], v.Get(key))
}
}
return r
} |
i encountered this bug today. for me it was a bit difficult to track down was i am currently merging multiple yaml files together; i thought the issue was in that part of the code. |
Same here. Unmarshal isn't picking up on the environment variables. |
Description:
A structured configuration is read using
ReadConfig
orReadInConfig
. Subsection keys are bound to environment variables. Viper correctly updates its config, i.e. key accessGet
/AllSettings
works correctly. But when trying to useUnmarshal
, only the config file seems to be used.Code:
Output:
Expected Output:
The text was updated successfully, but these errors were encountered: