Files
mostlymatter/plugin/helpers_kv.go
Gervasio Marchand 11b0a20d7d MM-16821 - Add a KVCompareAndDelete to the plugin API (#11804)
* Implement KVCompareAndDelete and KVCompareAndDeleteJSON

* Add tests for KVCompareAndDelete

* Update minimum server version

* Handle nil value on CompareAndSet so that it deletes it

* Fix comments

* Tweaks from PR comments

* Go back to deleted, err
2019-08-21 23:25:38 -03:00

86 строки
2.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"encoding/json"
"github.com/pkg/errors"
)
// KVGetJSON is a wrapper around KVGet to simplify reading a JSON object from the key value store.
func (p *HelpersImpl) KVGetJSON(key string, value interface{}) (bool, error) {
data, appErr := p.API.KVGet(key)
if appErr != nil {
return false, appErr
}
if data == nil {
return false, nil
}
err := json.Unmarshal(data, value)
if err != nil {
return false, err
}
return true, nil
}
// KVSetJSON is a wrapper around KVSet to simplify writing a JSON object to the key value store.
func (p *HelpersImpl) KVSetJSON(key string, value interface{}) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
return p.API.KVSet(key, data)
}
// KVCompareAndSetJSON is a wrapper around KVCompareAndSet to simplify atomically writing a JSON object to the key value store.
func (p *HelpersImpl) KVCompareAndSetJSON(key string, oldValue interface{}, newValue interface{}) (bool, error) {
var oldData, newData []byte
var err error
if oldValue != nil {
oldData, err = json.Marshal(oldValue)
if err != nil {
return false, errors.Wrap(err, "unable to marshal old value")
}
}
if newValue != nil {
newData, err = json.Marshal(newValue)
if err != nil {
return false, errors.Wrap(err, "unable to marshal new value")
}
}
return p.API.KVCompareAndSet(key, oldData, newData)
}
// KVCompareAndDeleteJSON is a wrapper around KVCompareAndDelete to simplify atomically deleting a JSON object from the key value store.
func (p *HelpersImpl) KVCompareAndDeleteJSON(key string, oldValue interface{}) (bool, error) {
var oldData []byte
var err error
if oldValue != nil {
oldData, err = json.Marshal(oldValue)
if err != nil {
return false, errors.Wrap(err, "unable to marshal old value")
}
}
return p.API.KVCompareAndDelete(key, oldData)
}
// KVSetWithExpiryJSON is a wrapper around KVSetWithExpiry to simplify atomically writing a JSON object with expiry to the key value store.
func (p *HelpersImpl) KVSetWithExpiryJSON(key string, value interface{}, expireInSeconds int64) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
return p.API.KVSetWithExpiry(key, data, expireInSeconds)
}