Skip to content
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

Fix missing config type when reading from a buffer #1857

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,10 @@ 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")
}

v.config = make(map[string]any)
return v.unmarshalReader(in, v.config)
}
Expand All @@ -1585,6 +1589,10 @@ 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")
}

cfg := make(map[string]any)
if err := v.unmarshalReader(in, cfg); err != nil {
return err
Expand Down
36 changes: 23 additions & 13 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,20 +1641,29 @@ func TestFindsNestedKeys(t *testing.T) {
}
}

func TestReadBufConfig(t *testing.T) {
v := New()
v.SetConfigType("yaml")
v.ReadConfig(bytes.NewBuffer(yamlExample))
t.Log(v.AllKeys())
func TestReadConfig(t *testing.T) {
t.Run("ok", func(t *testing.T) {
v := New()
v.SetConfigType("yaml")
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
require.NoError(t, err)
t.Log(v.AllKeys())

assert.True(t, v.InConfig("name"))
assert.True(t, v.InConfig("clothing.jacket"))
assert.False(t, v.InConfig("state"))
assert.False(t, v.InConfig("clothing.hat"))
assert.Equal(t, "steve", v.Get("name"))
assert.Equal(t, []any{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
assert.Equal(t, map[string]any{"jacket": "leather", "trousers": "denim", "pants": map[string]any{"size": "large"}}, v.Get("clothing"))
assert.Equal(t, 35, v.Get("age"))
})

assert.True(t, v.InConfig("name"))
assert.True(t, v.InConfig("clothing.jacket"))
assert.False(t, v.InConfig("state"))
assert.False(t, v.InConfig("clothing.hat"))
assert.Equal(t, "steve", v.Get("name"))
assert.Equal(t, []any{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
assert.Equal(t, map[string]any{"jacket": "leather", "trousers": "denim", "pants": map[string]any{"size": "large"}}, v.Get("clothing"))
assert.Equal(t, 35, v.Get("age"))
t.Run("missing config type", func(t *testing.T) {
v := New()
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
require.Error(t, err)
})
}

func TestIsSet(t *testing.T) {
Expand Down Expand Up @@ -2127,6 +2136,7 @@ func TestSafeWriteAsConfig(t *testing.T) {
v := New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
v.SetConfigType("yaml")
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
require.NoError(t, err)
require.NoError(t, v.SafeWriteConfigAs("/test/c.yaml"))
Expand Down