Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: ZREM. #2880

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,3 +1370,11 @@ func (client *baseClient) ZPopMaxWithCount(key string, count int64) (map[Result[
}
return handleStringDoubleMapResponse(result)
}

func (client *baseClient) ZRem(key string, members []string) (Result[int64], error) {
result, err := client.executeCommand(C.ZRem, append([]string{key}, members...))
if err != nil {
return CreateNilInt64Result(), err
}
return handleLongResponse(result)
}
20 changes: 20 additions & 0 deletions go/api/sorted_set_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,24 @@ type SortedSetCommands interface {
//
// [valkey.io]: https://valkey.io/commands/zpopmin/
ZPopMaxWithCount(key string, count int64) (map[Result[string]]Result[float64], error)

// Removes the specified members from the sorted set stored at `key`.
// Specified members that are not a member of this set are ignored.
//
// See [valkey.io] for details.
//
// Parameters:
// key - The key of the sorted set.
// members - The members to remove.
//
// Return value:
// The number of members that were removed from the sorted set, not including non-existing members.
// If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
//
// Example:
// res, err := client.ZRem("mySortedSet", []string{""member1", "member2", "missing"})
// fmt.Println(res.Value()) // Output: 2
//
// [valkey.io]: https://valkey.io/commands/zrem/
ZRem(key string, members []string) (Result[int64], error)
}
36 changes: 36 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4039,3 +4039,39 @@ func (suite *GlideTestSuite) TestZPopMax() {
assert.IsType(suite.T(), &api.RequestError{}, err)
})
}

func (suite *GlideTestSuite) TestZRem() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
memberScoreMap := map[string]float64{
"one": 1.0,
"two": 2.0,
"three": 3.0,
}
res, err := client.ZAdd(key, memberScoreMap)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), int64(3), res.Value())

// no members to remove
_, err = client.ZRem(key, []string{})
assert.NotNil(suite.T(), err)
assert.IsType(suite.T(), &api.RequestError{}, err)

res, err = client.ZRem(key, []string{"one"})
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), int64(1), res.Value())

// TODO: run ZCard there
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this TO DO added here? This TO DO should be in the ZCARD test, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge this PR today. @prateek-kumar-improving, can you have a chat with Yury after he is back. Thanks

res, err = client.ZRem(key, []string{"one", "two", "three"})
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), int64(2), res.Value())

// non sorted set key
_, err = client.Set(key, "test")
assert.Nil(suite.T(), err)

_, err = client.ZRem(key, []string{"value"})
assert.NotNil(suite.T(), err)
assert.IsType(suite.T(), &api.RequestError{}, err)
})
}
Loading