Fix IncrBy and DecrBy in memory keyvalue implementation if there was no value set previously

This commit is contained in:
kolaente 2021-02-03 21:59:45 +01:00
parent 0ab9ce9ec4
commit d600d8b5a6
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 10 additions and 0 deletions

View File

@ -66,6 +66,11 @@ func (s *Storage) IncrBy(key string, update int64) (err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
_, exists := s.store[key]
if !exists {
s.store[key] = int64(0)
}
val, is := s.store[key].(int64)
if !is {
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
@ -80,6 +85,11 @@ func (s *Storage) DecrBy(key string, update int64) (err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
_, exists := s.store[key]
if !exists {
s.store[key] = int64(0)
}
val, is := s.store[key].(int64)
if !is {
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}