From 00a14bca5a0be5280e7d16e7015ec56206c1f703 Mon Sep 17 00:00:00 2001 From: MikeMwita Date: Tue, 10 Sep 2024 23:24:28 +0300 Subject: [PATCH] Go: Implementing PING command Signed-off-by: MikeMwita --- go/api/base_client.go | 34 +++++++- go/api/commands.go | 115 ++++++++++++++++----------- go/integTest/shared_commands_test.go | 17 ++++ 3 files changed, 118 insertions(+), 48 deletions(-) diff --git a/go/api/base_client.go b/go/api/base_client.go index c94f30f608..0055e8a7c1 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -24,7 +24,7 @@ type BaseClient interface { StringCommands HashCommands ListCommands - + ConnectionManagementCommands // Close terminates the client by closing all associated resources. Close() } @@ -512,3 +512,35 @@ func (client *baseClient) RPush(key string, elements []string) (Result[int64], e return handleLongResponse(result) } +func (client *baseClient) Ping() (string, error) { + result, err := client.executeCommand(C.Ping, []string{}) + if err != nil { + return "", err + } + + response, err := handleStringOrNullResponse(result) + if err != nil { + return "", err + } + + // Return the result (typically "PONG") + return response.Value(), nil +} +func (client *baseClient) PingWithMessage(message string) (string, error) { + var args []string + if message != "" { + args = []string{message} + } else { + args = []string{} + } + + result, err := client.executeCommand(C.Ping, args) + if err != nil { + return "", err + } + response, err := handleStringOrNullResponse(result) + if err != nil { + return "", err + } + return response.Value(), nil +} diff --git a/go/api/commands.go b/go/api/commands.go index ec9dbdbb6d..938e9097c8 100644 --- a/go/api/commands.go +++ b/go/api/commands.go @@ -382,52 +382,6 @@ type StringCommands interface { // [valkey.io]: https://valkey.io/commands/getrange/ GetRange(key string, start int, end int) (Result[string], error) - // Appends a value to a key. If key does not exist it is created and set as an empty string, so APPEND will be similar to - // SET in this special case. - // - // See [valkey.io] for details. - // - // Parameters: - // key - The key of the string. - // value - The value to append. - // - // Return value: - // The Result[int64] containing the length of the string after appending the value. - // - // For example: - // result, err := client.Append("key", "value") - // result.Value(): 5 - // result.IsNil(): false - // - // [valkey.io]: https://valkey.io/commands/append/ - Append(key string, value string) (Result[int64], error) - - // Returns the longest common subsequence between strings stored at key1 and key2. - // - // Since: - // Valkey 7.0 and above. - // - // Note: - // When in cluster mode, key1 and key2 must map to the same hash slot. - // - // See [valkey.io] for details. - // - // Parameters: - // key1 - The key that stores the first string. - // key2 - The key that stores the second string. - // - // Return value: - // A Result[string] containing the longest common subsequence between the 2 strings. - // A Result[string] containing empty String is returned if the keys do not exist or have no common subsequences. - // - // For example: - // testKey1: foo, testKey2: fao - // result, err := client.LCS("testKey1", "testKey2") - // result.Value(): "fo" - // result.IsNil(): false - // - // [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: @@ -435,7 +389,7 @@ type StringCommands interface { // // Return value: // If key exists, returns the value of the key as a String and deletes the key. - // If key does not exist, returns a [api.NilResult[string]] (api.CreateNilStringResult()). + // If key does not exist, returns an empty string (""). // // For example: // result, err := client.GetDel("key") @@ -691,4 +645,71 @@ type HashCommands interface { // // [valkey.io]: https://valkey.io/commands/hstrlen/ HStrLen(key string, field string) (Result[int64], error) + // Appends a value to a key. If key does not exist it is created and set as an empty string, so APPEND will be similar to + // SET in this special case. + // + // See [valkey.io] for details. + // + // Parameters: + // key - The key of the string. + // value - The value to append. + // + // Return value: + // The Result[int64] containing the length of the string after appending the value. + // + // For example: + // result, err := client.Append("key", "value") + // result.Value(): 5 + // result.IsNil(): false + // + // [valkey.io]: https://valkey.io/commands/append/ + Append(key string, value string) (Result[int64], error) + + // Returns the longest common subsequence between strings stored at key1 and key2. + // + // Since: + // Valkey 7.0 and above. + // + // Note: + // When in cluster mode, key1 and key2 must map to the same hash slot. + // + // See [valkey.io] for details. + // + // Parameters: + // key1 - The key that stores the first string. + // key2 - The key that stores the second string. + // + // Return value: + // A Result[string] containing the longest common subsequence between the 2 strings. + // A Result[string] containing empty String is returned if the keys do not exist or have no common subsequences. + // + // For example: + // testKey1: foo, testKey2: fao + // result, err := client.LCS("testKey1", "testKey2") + // result.Value(): "fo" + // result.IsNil(): false + // + // [valkey.io]: https://valkey.io/commands/lcs/ + LCS(key1 string, key2 string) (Result[string], error) + // If key does not exist, returns a [api.NilResult[string]] (api.CreateNilStringResult()). +} + +// ConnectionManagementCommands defines an interface for connection management-related commands. +// +// See [valkey.io] for details. +type ConnectionManagementCommands interface { + // Ping sends the PING command to the server. + // + // If no argument is provided, returns "PONG". If a message is provided, returns the message. + // + // Return value: + // If no argument is provided, returns "PONG". + // If an argument is provided, returns the argument. + // + // For example: + // result, err := client.Ping("Hello") + // + //[valkey.io]: https://valkey.io/commands/ping/ + Ping() (string, error) + PingWithMessage(message string) (string, error) } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 4315cda5d7..b5b46c8fd0 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -588,6 +588,23 @@ func (suite *GlideTestSuite) TestGetDel_EmptyKey() { }) } +func (suite *GlideTestSuite) TestPing_NoArgument() { + suite.runWithDefaultClients(func(client api.BaseClient) { + result, err := client.Ping() + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), "PONG", result) + }) +} + +func (suite *GlideTestSuite) TestPing_WithArgument() { + suite.runWithDefaultClients(func(client api.BaseClient) { + // Passing "Hello" as the message + result, err := client.PingWithMessage("Hello") + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), "Hello", result) + }) +} + func (suite *GlideTestSuite) TestHSet_WithExistingKey() { suite.runWithDefaultClients(func(client api.BaseClient) { fields := map[string]string{"field1": "value1", "field2": "value2"}