Add KV helper methods to Helpers interface (#11307)
* Add KV helper methods to Helpers interface * Address code-review comments, add unit tests * Update documentation for KVCompareAndSetJSON. * Update assertions for helpers_bots_test.go * Fix assertion not called name in test.
Этот коммит содержится в:
коммит произвёл
Ali Farooq
родитель
ac4019afe5
Коммит
46f2b18e4f
@@ -4,6 +4,7 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
@@ -64,3 +65,42 @@ 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