MM-8602: KV Store Json helper tweaks (#11502)
* move kv helpers to helpers_kv*.go * change KVGetJSON return signature Return a boolean and an error, to clearly indicate if no value was found and thus no value unmarshalled into the target interface. Also fix an issue with CompareAndSet to allow an oldValue of nil (i.e. expected to be unset). * add missing license * tweak documentation * document KVSetWithExpiryJSON minimum version
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c34942afec
Коммит
620d941b6e
@@ -4,7 +4,6 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
@@ -65,42 +64,3 @@ func (p *HelpersImpl) EnsureBot(bot *model.Bot) (retBotId string, retErr error)
|
||||
|
||||
return createdBot.UserId, nil
|
||||
}
|
||||
|
||||
func (p *HelpersImpl) KVGetJSON(key string, value interface{}) error {
|
||||
data, err := p.API.KVGet(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(data, value)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (p *HelpersImpl) KVCompareAndSetJSON(key string, oldValue interface{}, newValue interface{}) (bool, error) {
|
||||
oldData, err := json.Marshal(oldValue)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "unable to marshal old value")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user