[MM-20353] Change KVSetWithOptions to accept a byte slice (#13213)

* Drop EncodeJSON from PluginKVSetOptions

* Add unit tests for KVSetWithOptions with nil values
Этот коммит содержится в:
Ben Schumacher
2019-12-03 10:46:15 +01:00
коммит произвёл GitHub
родитель 3543fcce6c
Коммит d0d6ce0a70
11 изменённых файлов: 104 добавлений и 161 удалений

Просмотреть файл

@@ -697,7 +697,7 @@ func (api *PluginAPI) InstallPlugin(file io.Reader, replace bool) (*model.Manife
// KV Store Section // KV Store Section
func (api *PluginAPI) KVSetWithOptions(key string, value interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) { func (api *PluginAPI) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
return api.app.SetPluginKeyWithOptions(api.id, key, value, options) return api.app.SetPluginKeyWithOptions(api.id, key, value, options)
} }

Просмотреть файл

@@ -38,7 +38,7 @@ func (a *App) CompareAndSetPluginKey(pluginId string, key string, oldValue, newV
return a.SetPluginKeyWithOptions(pluginId, key, newValue, options) return a.SetPluginKeyWithOptions(pluginId, key, newValue, options)
} }
func (a *App) SetPluginKeyWithOptions(pluginId string, key string, value interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) { func (a *App) SetPluginKeyWithOptions(pluginId string, key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
if err := options.IsValid(); err != nil { if err := options.IsValid(); err != nil {
mlog.Error("Failed to set plugin key value with options", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(err)) mlog.Error("Failed to set plugin key value with options", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(err))
return false, err return false, err

Просмотреть файл

@@ -14,6 +14,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -197,60 +198,24 @@ func TestPluginKeyValueStoreSetWithOptionsJSON(t *testing.T) {
assert.Nil(t, th.App.DeletePluginKey(pluginId, "key")) assert.Nil(t, th.App.DeletePluginKey(pluginId, "key"))
}() }()
t.Run("fails with a non-serializable object as the new value", func(t *testing.T) { t.Run("storing a value without providing options works", func(t *testing.T) {
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", func() {}, model.PluginKVSetOptions{ result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-1"), model.PluginKVSetOptions{})
EncodeJSON: true,
})
assert.False(t, result)
assert.NotNil(t, err)
// verify that after the failure it was not set
ret, err := th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err)
assert.Equal(t, []byte(nil), ret)
})
t.Run("fails with a non-serializable object as the old value", func(t *testing.T) {
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{
"val-a": 10,
}, model.PluginKVSetOptions{
EncodeJSON: true,
Atomic: true,
OldValue: func() {},
})
assert.False(t, result)
assert.NotNil(t, err)
// verify that after the failure it was not set
ret, err := th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err)
assert.Equal(t, []byte(nil), ret)
})
t.Run("storing a value json encoded works", func(t *testing.T) {
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{
"val-a": 10,
}, model.PluginKVSetOptions{
EncodeJSON: true,
})
assert.True(t, result) assert.True(t, result)
assert.Nil(t, err) assert.Nil(t, err)
// and I can get it back! // and I can get it back!
ret, err := th.App.GetPluginKey(pluginId, "key") ret, err := th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, []byte(`{"val-a":10}`), ret) assert.Equal(t, []byte(`value-1`), ret)
}) })
t.Run("test that setting it atomic when it doesn't match doesn't change anything", func(t *testing.T) { t.Run("test that setting it atomic when it doesn't match doesn't change anything", func(t *testing.T) {
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{ err := th.App.SetPluginKey(pluginId, "key", []byte("value-1"))
"val-a": 30, require.Nil(t, err)
}, model.PluginKVSetOptions{
EncodeJSON: true, result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-3"), model.PluginKVSetOptions{
Atomic: true, Atomic: true,
OldValue: map[string]interface{}{ OldValue: []byte("value-2"),
"val-a": 20,
},
}) })
assert.False(t, result) assert.False(t, result)
assert.Nil(t, err) assert.Nil(t, err)
@@ -258,18 +223,16 @@ func TestPluginKeyValueStoreSetWithOptionsJSON(t *testing.T) {
// test that the value didn't change // test that the value didn't change
ret, err := th.App.GetPluginKey(pluginId, "key") ret, err := th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, []byte(`{"val-a":10}`), ret) assert.Equal(t, []byte(`value-1`), ret)
}) })
t.Run("test the atomic change with the proper old value", func(t *testing.T) { t.Run("test the atomic change with the proper old value", func(t *testing.T) {
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{ err := th.App.SetPluginKey(pluginId, "key", []byte("value-2"))
"val-a": 30, require.Nil(t, err)
}, model.PluginKVSetOptions{
EncodeJSON: true, result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-3"), model.PluginKVSetOptions{
Atomic: true, Atomic: true,
OldValue: map[string]interface{}{ OldValue: []byte("value-2"),
"val-a": 10,
},
}) })
assert.True(t, result) assert.True(t, result)
assert.Nil(t, err) assert.Nil(t, err)
@@ -277,55 +240,76 @@ func TestPluginKeyValueStoreSetWithOptionsJSON(t *testing.T) {
// test that the value did change // test that the value did change
ret, err := th.App.GetPluginKey(pluginId, "key") ret, err := th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, []byte(`{"val-a":30}`), ret) assert.Equal(t, []byte(`value-3`), ret)
}) })
}
func TestPluginKeyValueStoreSetWithOptionsByteArray(t *testing.T) { t.Run("when new value is nil and old value matches with the current, it should delete the currently set value", func(t *testing.T) {
th := Setup(t).InitBasic() // first set a value.
defer th.TearDown() result, err := th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-2", []byte("value-1"), model.PluginKVSetOptions{})
require.Nil(t, err)
require.True(t, result)
pluginId := "testpluginid" // now it should delete the set value.
result, err = th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-2", nil, model.PluginKVSetOptions{
Atomic: true,
OldValue: []byte("value-1"),
})
assert.Nil(t, err)
assert.True(t, result)
defer func() { ret, err := th.App.GetPluginKey(pluginId, "nil-test-key-2")
assert.Nil(t, th.App.DeletePluginKey(pluginId, "key")) assert.Nil(t, err)
}() assert.Nil(t, ret)
// storing a value works
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte(`myvalue`), model.PluginKVSetOptions{})
assert.True(t, result)
assert.Nil(t, err)
// and I can get it back!
ret, err := th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err)
assert.Equal(t, []byte(`myvalue`), ret)
// test that setting it atomic when it doesn't match doesn't change anything
result, err = th.App.SetPluginKeyWithOptions(pluginId, "key", []byte(`newvalue`), model.PluginKVSetOptions{
Atomic: true,
OldValue: []byte(`differentvalue`),
}) })
assert.False(t, result)
assert.Nil(t, err)
// test that the value didn't change t.Run("when new value is nil and there is a value set for the key already, it should delete the currently set value", func(t *testing.T) {
ret, err = th.App.GetPluginKey(pluginId, "key") // first set a value.
assert.Nil(t, err) result, err := th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-3", []byte("value-1"), model.PluginKVSetOptions{})
assert.Equal(t, []byte(`myvalue`), ret) require.Nil(t, err)
require.True(t, result)
// now do the atomic change with the proper old value // now it should delete the set value.
result, err = th.App.SetPluginKeyWithOptions(pluginId, "key", []byte(`newvalue`), model.PluginKVSetOptions{ result, err = th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-3", nil, model.PluginKVSetOptions{})
Atomic: true, assert.Nil(t, err)
OldValue: []byte(`myvalue`), assert.True(t, result)
ret, err := th.App.GetPluginKey(pluginId, "nil-test-key-3")
assert.Nil(t, err)
assert.Nil(t, ret)
}) })
assert.True(t, result)
assert.Nil(t, err)
// test that the value did change t.Run("when old value is nil and there is no value set for the key before, it should set the new value", func(t *testing.T) {
ret, err = th.App.GetPluginKey(pluginId, "key") result, err := th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-4", []byte("value-1"), model.PluginKVSetOptions{
assert.Nil(t, err) Atomic: true,
assert.Equal(t, []byte(`newvalue`), ret) OldValue: nil,
})
assert.Nil(t, err)
assert.True(t, result)
ret, err := th.App.GetPluginKey(pluginId, "nil-test-key-4")
assert.Nil(t, err)
assert.Equal(t, []byte("value-1"), ret)
})
t.Run("test that value is set and unset with ExpireInSeconds", func(t *testing.T) {
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-1"), model.PluginKVSetOptions{
ExpireInSeconds: 1,
})
assert.True(t, result)
assert.Nil(t, err)
// test that the value is set
ret, err := th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err)
assert.Equal(t, []byte(`value-1`), ret)
// test that the value is not longer
time.Sleep(1500 * time.Millisecond)
ret, err = th.App.GetPluginKey(pluginId, "key")
assert.Nil(t, err)
assert.Nil(t, ret)
})
} }
func TestServePluginRequest(t *testing.T) { func TestServePluginRequest(t *testing.T) {

Просмотреть файл

@@ -5254,10 +5254,6 @@
"id": "model.plugin_kvset_options.is_valid.old_value.app_error", "id": "model.plugin_kvset_options.is_valid.old_value.app_error",
"translation": "Invalid old value, it shouldn't be set when the operation is not atomic." "translation": "Invalid old value, it shouldn't be set when the operation is not atomic."
}, },
{
"id": "model.plugin_kvset_options.serialize_value.app_error",
"translation": "Could not deserialize a value. EncodeJSON: {{.EncodeJSON}}."
},
{ {
"id": "model.post.is_valid.channel_id.app_error", "id": "model.post.is_valid.channel_id.app_error",
"translation": "Invalid channel id" "translation": "Invalid channel id"

Просмотреть файл

@@ -4,16 +4,14 @@
package model package model
import ( import (
"encoding/json"
"net/http" "net/http"
) )
// PluginKVSetOptions contains information on how to store a value in the plugin KV store. // PluginKVSetOptions contains information on how to store a value in the plugin KV store.
type PluginKVSetOptions struct { type PluginKVSetOptions struct {
EncodeJSON bool // If true, store the JSON encoding of newValue Atomic bool // Only store the value if the current value matches the oldValue
Atomic bool // Only store the value if the current value matches the oldValue OldValue []byte // The value to compare with the current value. Only used when Atomic is true
OldValue interface{} // The value to compare with the current value. Only used when Atomic is true ExpireInSeconds int64 // Set an expire counter
ExpireInSeconds int64 // Set an expire counter
} }
// IsValid returns nil if the chosen options are valid. // IsValid returns nil if the chosen options are valid.
@@ -31,36 +29,8 @@ func (opt *PluginKVSetOptions) IsValid() *AppError {
return nil return nil
} }
// GetOldValueSerialized returns the serialized old value either as directly
// or encoded as JSON depending on the chosen options.
func (opt *PluginKVSetOptions) GetOldValueSerialized() ([]byte, *AppError) {
return opt.serializeValue(opt.OldValue)
}
func (opt *PluginKVSetOptions) serializeValue(value interface{}) ([]byte, *AppError) {
if opt.EncodeJSON {
data, err := json.Marshal(value)
if err != nil {
return nil, NewAppError("PluginKVSetOptions.serializeValue", "model.plugin_kvset_options.serialize_value.app_error", map[string]interface{}{"EncodeJSON": opt.EncodeJSON}, "Could not serialize JSON value", http.StatusBadRequest)
}
return data, nil
}
castResult, ok := value.([]byte)
if !ok {
return nil, NewAppError("PluginKVSetOptions.SerializeValue", "model.plugin_kvset_options.serialize_value.app_error", map[string]interface{}{"EncodeJSON": opt.EncodeJSON}, "Could not cast value to []byte", http.StatusBadRequest)
}
return castResult, nil
}
// NewPluginKeyValueFromOptions return a PluginKeyValue given a pluginID, a KV pair and options. // NewPluginKeyValueFromOptions return a PluginKeyValue given a pluginID, a KV pair and options.
func NewPluginKeyValueFromOptions(pluginId, key string, value interface{}, opt PluginKVSetOptions) (*PluginKeyValue, *AppError) { func NewPluginKeyValueFromOptions(pluginId, key string, value []byte, opt PluginKVSetOptions) (*PluginKeyValue, *AppError) {
serializedValue, err := opt.serializeValue(value)
if err != nil {
return nil, err
}
expireAt := int64(0) expireAt := int64(0)
if opt.ExpireInSeconds > 0 { if opt.ExpireInSeconds > 0 {
expireAt = GetMillis() + (opt.ExpireInSeconds * 1000) expireAt = GetMillis() + (opt.ExpireInSeconds * 1000)
@@ -69,7 +39,7 @@ func NewPluginKeyValueFromOptions(pluginId, key string, value interface{}, opt P
kv := &PluginKeyValue{ kv := &PluginKeyValue{
PluginId: pluginId, PluginId: pluginId,
Key: key, Key: key,
Value: serializedValue, Value: value,
ExpireAt: expireAt, ExpireAt: expireAt,
} }

Просмотреть файл

@@ -758,13 +758,12 @@ type API interface {
KVCompareAndDelete(key string, oldValue []byte) (bool, *model.AppError) KVCompareAndDelete(key string, oldValue []byte) (bool, *model.AppError)
// KVSetWithOptions stores a key-value pair, unique per plugin, according to the given options. // KVSetWithOptions stores a key-value pair, unique per plugin, according to the given options.
// If options.EncodeJSON is not true, the type of newValue must be of type []byte.
// Returns (false, err) if DB error occurred // Returns (false, err) if DB error occurred
// Returns (false, nil) if the value was not set // Returns (false, nil) if the value was not set
// Returns (true, nil) if the value was set // Returns (true, nil) if the value was set
// //
// Minimum server version: 5.18 // Minimum server version: 5.20
KVSetWithOptions(key string, newValue interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError)
// KVSet stores a key-value pair with an expiry time, unique per plugin. // KVSet stores a key-value pair with an expiry time, unique per plugin.
// //

Просмотреть файл

@@ -3698,7 +3698,7 @@ func (s *apiRPCServer) KVCompareAndDelete(args *Z_KVCompareAndDeleteArgs, return
type Z_KVSetWithOptionsArgs struct { type Z_KVSetWithOptionsArgs struct {
A string A string
B interface{} B []byte
C model.PluginKVSetOptions C model.PluginKVSetOptions
} }
@@ -3707,8 +3707,8 @@ type Z_KVSetWithOptionsReturns struct {
B *model.AppError B *model.AppError
} }
func (g *apiRPCClient) KVSetWithOptions(key string, newValue interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) { func (g *apiRPCClient) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
_args := &Z_KVSetWithOptionsArgs{key, newValue, options} _args := &Z_KVSetWithOptionsArgs{key, value, options}
_returns := &Z_KVSetWithOptionsReturns{} _returns := &Z_KVSetWithOptionsReturns{}
if err := g.client.Call("Plugin.KVSetWithOptions", _args, _returns); err != nil { if err := g.client.Call("Plugin.KVSetWithOptions", _args, _returns); err != nil {
log.Printf("RPC call to KVSetWithOptions API failed: %s", err.Error()) log.Printf("RPC call to KVSetWithOptions API failed: %s", err.Error())
@@ -3718,7 +3718,7 @@ func (g *apiRPCClient) KVSetWithOptions(key string, newValue interface{}, option
func (s *apiRPCServer) KVSetWithOptions(args *Z_KVSetWithOptionsArgs, returns *Z_KVSetWithOptionsReturns) error { func (s *apiRPCServer) KVSetWithOptions(args *Z_KVSetWithOptionsArgs, returns *Z_KVSetWithOptionsReturns) error {
if hook, ok := s.impl.(interface { if hook, ok := s.impl.(interface {
KVSetWithOptions(key string, newValue interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError)
}); ok { }); ok {
returns.A, returns.B = hook.KVSetWithOptions(args.A, args.B, args.C) returns.A, returns.B = hook.KVSetWithOptions(args.A, args.B, args.C)
} else { } else {

Просмотреть файл

@@ -2210,20 +2210,20 @@ func (_m *API) KVSetWithExpiry(key string, value []byte, expireInSeconds int64)
return r0 return r0
} }
// KVSetWithOptions provides a mock function with given fields: key, newValue, options // KVSetWithOptions provides a mock function with given fields: key, value, options
func (_m *API) KVSetWithOptions(key string, newValue interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) { func (_m *API) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
ret := _m.Called(key, newValue, options) ret := _m.Called(key, value, options)
var r0 bool var r0 bool
if rf, ok := ret.Get(0).(func(string, interface{}, model.PluginKVSetOptions) bool); ok { if rf, ok := ret.Get(0).(func(string, []byte, model.PluginKVSetOptions) bool); ok {
r0 = rf(key, newValue, options) r0 = rf(key, value, options)
} else { } else {
r0 = ret.Get(0).(bool) r0 = ret.Get(0).(bool)
} }
var r1 *model.AppError var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, interface{}, model.PluginKVSetOptions) *model.AppError); ok { if rf, ok := ret.Get(1).(func(string, []byte, model.PluginKVSetOptions) *model.AppError); ok {
r1 = rf(key, newValue, options) r1 = rf(key, value, options)
} else { } else {
if ret.Get(1) != nil { if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError) r1 = ret.Get(1).(*model.AppError)

Просмотреть файл

@@ -148,7 +148,7 @@ func (ps SqlPluginStore) CompareAndDelete(kv *model.PluginKeyValue, oldValue []b
return true, nil return true, nil
} }
func (ps SqlPluginStore) SetWithOptions(pluginId string, key string, value interface{}, opt model.PluginKVSetOptions) (bool, *model.AppError) { func (ps SqlPluginStore) SetWithOptions(pluginId string, key string, value []byte, opt model.PluginKVSetOptions) (bool, *model.AppError) {
if err := opt.IsValid(); err != nil { if err := opt.IsValid(); err != nil {
return false, err return false, err
} }
@@ -159,13 +159,7 @@ func (ps SqlPluginStore) SetWithOptions(pluginId string, key string, value inter
} }
if opt.Atomic { if opt.Atomic {
var serializedOldValue []byte return ps.CompareAndSet(kv, opt.OldValue)
serializedOldValue, err = opt.GetOldValueSerialized()
if err != nil {
return false, err
}
return ps.CompareAndSet(kv, serializedOldValue)
} }
savedKv, err := ps.SaveOrUpdate(kv) savedKv, err := ps.SaveOrUpdate(kv)

Просмотреть файл

@@ -531,7 +531,7 @@ type PluginStore interface {
SaveOrUpdate(keyVal *model.PluginKeyValue) (*model.PluginKeyValue, *model.AppError) SaveOrUpdate(keyVal *model.PluginKeyValue) (*model.PluginKeyValue, *model.AppError)
CompareAndSet(keyVal *model.PluginKeyValue, oldValue []byte) (bool, *model.AppError) CompareAndSet(keyVal *model.PluginKeyValue, oldValue []byte) (bool, *model.AppError)
CompareAndDelete(keyVal *model.PluginKeyValue, oldValue []byte) (bool, *model.AppError) CompareAndDelete(keyVal *model.PluginKeyValue, oldValue []byte) (bool, *model.AppError)
SetWithOptions(pluginId string, key string, value interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) SetWithOptions(pluginId string, key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError)
Get(pluginId, key string) (*model.PluginKeyValue, *model.AppError) Get(pluginId, key string) (*model.PluginKeyValue, *model.AppError)
Delete(pluginId, key string) *model.AppError Delete(pluginId, key string) *model.AppError
DeleteAllForPlugin(PluginId string) *model.AppError DeleteAllForPlugin(PluginId string) *model.AppError

Просмотреть файл

@@ -184,18 +184,18 @@ func (_m *PluginStore) SaveOrUpdate(keyVal *model.PluginKeyValue) (*model.Plugin
} }
// SetWithOptions provides a mock function with given fields: pluginId, key, value, options // SetWithOptions provides a mock function with given fields: pluginId, key, value, options
func (_m *PluginStore) SetWithOptions(pluginId string, key string, value interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) { func (_m *PluginStore) SetWithOptions(pluginId string, key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
ret := _m.Called(pluginId, key, value, options) ret := _m.Called(pluginId, key, value, options)
var r0 bool var r0 bool
if rf, ok := ret.Get(0).(func(string, string, interface{}, model.PluginKVSetOptions) bool); ok { if rf, ok := ret.Get(0).(func(string, string, []byte, model.PluginKVSetOptions) bool); ok {
r0 = rf(pluginId, key, value, options) r0 = rf(pluginId, key, value, options)
} else { } else {
r0 = ret.Get(0).(bool) r0 = ret.Get(0).(bool)
} }
var r1 *model.AppError var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, string, interface{}, model.PluginKVSetOptions) *model.AppError); ok { if rf, ok := ret.Get(1).(func(string, string, []byte, model.PluginKVSetOptions) *model.AppError); ok {
r1 = rf(pluginId, key, value, options) r1 = rf(pluginId, key, value, options)
} else { } else {
if ret.Get(1) != nil { if ret.Get(1) != nil {