Skip to content

Commit

Permalink
Go: Implementing PING command
Browse files Browse the repository at this point in the history
Signed-off-by: MikeMwita <[email protected]>
  • Loading branch information
MikeMwita committed Oct 9, 2024
1 parent e5c1bae commit 00a14bc
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 48 deletions.
34 changes: 33 additions & 1 deletion go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type BaseClient interface {
StringCommands
HashCommands
ListCommands

ConnectionManagementCommands
// Close terminates the client by closing all associated resources.
Close()
}
Expand Down Expand Up @@ -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
}
115 changes: 68 additions & 47 deletions go/api/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,60 +382,14 @@ 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:
// 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 a [api.NilResult[string]] (api.CreateNilStringResult()).
// If key does not exist, returns an empty string ("").
//
// For example:
// result, err := client.GetDel("key")
Expand Down Expand Up @@ -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)
}
17 changes: 17 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down

0 comments on commit 00a14bc

Please sign in to comment.