add support for slices in XAddArgs.Values

This commit is contained in:
Adam Babik 2020-06-28 12:01:21 +02:00
parent 5b4d00c217
commit b2acec277e
No known key found for this signature in database
GPG Key ID: FEE9363B9D8EA476
2 changed files with 19 additions and 8 deletions

View File

@ -43,6 +43,9 @@ func appendArgs(dst, src []interface{}) []interface{} {
dst = append(dst, s) dst = append(dst, s)
} }
return dst return dst
case []interface{}:
dst = append(dst, v...)
return dst
case map[string]interface{}: case map[string]interface{}:
for k, v := range v { for k, v := range v {
dst = append(dst, k, v) dst = append(dst, k, v)
@ -1387,16 +1390,25 @@ func (c cmdable) SUnionStore(ctx context.Context, destination string, keys ...st
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// XAddArgs accepts values in the following formats:
// - XAddArgs.Values = []interface{}{"key1", "value1", "key2", "value2"}
// - XAddArgs.Values = []string("key1", "value1", "key2", "value2")
// - XAddArgs.Values = map[string]interface{}{"key1": "value1", "key2": "value2"}
//
// Note that map will not preserve the order of key-value pairs.
type XAddArgs struct { type XAddArgs struct {
Stream string Stream string
MaxLen int64 // MAXLEN N MaxLen int64 // MAXLEN N
MaxLenApprox int64 // MAXLEN ~ N MaxLenApprox int64 // MAXLEN ~ N
ID string ID string
Values map[string]interface{} Values interface{}
} }
func (c cmdable) XAdd(ctx context.Context, a *XAddArgs) *StringCmd { func (c cmdable) XAdd(ctx context.Context, a *XAddArgs) *StringCmd {
args := make([]interface{}, 0, 6+len(a.Values)*2) var values []interface{}
values = appendArgs(values, []interface{}{a.Values})
args := make([]interface{}, 0, 6+len(values))
args = append(args, "xadd") args = append(args, "xadd")
args = append(args, a.Stream) args = append(args, a.Stream)
if a.MaxLen > 0 { if a.MaxLen > 0 {
@ -1409,10 +1421,7 @@ func (c cmdable) XAdd(ctx context.Context, a *XAddArgs) *StringCmd {
} else { } else {
args = append(args, "*") args = append(args, "*")
} }
for k, v := range a.Values { args = append(args, values...)
args = append(args, k)
args = append(args, v)
}
cmd := NewStringCmd(ctx, args...) cmd := NewStringCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)

View File

@ -3413,18 +3413,20 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("1-0")) Expect(id).To(Equal("1-0"))
// Values supports []interface{}.
id, err = client.XAdd(ctx, &redis.XAddArgs{ id, err = client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream", Stream: "stream",
ID: "2-0", ID: "2-0",
Values: map[string]interface{}{"dos": "deux"}, Values: []interface{}{"dos", "deux"},
}).Result() }).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("2-0")) Expect(id).To(Equal("2-0"))
// Value supports []string.
id, err = client.XAdd(ctx, &redis.XAddArgs{ id, err = client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream", Stream: "stream",
ID: "3-0", ID: "3-0",
Values: map[string]interface{}{"tres": "troix"}, Values: []string{"tres", "troix"},
}).Result() }).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("3-0")) Expect(id).To(Equal("3-0"))