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)
}
return dst
case []interface{}:
dst = append(dst, v...)
return dst
case map[string]interface{}:
for k, v := range 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 {
Stream string
MaxLen int64 // MAXLEN N
MaxLenApprox int64 // MAXLEN ~ N
ID string
Values map[string]interface{}
Values interface{}
}
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, a.Stream)
if a.MaxLen > 0 {
@ -1409,10 +1421,7 @@ func (c cmdable) XAdd(ctx context.Context, a *XAddArgs) *StringCmd {
} else {
args = append(args, "*")
}
for k, v := range a.Values {
args = append(args, k)
args = append(args, v)
}
args = append(args, values...)
cmd := NewStringCmd(ctx, args...)
_ = c(ctx, cmd)

View File

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