diff --git a/go/api/base_client.go b/go/api/base_client.go index ba83dedd95..5441cbcc77 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -1155,3 +1155,19 @@ func (client *baseClient) Touch(keys []string) (Result[int64], error) { return handleLongResponse(result) } + +func (client *baseClient) Rename(key string, newKey string) (Result[string], error) { + result, err := client.executeCommand(C.Rename, []string{key, newKey}) + if err != nil { + return CreateNilStringResult(), err + } + return handleStringOrNullResponse(result) +} + +func (client *baseClient) Renamenx(key string, newKey string) (Result[bool], error) { + result, err := client.executeCommand(C.RenameNX, []string{key, newKey}) + if err != nil { + return CreateNilBoolResult(), err + } + return handleBooleanResponse(result) +} diff --git a/go/api/generic_commands.go b/go/api/generic_commands.go index 5978ce0c25..04fd69d520 100644 --- a/go/api/generic_commands.go +++ b/go/api/generic_commands.go @@ -385,4 +385,49 @@ type GenericBaseCommands interface { // // [valkey.io]: Https://valkey.io/commands/type/ Type(key string) (Result[string], error) + + // Renames key to new key. + // If new Key already exists it is overwritten. + // + // Note: + // When in cluster mode, both key and newKey must map to the same hash slot. + // + // Parameters: + // key to rename. + // newKey The new name of the key. + // + // Return value: + // If the key was successfully renamed, return "OK". If key does not exist, an error is thrown. + // + // Example: + // result, err := client.Rename([]string{"key","newkey"}) + // if err != nil { + // // handle error + // } + // fmt.Println(result.Value()) // Output: OK + // + // [valkey.io]: https://valkey.io/commands/rename/ + Rename(key string, newKey string) (Result[string], error) + + // Renames key to newkey if newKey does not yet exist. + // + // Note: + // When in cluster mode, both key and newkey must map to the same hash slot. + // + // Parameters: + // key to rename. + // newKey The new name of the key. + // + // Return value: + // true if key was renamed to newKey, false if newKey already exists. + // + // Example: + // result, err := client.Renamenx([]string{"key","newkey"}) + // if err != nil { + // // handle error + // } + // fmt.Println(result.Value()) // Output: OK + // + // [valkey.io]: https://valkey.io/commands/renamenx/ + Renamenx(key string, newKey string) (Result[bool], error) } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 46e4c86133..406e0a1ac6 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -3504,3 +3504,42 @@ func (suite *GlideTestSuite) TestUnlink() { assert.Equal(suite.T(), int64(0), resultInvalidKey.Value(), "The unlink should be 0") }) } + +func (suite *GlideTestSuite) Test_Rename() { + suite.runWithDefaultClients(func(client api.BaseClient) { + // Test 1 Check if the command successfully renamed + key := "{keyName}" + uuid.NewString() + initialValueRename := "TestRename_RenameValue" + newRenameKey := "{newkeyName}" + uuid.NewString() + suite.verifyOK(client.Set(key, initialValueRename)) + client.Rename(key, newRenameKey) + + // Test 2 Check if the rename command return false if the key/newkey is invalid. + key1 := "{keyName}" + uuid.NewString() + res1, err := client.Rename(key1, "invalidKey") + assert.Equal(suite.T(), "", res1.Value()) + assert.NotNil(suite.T(), err) + assert.IsType(suite.T(), &api.RequestError{}, err) + }) +} + +func (suite *GlideTestSuite) TestRenamenx() { + suite.runWithDefaultClients(func(client api.BaseClient) { + // Test 1 Check if the renamenx command return true if key was renamed to newKey + key := "{keyName}" + uuid.NewString() + key2 := "{keyName}" + uuid.NewString() + suite.verifyOK(client.Set(key, initialValue)) + res1, err := client.Renamenx(key, key2) + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), true, res1.Value()) + + // Test 2 Check if the renamenx command return false if newKey already exists. + key3 := "{keyName}" + uuid.NewString() + key4 := "{keyName}" + uuid.NewString() + suite.verifyOK(client.Set(key3, initialValue)) + suite.verifyOK(client.Set(key4, initialValue)) + res2, err := client.Renamenx(key3, key4) + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), false, res2.Value()) + }) +}