Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
fix: SetRawConfig resets cache (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Mar 23, 2020
1 parent 443a9dd commit 2ae50bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,7 @@ func (v *Viper) SetRawConfig(config map[string]interface{}) {
insensitiviseMap(config)
v.config = config
v.configChangedAt = time.Now()
v.cache.Clear()
}

// MergeInConfig merges a new configuration with an existing config.
Expand Down
21 changes: 18 additions & 3 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2553,9 +2553,24 @@ bar:
}

func TestSetRawConfig(t *testing.T) {
Reset()
SetRawConfig(map[string]interface{}{"foo": "bar"})
assert.Equal(t, "bar", Get("foo"))
t.Run("case=setting the config map works", func(t *testing.T) {
v := New()
v.SetRawConfig(map[string]interface{}{"foo": "bar"})
assert.Equal(t, "bar", v.Get("foo"))
})

t.Run("case=resets cache", func(t *testing.T) {
key := "foo"
v := New()
// make sure the value is in the cache
for !v.cache.Set(key, "bar", 0) {
}
for _, ok := v.cache.Get(key); !ok; _, ok = v.cache.Get(key) {
}

v.SetRawConfig(map[string]interface{}{key: "not bar"})
assert.Equal(t, "not bar", v.Get(key))
})
}

func BenchmarkGetBool(b *testing.B) {
Expand Down

0 comments on commit 2ae50bd

Please sign in to comment.