Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat/simplify-seri…
Browse files Browse the repository at this point in the history
…alization
  • Loading branch information
jonastheis committed Oct 31, 2023
2 parents a5ebefe + 620bd74 commit 6a0b43d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
var (
// ErrKeyNotFound is returned when an op. doesn't find the given key.
ErrKeyNotFound = ierrors.New("key not found")
// ErrTypedValueNotChanged is a sentinel error that can be returned by the TypedValue.Compute callback to indicate
// that the current value should not be changed.
ErrTypedValueNotChanged = ierrors.New("typed value not changed")
// ErrStoreClosed is returned when an op accesses the kvstore but it was already closed.
ErrStoreClosed = ierrors.New("trying to access closed kvstore")

Expand Down
10 changes: 9 additions & 1 deletion kvstore/typedvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ func (t *TypedValue[V]) Compute(computeFunc func(currentValue V, exists bool) (n
}

if newValue, err = computeFunc(currentValue, exists); err != nil {
if ierrors.Is(err, ErrTypedValueNotChanged) {
return currentValue, nil
}

return newValue, ierrors.Wrap(err, "failed to compute new value")
} else if newValueBytes, newValueBytesErr := t.vToBytes(newValue); err != nil {
return currentValue, ierrors.Wrap(newValueBytesErr, "failed to encode new value")
} else if err = t.kv.Set(t.keyBytes, newValueBytes); err != nil {
return currentValue, ierrors.Wrap(err, "failed to store new value in KV store")
}

t.valueCached = &newValue
Expand All @@ -119,7 +127,7 @@ func (t *TypedValue[V]) Set(value V) error {
if valueBytes, err := t.vToBytes(value); err != nil {
return ierrors.Wrap(err, "failed to encode value")
} else if err = t.kv.Set(t.keyBytes, valueBytes); err != nil {
return ierrors.Wrap(err, "failed to store in KV store")
return ierrors.Wrap(err, "failed to store value in KV store")
}

t.valueCached = &value
Expand Down

0 comments on commit 6a0b43d

Please sign in to comment.