Skip to content

Commit

Permalink
Go: Implementing GETDEL command (#2228)
Browse files Browse the repository at this point in the history
* Go: Implementing GETDEL command

Signed-off-by: MikeMwita <[email protected]>

* Go: Implementing GETDEL command

Signed-off-by: MikeMwita <[email protected]>

---------

Signed-off-by: MikeMwita <[email protected]>
Co-authored-by: Avi Fenesh <[email protected]>
  • Loading branch information
MikeMwita and avifenesh authored Sep 6, 2024
1 parent 3dc289f commit d6ee9b7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
14 changes: 14 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package api
import "C"

import (
"errors"
"unsafe"

"github.com/valkey-io/valkey-glide/go/glide/protobuf"
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions go/api/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions go/api/response_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

0 comments on commit d6ee9b7

Please sign in to comment.