Skip to content

Commit

Permalink
Go: ZScore command added (#2945)
Browse files Browse the repository at this point in the history
* Go: ZScore command added

Signed-off-by: Prateek Kumar <[email protected]>
  • Loading branch information
prateek-kumar-improving authored Jan 15, 2025
1 parent aa8760b commit a2b9d20
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
35 changes: 35 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions go/api/sorted_set_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
39 changes: 39 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a2b9d20

Please sign in to comment.