Skip to content

Commit

Permalink
Implement Echo Connection Management Cluster
Browse files Browse the repository at this point in the history
Signed-off-by: EdricCua <[email protected]>
  • Loading branch information
EdricCua committed Jan 23, 2025
1 parent 94a872b commit 79a02da
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
14 changes: 14 additions & 0 deletions go/api/connection_management_cluster_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api

import "github.com/valkey-io/valkey-glide/go/glide/api/options"

// Supports commands and transactions for the "Connection Management" group of commands for cluster client.
//
// See [valkey.io] for details.
//
// [valkey.io]: https://valkey.io/commands/#connection
type ConnectionManagementClusterCommands interface {
EchoWithOptions(echoOptions *options.EchoOptions) (ClusterValue[interface{}], error)
}
2 changes: 2 additions & 0 deletions go/api/connection_management_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ type ConnectionManagementCommands interface {
//
// [valkey.io]: https://valkey.io/commands/echo/
Echo(message string) (Result[string], error)

EchoWithOptions(echoOptions *options.EchoOptions) (string, error)
}
34 changes: 34 additions & 0 deletions go/api/glide_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,37 @@ func (client *glideClusterClient) CustomCommand(args []string) (ClusterValue[int
}
return CreateClusterValue(data), nil
}

// Echoes the provided message back.
//
// Parameters:
//
// echoOptions - The EchoOptions type.
//
// Return value:
//
// Returns "PONG" or the copy of message.
//
// For example:
//
// route := config.SimpleNodeRoute(config.RandomRoute)
// options := options.NewEchoOptionsBuilder().SetRoute(route).SetMessage("Hello")
// result, err := client.EchoWithOptions(options)
// fmt.Println(result) // Output: "Hello"
//
// [valkey.io]: https://valkey.io/commands/echo/
func (client *glideClusterClient) EchoWithOptions(opts *options.EchoOptions) (ClusterValue[interface{}], error) {
args, err := opts.ToArgs()
if err != nil {
return CreateEmptyClusterValue(), err
}
res, err := client.executeCommandWithRoute(C.Echo, args, opts.Route)
if err != nil {
return CreateEmptyClusterValue(), err
}
data, err := handleInterfaceResponse(res)
if err != nil {
return CreateEmptyClusterValue(), err
}
return CreateClusterValue(data), nil
}
35 changes: 35 additions & 0 deletions go/api/options/echo_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package options

import (
"github.com/valkey-io/valkey-glide/go/glide/api/config"
)

type EchoOptions struct {
message string
Route config.Route
}

func NewEchoOptionsBuilder() *EchoOptions {
return &EchoOptions{}
}

func (echoOptions *EchoOptions) SetMessage(msg string) *EchoOptions {
echoOptions.message = msg
return echoOptions
}

func (echoOptions *EchoOptions) SetRoute(route config.Route) *EchoOptions {
echoOptions.Route = route
return echoOptions
}

func (opts *EchoOptions) ToArgs() ([]string, error) {
args := []string{}

if opts.message != "" {
args = append(args, opts.message)
}
return args, nil
}
12 changes: 12 additions & 0 deletions go/integTest/standalone_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,15 @@ func (suite *GlideTestSuite) TestSortReadOnlyWithOptions_SuccessfulSortByWeightA
assert.Equal(suite.T(), "item1", sortResult[3].Value())
assert.Equal(suite.T(), "item3", sortResult[5].Value())
}

func (suite *GlideTestSuite) TestEchoWithOptions_WithRoute() {
client := suite.defaultClient()
options := options.NewEchoOptionsBuilder().
SetRoute(config.SimpleNodeRoute(config.RandomRoute))

result, err := client.EchoWithOptions(options)

assert.NotNil(suite.T(), err)
assert.Equal(suite.T(), "", result)
assert.IsType(suite.T(), &errors.RequestError{}, err)
}

0 comments on commit 79a02da

Please sign in to comment.