From d6ee9b7c6762b28afa26f1526d098a47f7317fbf Mon Sep 17 00:00:00 2001 From: MikeMwita <114430148+MikeMwita@users.noreply.github.com> Date: Fri, 6 Sep 2024 05:23:11 +0300 Subject: [PATCH] Go: Implementing GETDEL command (#2228) * Go: Implementing GETDEL command Signed-off-by: MikeMwita * Go: Implementing GETDEL command Signed-off-by: MikeMwita --------- Signed-off-by: MikeMwita Co-authored-by: Avi Fenesh <55848801+avifenesh@users.noreply.github.com> --- go/api/base_client.go | 14 +++++++++++ go/api/commands.go | 15 +++++++++++ go/api/response_handlers.go | 9 +++++++ go/integTest/shared_commands_test.go | 37 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/go/api/base_client.go b/go/api/base_client.go index 25ff0c1cc4..16c2f97256 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -10,6 +10,7 @@ package api import "C" import ( + "errors" "unsafe" "github.com/valkey-io/valkey-glide/go/glide/protobuf" @@ -199,3 +200,16 @@ func (client *baseClient) DecrBy(key string, amount int64) (int64, error) { } return handleLongResponse(result), nil } + +func (client *baseClient) GetDel(key string) (string, error) { + if key == "" { + return "", errors.New("key is required") + } + + result, err := client.executeCommand(C.GetDel, []string{key}) + if err != nil { + return "", err + } + + return handleStringOrNullResponseWithFree(result), nil +} diff --git a/go/api/commands.go b/go/api/commands.go index 1864bcca8d..a00fad5002 100644 --- a/go/api/commands.go +++ b/go/api/commands.go @@ -154,4 +154,19 @@ type StringCommands interface { // // [valkey.io]: https://valkey.io/commands/decrby/ DecrBy(key string, amount int64) (int64, error) + + // GetDel gets the value associated with the given key and deletes the key. + // + // Parameters: + // key - The key to get and delete. + // + // Return value: + // If key exists, returns the value of the key as a String and deletes the key. + // If key does not exist, returns an empty string (""). + // + // For example: + // result, err := client.GetDel("key") + // + //[valkey.io]: https://valkey.io/commands/getdel/ + GetDel(key string) (string, error) } diff --git a/go/api/response_handlers.go b/go/api/response_handlers.go index b28e7e4bb5..eae4a59dc7 100644 --- a/go/api/response_handlers.go +++ b/go/api/response_handlers.go @@ -31,6 +31,15 @@ func handleStringOrNullResponse(response *C.struct_CommandResponse) string { return handleStringResponse(response) } +func handleStringOrNullResponseWithFree(response *C.struct_CommandResponse) string { + if response == nil { + return "" + } + defer C.free_command_response(response) + + return convertCharArrayToString(response.string_value, response.string_value_len) +} + func handleLongResponse(response *C.struct_CommandResponse) int64 { defer C.free_command_response(response) return int64(response.int_value) diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 85d5809320..c74ac4e9b5 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -260,3 +260,40 @@ func (suite *GlideTestSuite) TestDecrCommands_nonExistingKey() { assert.Equal(suite.T(), int64(-10), res2) }) } + +func (suite *GlideTestSuite) TestGetDel_ExistingKey() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key := uuid.New().String() + value := "testValue" + + suite.verifyOK(client.Set(key, value)) + result, err := client.GetDel(key) + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), value, result) + + result, err = client.Get(key) + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), "", result) + }) +} + +func (suite *GlideTestSuite) TestGetDel_NonExistingKey() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key := uuid.New().String() + + result, err := client.GetDel(key) + + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), "", result) + }) +} + +func (suite *GlideTestSuite) TestGetDel_EmptyKey() { + suite.runWithDefaultClients(func(client api.BaseClient) { + result, err := client.GetDel("") + + assert.NotNil(suite.T(), err) + assert.Equal(suite.T(), "", result) + assert.Equal(suite.T(), "key is required", err.Error()) + }) +}