From 4935c434b169340eb36b341eb382692a9b8b3ca8 Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Mon, 18 Dec 2023 19:03:04 +0800 Subject: [PATCH 1/2] feat: add hstrlen command for hash Signed-off-by: rfyiamcool --- commands_test.go | 16 ++++++++++++++++ hash_commands.go | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/commands_test.go b/commands_test.go index 309ba1eb0..c71b730e3 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2414,6 +2414,22 @@ var _ = Describe("Commands", func() { Equal([]redis.KeyValue{{Key: "key2", Value: "hello2"}}), )) }) + + It("should HStrLen", func() { + hSet := client.HSet(ctx, "hash", "key", "hello") + Expect(hSet.Err()).NotTo(HaveOccurred()) + + hStrLen := client.HStrLen(ctx, "hash", "key") + Expect(hStrLen.Err()).NotTo(HaveOccurred()) + Expect(hStrLen.Val()).NotTo(Equal(int64(len("hello")))) + + nonHStrLen := client.HGet(ctx, "hash", "key1") + Expect(nonHStrLen.Val()).To(Equal(int64(0))) + + hDel := client.HDel(ctx, "hash", "key") + Expect(hDel.Err()).NotTo(HaveOccurred()) + Expect(hDel.Val()).To(Equal(int64(1))) + }) }) Describe("hyperloglog", func() { diff --git a/hash_commands.go b/hash_commands.go index 2c62a75ad..7e1286db0 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -6,6 +6,7 @@ type HashCmdable interface { HDel(ctx context.Context, key string, fields ...string) *IntCmd HExists(ctx context.Context, key, field string) *BoolCmd HGet(ctx context.Context, key, field string) *StringCmd + HStrLen(ctx context.Context, key, field string) *IntCmd HGetAll(ctx context.Context, key string) *MapStringStringCmd HIncrBy(ctx context.Context, key, field string, incr int64) *IntCmd HIncrByFloat(ctx context.Context, key, field string, incr float64) *FloatCmd @@ -45,6 +46,12 @@ func (c cmdable) HGet(ctx context.Context, key, field string) *StringCmd { return cmd } +func (c cmdable) HStrLen(ctx context.Context, key, field string) *IntCmd { + cmd := NewIntCmd(ctx, "hstrlen", key, field) + _ = c(ctx, cmd) + return cmd +} + func (c cmdable) HGetAll(ctx context.Context, key string) *MapStringStringCmd { cmd := NewMapStringStringCmd(ctx, "hgetall", key) _ = c(ctx, cmd) From 47591c8c2dba736298a917de904de8387b1f3edc Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Tue, 19 Dec 2023 00:21:07 +0800 Subject: [PATCH 2/2] feat: add hstrlen command for hash Signed-off-by: rfyiamcool --- commands_test.go | 5 +++-- hash_commands.go | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/commands_test.go b/commands_test.go index c71b730e3..56956c6e9 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2421,9 +2421,10 @@ var _ = Describe("Commands", func() { hStrLen := client.HStrLen(ctx, "hash", "key") Expect(hStrLen.Err()).NotTo(HaveOccurred()) - Expect(hStrLen.Val()).NotTo(Equal(int64(len("hello")))) + Expect(hStrLen.Val()).To(Equal(int64(len("hello")))) - nonHStrLen := client.HGet(ctx, "hash", "key1") + nonHStrLen := client.HStrLen(ctx, "hash", "keyNon") + Expect(hStrLen.Err()).NotTo(HaveOccurred()) Expect(nonHStrLen.Val()).To(Equal(int64(0))) hDel := client.HDel(ctx, "hash", "key") diff --git a/hash_commands.go b/hash_commands.go index 7e1286db0..f8ca64d34 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -6,7 +6,6 @@ type HashCmdable interface { HDel(ctx context.Context, key string, fields ...string) *IntCmd HExists(ctx context.Context, key, field string) *BoolCmd HGet(ctx context.Context, key, field string) *StringCmd - HStrLen(ctx context.Context, key, field string) *IntCmd HGetAll(ctx context.Context, key string) *MapStringStringCmd HIncrBy(ctx context.Context, key, field string, incr int64) *IntCmd HIncrByFloat(ctx context.Context, key, field string, incr float64) *FloatCmd @@ -20,6 +19,7 @@ type HashCmdable interface { HVals(ctx context.Context, key string) *StringSliceCmd HRandField(ctx context.Context, key string, count int) *StringSliceCmd HRandFieldWithValues(ctx context.Context, key string, count int) *KeyValueSliceCmd + HStrLen(ctx context.Context, key, field string) *IntCmd } func (c cmdable) HDel(ctx context.Context, key string, fields ...string) *IntCmd { @@ -46,12 +46,6 @@ func (c cmdable) HGet(ctx context.Context, key, field string) *StringCmd { return cmd } -func (c cmdable) HStrLen(ctx context.Context, key, field string) *IntCmd { - cmd := NewIntCmd(ctx, "hstrlen", key, field) - _ = c(ctx, cmd) - return cmd -} - func (c cmdable) HGetAll(ctx context.Context, key string) *MapStringStringCmd { cmd := NewMapStringStringCmd(ctx, "hgetall", key) _ = c(ctx, cmd) @@ -179,3 +173,9 @@ func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match str _ = c(ctx, cmd) return cmd } + +func (c cmdable) HStrLen(ctx context.Context, key, field string) *IntCmd { + cmd := NewIntCmd(ctx, "hstrlen", key, field) + _ = c(ctx, cmd) + return cmd +}