mirror of
https://github.com/spf13/viper
synced 2025-04-07 05:59:13 +00:00
fix(config): get config type from v.configType or config file ext
This commit is contained in:
parent
d319333b0f
commit
cbc92421b1
2 changed files with 20 additions and 4 deletions
8
viper.go
8
viper.go
|
@ -1535,8 +1535,8 @@ func (v *Viper) MergeInConfig() error {
|
||||||
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
|
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
|
||||||
|
|
||||||
func (v *Viper) ReadConfig(in io.Reader) error {
|
func (v *Viper) ReadConfig(in io.Reader) error {
|
||||||
if v.configType == "" {
|
if v.getConfigType() == "" {
|
||||||
return errors.New("cannot decode configuration: config type is not set")
|
return errors.New("cannot decode configuration: unable to get config type from configType or file extension")
|
||||||
}
|
}
|
||||||
|
|
||||||
v.config = make(map[string]any)
|
v.config = make(map[string]any)
|
||||||
|
@ -1547,8 +1547,8 @@ func (v *Viper) ReadConfig(in io.Reader) error {
|
||||||
func MergeConfig(in io.Reader) error { return v.MergeConfig(in) }
|
func MergeConfig(in io.Reader) error { return v.MergeConfig(in) }
|
||||||
|
|
||||||
func (v *Viper) MergeConfig(in io.Reader) error {
|
func (v *Viper) MergeConfig(in io.Reader) error {
|
||||||
if v.configType == "" {
|
if v.getConfigType() == "" {
|
||||||
return errors.New("cannot decode configuration: config type is not set")
|
return errors.New("cannot decode configuration: unable to get config type from configType or file extension")
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := make(map[string]any)
|
cfg := make(map[string]any)
|
||||||
|
|
|
@ -1542,6 +1542,14 @@ func TestReadConfig(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadConfigWithSetConfigFile(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
v.SetConfigFile("config.yaml") // Dummy value to infer config type from file extension
|
||||||
|
err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleSrc))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 45000, v.GetInt("hello.pop"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsSet(t *testing.T) {
|
func TestIsSet(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
v.SetConfigType("yaml")
|
v.SetConfigType("yaml")
|
||||||
|
@ -2059,6 +2067,14 @@ func TestMergeConfig(t *testing.T) {
|
||||||
assert.Equal(t, "bar", v.GetString("fu"))
|
assert.Equal(t, "bar", v.GetString("fu"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMergeConfigWithSetConfigFile(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
v.SetConfigFile("config.yaml") // Dummy value to infer config type from file extension
|
||||||
|
err := v.MergeConfig(bytes.NewBuffer(yamlMergeExampleSrc))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 45000, v.GetInt("hello.pop"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestMergeConfigOverrideType(t *testing.T) {
|
func TestMergeConfigOverrideType(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
v.SetConfigType("json")
|
v.SetConfigType("json")
|
||||||
|
|
Loading…
Reference in a new issue