From a2b9d208f604faa0ca5c4584ed0ddcd8e51f5b2f Mon Sep 17 00:00:00 2001 From: prateek-kumar-improving Date: Wed, 15 Jan 2025 12:20:18 -0800 Subject: [PATCH] Go: ZScore command added (#2945) * Go: ZScore command added Signed-off-by: Prateek Kumar --- go/api/base_client.go | 35 +++++++++++++++++++++++++ go/api/sorted_set_commands.go | 2 ++ go/integTest/shared_commands_test.go | 39 ++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/go/api/base_client.go b/go/api/base_client.go index 4035196813..de8e3f91f4 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -1783,3 +1783,38 @@ func (client *baseClient) XDel(key string, ids []string) (int64, error) { } return handleIntResponse(result) } + +// Returns the score of `member` in the sorted set stored at `key`. +// +// See [valkey.io] for details. +// +// Parameters: +// +// key - The key of the sorted set. +// member - The member whose score is to be retrieved. +// +// Return value: +// +// The score of the member. If `member` does not exist in the sorted set, `nil` is returned. +// If `key` does not exist, `nil` is returned. +// +// Example: +// +// membersScores := map[string]float64{ +// "one": 1.0, +// "two": 2.0, +// "three": 3.0, +// } +// +// zAddResult, err := client.ZAdd("key1", membersScores) +// zScoreResult, err := client.ZScore("key1", "one") +// //fmt.Println(zScoreResult.Value()) // Value: 1.0 +// +// [valkey.io]: https://valkey.io/commands/zscore/ +func (client *baseClient) ZScore(key string, member string) (Result[float64], error) { + result, err := client.executeCommand(C.ZScore, []string{key, member}) + if err != nil { + return CreateNilFloat64Result(), err + } + return handleFloatOrNilResponse(result) +} diff --git a/go/api/sorted_set_commands.go b/go/api/sorted_set_commands.go index 96304701dc..398cc7c61f 100644 --- a/go/api/sorted_set_commands.go +++ b/go/api/sorted_set_commands.go @@ -378,5 +378,7 @@ type SortedSetCommands interface { // [valkey.io]: https://valkey.io/commands/zrevrank/ ZRevRankWithScore(key string, member string) (Result[int64], Result[float64], error) + ZScore(key string, member string) (Result[float64], error) + ZCount(key string, rangeOptions *options.ZCountRange) (int64, error) } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 0e4bf4105a..7c9473e325 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -4877,6 +4877,45 @@ func (suite *GlideTestSuite) Test_XAdd_XLen_XTrim() { }) } +func (suite *GlideTestSuite) Test_ZScore() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key1 := uuid.NewString() + key2 := uuid.NewString() + t := suite.T() + + membersScores := map[string]float64{ + "one": 1.0, + "two": 2.0, + "three": 3.0, + } + + zAddResult, err := client.ZAdd(key1, membersScores) + assert.NoError(t, err) + assert.Equal(t, zAddResult, int64(3)) + + zScoreResult, err := client.ZScore(key1, "one") + assert.NoError(t, err) + assert.Equal(t, zScoreResult.Value(), float64(1.0)) + + zScoreResult, err = client.ZScore(key1, "non_existing_member") + assert.NoError(t, err) + assert.True(t, zScoreResult.IsNil()) + + zScoreResult, err = client.ZScore("non_existing_key", "non_existing_member") + assert.NoError(t, err) + assert.True(t, zScoreResult.IsNil()) + + // Key exists, but it is not a set + setResult, err := client.Set(key2, "bar") + assert.NoError(t, err) + assert.Equal(t, setResult, "OK") + + _, err = client.ZScore(key2, "one") + assert.NotNil(t, err) + assert.IsType(t, &api.RequestError{}, err) + }) +} + func (suite *GlideTestSuite) TestZCount() { suite.runWithDefaultClients(func(client api.BaseClient) { key1 := uuid.NewString()