From ba616a45ba5c5f9599116ed920ceb5792d4d2fd9 Mon Sep 17 00:00:00 2001 From: Jens Fosgerau Date: Tue, 7 Feb 2023 19:41:31 +0100 Subject: [PATCH] feat(zadd): add ZAddLT and ZAddGT --- commands.go | 18 +++++++++++++ commands_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/commands.go b/commands.go index a8d0b56ea3..63076bde67 100644 --- a/commands.go +++ b/commands.go @@ -299,6 +299,8 @@ type Cmdable interface { BZPopMin(ctx context.Context, timeout time.Duration, keys ...string) *ZWithKeyCmd ZAdd(ctx context.Context, key string, members ...Z) *IntCmd + ZAddLT(ctx context.Context, key string, members ...Z) *IntCmd + ZAddGT(ctx context.Context, key string, members ...Z) *IntCmd ZAddNX(ctx context.Context, key string, members ...Z) *IntCmd ZAddXX(ctx context.Context, key string, members ...Z) *IntCmd ZAddArgs(ctx context.Context, key string, args ZAddArgs) *IntCmd @@ -2353,6 +2355,22 @@ func (c cmdable) ZAdd(ctx context.Context, key string, members ...Z) *IntCmd { }) } +// ZAddLT Redis `ZADD key LT score member [score member ...]` command. +func (c cmdable) ZAddLT(ctx context.Context, key string, members ...Z) *IntCmd { + return c.ZAddArgs(ctx, key, ZAddArgs{ + LT: true, + Members: members, + }) +} + +// ZAddGT Redis `ZADD key GT score member [score member ...]` command. +func (c cmdable) ZAddGT(ctx context.Context, key string, members ...Z) *IntCmd { + return c.ZAddArgs(ctx, key, ZAddArgs{ + GT: true, + Members: members, + }) +} + // ZAddNX Redis `ZADD key NX score member [score member ...]` command. func (c cmdable) ZAddNX(ctx context.Context, key string, members ...Z) *IntCmd { return c.ZAddArgs(ctx, key, ZAddArgs{ diff --git a/commands_test.go b/commands_test.go index fdc3452caa..80aa3e3acd 100644 --- a/commands_test.go +++ b/commands_test.go @@ -3292,6 +3292,76 @@ var _ = Describe("Commands", func() { Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}})) }) + It("should ZAddArgsLT", func() { + added, err := client.ZAddLT(ctx, "zset", redis.Z{ + Score: 2, + Member: "one", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(added).To(Equal(int64(1))) + + vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}})) + + added, err = client.ZAddLT(ctx, "zset", redis.Z{ + Score: 3, + Member: "one", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(added).To(Equal(int64(0))) + + vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}})) + + added, err = client.ZAddLT(ctx, "zset", redis.Z{ + Score: 1, + Member: "one", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(added).To(Equal(int64(0))) + + vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}})) + }) + + It("should ZAddArgsGT", func() { + added, err := client.ZAddGT(ctx, "zset", redis.Z{ + Score: 2, + Member: "one", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(added).To(Equal(int64(1))) + + vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}})) + + added, err = client.ZAddGT(ctx, "zset", redis.Z{ + Score: 3, + Member: "one", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(added).To(Equal(int64(0))) + + vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(vals).To(Equal([]redis.Z{{Score: 3, Member: "one"}})) + + added, err = client.ZAddGT(ctx, "zset", redis.Z{ + Score: 1, + Member: "one", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(added).To(Equal(int64(0))) + + vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(vals).To(Equal([]redis.Z{{Score: 3, Member: "one"}})) + }) + It("should ZAddArgsNX", func() { added, err := client.ZAddNX(ctx, "zset", redis.Z{ Score: 1,