Skip to content

Commit

Permalink
Go: ZScore command added
Browse files Browse the repository at this point in the history
Signed-off-by: Prateek Kumar <[email protected]>
  • Loading branch information
prateek-kumar-improving committed Jan 13, 2025
1 parent d31d3fa commit 5f96f46
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,3 +1630,11 @@ func (client *baseClient) XLen(key string) (Result[int64], error) {
}
return handleLongResponse(result)
}

func (client *baseClient) ZScore(key string, member string) (float64, error) {
result, err := client.executeCommand(C.ZScore, []string{key, member})
if err != nil {
return 0.0, err
}
return float64(result.float_value), nil
}
26 changes: 26 additions & 0 deletions go/api/sorted_set_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,30 @@ type SortedSetCommands interface {
//
// [valkey.io]: https://valkey.io/commands/zrevrank/
ZRevRankWithScore(key string, member string) (Result[int64], Result[float64], error)

// 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, null is returned.
// If `key` does not exist, null 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: 1.0
//
// [valkey.io]: https://valkey.io/commands/zscore/
ZScore(key string, member string) (float64, error)
}
40 changes: 40 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4889,3 +4889,43 @@ func (suite *GlideTestSuite) Test_XAdd_XLen_XTrim() {
assert.IsType(t, &api.RequestError{}, err)
})
}

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.Value(), int64(3))

zScoreResult, err := client.ZScore(key1, "one")
assert.NoError(t, err)
assert.Equal(t, zScoreResult, float64(1.0))

zScoreResult, err = client.ZScore(key1, "non_existing_member")
assert.NoError(t, err)
assert.Equal(t, zScoreResult, float64(0))

zScoreResult, err = client.ZScore("non_existing_key", "non_existing_member")
assert.NoError(t, err)
assert.Equal(t, zScoreResult, float64(0))

// Key exists, but it is not a set
setResult, err := client.Set(key2, "bar")
assert.NoError(t, err)
assert.Equal(t, setResult.Value(), "OK")

_, err = client.ZScore(key2, "one")
assert.NotNil(t, err)
assert.IsType(t, &api.RequestError{}, err)

})
}

0 comments on commit 5f96f46

Please sign in to comment.