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
Unmarshal does not resolve the correct value when using environment variable and a yaml config file with commented out property.
To reproduce, see the following unit test:
funcTest(t*testing.T) {
typeFooConfigstruct {
Barstring
}
typeConfigstruct {
FooFooConfig
}
yamlWithValueCommented:=`---foo:# bar: "value from file"`// Overriding with OS Environment Variableos.Setenv("FOO_BAR", "value from env")
v:=viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
v.SetConfigType("yaml")
v.SetDefault("foo.bar", "default value")
v.ReadConfig(strings.NewReader(yamlWithValueCommented))
c:=Config{}
v.Unmarshal(&c)
fmt.Println(v.Get("foo.bar")) // As expected: "value from env"fmt.Println(c.Foo.Bar) // Expected: "value from env", Actual: "default value"
}
Output:
=== RUN Test
value from env
default value
--- PASS: Test (0.00s)
The full test, which includes some sanity tests:
funcTest(t*testing.T) {
typeFooConfigstruct {
Barstring
}
typeConfigstruct {
FooFooConfig
}
yamlWithValue:=`---foo: bar: "value from file"`yamlWithValueCommented:=`---foo:# bar: "value from file"`v:=viper.New()
v.SetConfigType("yaml")
v.SetDefault("foo.bar", "default value")
v.ReadConfig(strings.NewReader(yamlWithValue))
c:=Config{}
v.Unmarshal(&c)
fmt.Println(v.Get("foo.bar")) // As expected: "value from file"fmt.Println(c.Foo.Bar) // As expected: "value from file"v=viper.New()
v.SetConfigType("yaml")
v.SetDefault("foo.bar", "default value")
v.ReadConfig(strings.NewReader(yamlWithValueCommented))
c=Config{}
v.Unmarshal(&c)
fmt.Println(v.Get("foo.bar")) // As expected: "default value"fmt.Println(c.Foo.Bar) // As expected: "default value"// Now overriding with OS Environment Variableos.Setenv("FOO_BAR", "value from env")
v=viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
v.SetConfigType("yaml")
v.SetDefault("foo.bar", "default value")
v.ReadConfig(strings.NewReader(yamlWithValue))
c=Config{}
v.Unmarshal(&c)
fmt.Println(v.Get("foo.bar")) // As expected: "value from env"fmt.Println(c.Foo.Bar) // As expected: "value from env"v=viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
v.SetConfigType("yaml")
v.SetDefault("foo.bar", "default value")
v.ReadConfig(strings.NewReader(yamlWithValueCommented))
c=Config{}
v.Unmarshal(&c)
fmt.Println(v.Get("foo.bar")) // As expected: "value from env"fmt.Println(c.Foo.Bar) // Expected: "value from env", Actual: "default value"
}
Output:
=== RUN Test
value from file
value from file
default value
default value
value from env
value from env
value from env
default value
--- PASS: Test (0.00s)
The text was updated successfully, but these errors were encountered:
Unmarshal does not resolve the correct value when using environment variable and a yaml config file with commented out property.
To reproduce, see the following unit test:
Output:
The full test, which includes some sanity tests:
Output:
The text was updated successfully, but these errors were encountered: