Skip to content

Commit

Permalink
Merge pull request valkey-io#2294 from MikeMwita/implement-del-command
Browse files Browse the repository at this point in the history
Go: Implementing DEL command
  • Loading branch information
MikeMwita authored Oct 15, 2024
2 parents b0d39ed + fa86a6e commit 63e33e1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BaseClient interface {
ListCommands
SetCommands
ConnectionManagementCommands
GenericBaseCommands
// Close terminates the client by closing all associated resources.
Close()
}
Expand Down Expand Up @@ -734,3 +735,12 @@ func (client *baseClient) PingWithMessage(message string) (string, error) {
}
return response.Value(), nil
}

func (client *baseClient) Del(keys []string) (Result[int64], error) {
result, err := client.executeCommand(C.Del, keys)
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}
1 change: 1 addition & 0 deletions go/api/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ type StringCommands interface {
//
// [valkey.io]: https://valkey.io/commands/lcs/
LCS(key1 string, key2 string) (Result[string], error)

// GetDel gets the value associated with the given key and deletes the key.
//
// Parameters:
Expand Down
33 changes: 33 additions & 0 deletions go/api/generic_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api

// Supports commands and transactions for the "List Commands" group for standalone and cluster clients.
//
// See [valkey.io] for details.
//
// GenericBaseCommands defines an interface for the "Generic Commands".
//
// [valkey.io]: https://valkey.io/commands/?group=Generic
type GenericBaseCommands interface {
// Del removes the specified keys from the database. A key is ignored if it does not exist.
//
// Note:
//When in cluster mode, the command may route to multiple nodes when `keys` map to different hash slots.
//
// Parameters:
// keys - One or more keys to delete.
//
// Return value:
// Returns the number of keys that were removed.
//
// Example:
// result, err := client.Del([]string{"key1", "key2", "key3"})
// if err != nil {
// // handle error
// }
// fmt.Println(result) // Output: 2
//
// [valkey.io]: https://valkey.io/commands/del/
Del(keys []string) (Result[int64], error)
}
30 changes: 30 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1951,3 +1951,33 @@ func (suite *GlideTestSuite) TestLInsert() {
assert.IsType(suite.T(), &api.RequestError{}, err)
})
}

func (suite *GlideTestSuite) TestDel_MultipleKeys() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key1 := "testKey1_" + uuid.New().String()
key2 := "testKey2_" + uuid.New().String()
key3 := "testKey3_" + uuid.New().String()

suite.verifyOK(client.Set(key1, initialValue))
suite.verifyOK(client.Set(key2, initialValue))
suite.verifyOK(client.Set(key3, initialValue))

deletedCount, err := client.Del([]string{key1, key2, key3})

assert.Nil(suite.T(), err)
assert.Equal(suite.T(), int64(3), deletedCount.Value())

result1, err1 := client.Get(key1)
result2, err2 := client.Get(key2)
result3, err3 := client.Get(key3)

assert.Nil(suite.T(), err1)
assert.True(suite.T(), result1.IsNil())

assert.Nil(suite.T(), err2)
assert.True(suite.T(), result2.IsNil())

assert.Nil(suite.T(), err3)
assert.True(suite.T(), result3.IsNil())
})
}

0 comments on commit 63e33e1

Please sign in to comment.