Skip to content

Commit

Permalink
Add BITFIELD_RO Command (#2820)
Browse files Browse the repository at this point in the history
Co-authored-by: Chayim <chayim@users.noreply.github.com>
  • Loading branch information
ofekshenawa and chayim committed Dec 17, 2023
1 parent 277e8b7 commit 1d78457
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
22 changes: 21 additions & 1 deletion bitmap_commands.go
@@ -1,6 +1,8 @@
package redis

import "context"
import (
"context"
)

type BitMapCmdable interface {
GetBit(ctx context.Context, key string, offset int64) *IntCmd
Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions commands_test.go
Expand Up @@ -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())
Expand Down

0 comments on commit 1d78457

Please sign in to comment.