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
During an update of my application I made a change to my configuration format and was hoping by using RegisterAlias, to deal with this change in a backwards compatible way. I'm not sure if the failing testcase below is expected behavior or not.
From the README example:
viper.RegisterAlias("loud", "Verbose")
viper.Set("verbose", true) // same result as next lineviper.Set("loud", true) // same result as prior lineviper.GetBool("loud") // trueviper.GetBool("verbose") // true
As the example sets the value using two keys, my assumption is that ReadConfig would do the same as it is just an other way to set config variables.
Testcase
In my case, I wanted to refactor loud to verbose. I expect that by using an alias, I would be able to handle this change. However, depending if I'm using RegisterAlias(A, B) or RegisterAlias(B, A), either the "old" or "new" configuration file isn't parsed correctly:
import (
"bytes""testing""github.com/spf13/viper"
)
typeUnmarshalWithAliasConfigstruct {
Verbosebool`mapstructure:"verbose"`
}
funcTestReadConfigWithAlias(t*testing.T) {
tests:= []struct {
ConfigFilestringExpectedValuebool
}{
{ // 0ConfigFile: ` loud=true `,
ExpectedValue: true,
},
{ // 1ConfigFile: ` verbose=true `,
ExpectedValue: true,
},
}
fori, test:=rangetests {
v:=viper.New()
// test 0 fails and test 1 passesv.RegisterAlias("loud", "verbose")
// test 0 passes and test 1 fails// v.RegisterAlias("verbose", "loud")v.SetConfigType("toml")
iferr:=v.ReadConfig(bytes.NewBuffer([]byte(test.ConfigFile))); err!=nil {
t.Fatal(err)
}
// instead of ReadConfig using Set makes the tests pass, it works// with either key name:// v.Set("verbose", true)// v.Set("loud", true)ifv:=v.GetBool("verbose"); v!=test.ExpectedValue {
t.Errorf("[%d] expected from GetBool: %t, got: %t", i, test.ExpectedValue, v)
}
varcUnmarshalWithAliasConfigiferr:=v.Unmarshal(&c); err!=nil {
t.Fatal(err)
}
ifc.Verbose!=test.ExpectedValue {
t.Errorf("[%d] expected in struct: %t, got: %t", i, test.ExpectedValue, c.Verbose)
}
}
}
The text was updated successfully, but these errors were encountered:
Context
During an update of my application I made a change to my configuration format and was hoping by using
RegisterAlias
, to deal with this change in a backwards compatible way. I'm not sure if the failing testcase below is expected behavior or not.From the README example:
As the example sets the value using two keys, my assumption is that
ReadConfig
would do the same as it is just an other way to set config variables.Testcase
In my case, I wanted to refactor
loud
toverbose
. I expect that by using an alias, I would be able to handle this change. However, depending if I'm usingRegisterAlias(A, B)
orRegisterAlias(B, A)
, either the "old" or "new" configuration file isn't parsed correctly:The text was updated successfully, but these errors were encountered: