Add BITFIELD_RO Command (#2820)

Co-authored-by: Chayim <chayim@users.noreply.github.com>
This commit is contained in:
ofekshenawa 2023-12-17 15:19:27 +02:00 committed by GitHub
parent 277e8b7d9f
commit 1d784578df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package redis
import "context"
import (
"context"
)
type BitMapCmdable interface {
GetBit(ctx context.Context, key string, offset int64) *IntCmd
@ -127,3 +129,21 @@ func (c cmdable) BitField(ctx context.Context, key string, values ...interface{}
_ = c(ctx, cmd)
return cmd
}
// BitFieldRO - Read-only variant of the BITFIELD command.
// It is like the original BITFIELD but only accepts GET subcommand and can safely be used in read-only replicas.
// - BitFieldRO(ctx, key, "<Encoding0>", "<Offset0>", "<Encoding1>","<Offset1>")
func (c cmdable) BitFieldRO(ctx context.Context, key string, values ...interface{}) *IntSliceCmd {
args := make([]interface{}, 2, 2+len(values))
args[0] = "BITFIELD_RO"
args[1] = key
if len(values)%2 != 0 {
panic("BitFieldRO: invalid number of arguments, must be even")
}
for i := 0; i < len(values); i += 2 {
args = append(args, "GET", values[i], values[i+1])
}
cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}

View File

@ -1279,6 +1279,20 @@ var _ = Describe("Commands", func() {
Expect(nn).To(Equal([]int64{0, 4}))
})
It("should BitFieldRO", func() {
nn, err := client.BitField(ctx, "mykey", "SET", "u8", 8, 255).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0}))
nn, err = client.BitFieldRO(ctx, "mykey", "u8", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0}))
nn, err = client.BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4", 13).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{0, 15, 15, 14}))
})
It("should Decr", func() {
set := client.Set(ctx, "key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())