mirror of
https://github.com/spf13/viper.git
synced 2026-10-19 16:15:33 +00:00
fix(config): get config type from v.configType or config file ext
This commit is contained in:
committed by
Márk Sági-Kazár
parent
d319333b0f
commit
cbc92421b1
@@ -1535,8 +1535,8 @@ func (v *Viper) MergeInConfig() error {
|
||||
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
|
||||
|
||||
func (v *Viper) ReadConfig(in io.Reader) error {
|
||||
if v.configType == "" {
|
||||
return errors.New("cannot decode configuration: config type is not set")
|
||||
if v.getConfigType() == "" {
|
||||
return errors.New("cannot decode configuration: unable to get config type from configType or file extension")
|
||||
}
|
||||
|
||||
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 (v *Viper) MergeConfig(in io.Reader) error {
|
||||
if v.configType == "" {
|
||||
return errors.New("cannot decode configuration: config type is not set")
|
||||
if v.getConfigType() == "" {
|
||||
return errors.New("cannot decode configuration: unable to get config type from configType or file extension")
|
||||
}
|
||||
|
||||
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) {
|
||||
v := New()
|
||||
v.SetConfigType("yaml")
|
||||
@@ -2059,6 +2067,14 @@ func TestMergeConfig(t *testing.T) {
|
||||
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) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
|
||||
Reference in New Issue
Block a user