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
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

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"}