diff --git a/CHANGELOG.md b/CHANGELOG.md index 938f0a96fa..1b85345f69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Go: Add `ZADD` ([#2813](https://github.com/valkey-io/valkey-glide/issues/2813)) * Go: Add `ZPopMin` and `ZPopMax` ([#2850](https://github.com/valkey-io/valkey-glide/pull/2850)) * Java: Add binary version of `ZRANK WITHSCORE` ([#2896](https://github.com/valkey-io/valkey-glide/pull/2896)) +* Go: Add `ZCARD` ([#2838](https://github.com/valkey-io/valkey-glide/pull/2838)) #### Breaking Changes diff --git a/go/api/base_client.go b/go/api/base_client.go index 8e4cb5a020..99cab3608d 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -1432,3 +1432,12 @@ func (client *baseClient) ZRem(key string, members []string) (Result[int64], err } return handleLongResponse(result) } + +func (client *baseClient) ZCard(key string) (Result[int64], error) { + result, err := client.executeCommand(C.ZCard, []string{key}) + if err != nil { + return CreateNilInt64Result(), err + } + + return handleLongResponse(result) +} diff --git a/go/api/sorted_set_commands.go b/go/api/sorted_set_commands.go index 835f7a5228..4159acabe1 100644 --- a/go/api/sorted_set_commands.go +++ b/go/api/sorted_set_commands.go @@ -212,4 +212,24 @@ type SortedSetCommands interface { // // [valkey.io]: https://valkey.io/commands/zrem/ ZRem(key string, members []string) (Result[int64], error) + + // Returns the cardinality (number of elements) of the sorted set stored at `key`. + // + // See [valkey.io] for details. + // + // Parameters: + // key - The key of the set. + // + // Return value: + // The number of elements in the sorted set. + // + // If `key` does not exist, it is treated as an empty sorted set, and this command returns 0. + // If `key` holds a value that is not a sorted set, an error is returned. + // + // Example: + // result1, err := client.ZCard("mySet") + // result1.Value() :1 // There is 1 item in the set + // + // [valkey.io]: https://valkey.io/commands/zcard/ + ZCard(key string) (Result[int64], error) } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 30a96f0d04..dd0fc29022 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -3544,6 +3544,33 @@ func (suite *GlideTestSuite) TestPExpireTime() { }) } +func (suite *GlideTestSuite) Test_ZCard() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key := "{key}" + uuid.NewString() + membersScores := map[string]float64{ + "one": 1.0, + "two": 2.0, + "three": 3.0, + } + t := suite.T() + res1, err := client.ZAdd(key, membersScores) + assert.Nil(t, err) + assert.Equal(t, int64(3), res1.Value()) + + res2, err := client.ZCard(key) + assert.Nil(t, err) + assert.Equal(t, int64(3), res2.Value()) + + res3, err := client.ZRem(key, []string{"one"}) + assert.Nil(t, err) + assert.Equal(t, int64(1), res3.Value()) + + res4, err := client.ZCard(key) + assert.Nil(t, err) + assert.Equal(t, int64(2), res4.Value()) + }) +} + func (suite *GlideTestSuite) TestPExpireTime_KeyDoesNotExist() { suite.SkipIfServerVersionLowerThanBy("7.0.0") suite.runWithDefaultClients(func(client api.BaseClient) {