Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Implement Custom Command with route(Generic Cluster cmd), Ping with Route #2979

Merged
merged 27 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e25d23e
Custom Cmd(Generic Cluster cmd), Ping with Route
niharikabhavaraju Jan 20, 2025
d13523b
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 20, 2025
46ba328
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 20, 2025
b8a7720
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 21, 2025
b082b30
Added Ping , PingWithOptions
niharikabhavaraju Jan 22, 2025
91806cd
Minor changes
niharikabhavaraju Jan 22, 2025
17b1ed8
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 22, 2025
51884e6
Minor lint fix and error fix
niharikabhavaraju Jan 22, 2025
eb60104
Fixed failing tests
niharikabhavaraju Jan 22, 2025
0fd48d2
Fixed linting error
niharikabhavaraju Jan 22, 2025
2761f5d
Fixed lint error
niharikabhavaraju Jan 22, 2025
767e001
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 24, 2025
d6a5c21
Fixed review comments
niharikabhavaraju Jan 27, 2025
1de3c7d
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 27, 2025
2e3d2d7
Minor comments changes
niharikabhavaraju Jan 27, 2025
8ed86c5
Merge branch 'pingcommand-niharika' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 27, 2025
6b9a527
Fixed indents
niharikabhavaraju Jan 27, 2025
7e2d86d
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 27, 2025
2ef3929
Fixed Response for Ping
niharikabhavaraju Jan 27, 2025
eacc4a8
Merge branch 'pingcommand-niharika' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 27, 2025
b8cf9a5
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 28, 2025
d725726
Fixed code review comments
niharikabhavaraju Jan 28, 2025
88a7a23
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 28, 2025
ac3b8ff
Merge branch 'main' into pingcommand-niharika
niharikabhavaraju Jan 29, 2025
a3f6549
Fixed review comments and failing tests
niharikabhavaraju Jan 29, 2025
23b0bd3
Fixed failed tests
niharikabhavaraju Jan 29, 2025
2c04be7
Update go/api/glide_cluster_client.go
Yury-Fridlyand Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type BaseClient interface {
SetCommands
StreamCommands
SortedSetCommands
ConnectionManagementCommands
HyperLogLogCommands
GenericBaseCommands
BitmapCommands
Expand Down Expand Up @@ -3104,52 +3103,6 @@ func (client *baseClient) BLMove(
return handleStringOrNilResponse(result)
}

// Pings the server.
//
// Return value:
//
// Returns "PONG".
//
// For example:
//
// result, err := client.Ping()
//
// [valkey.io]: https://valkey.io/commands/ping/
func (client *baseClient) Ping() (string, error) {
result, err := client.executeCommand(C.Ping, []string{})
if err != nil {
return defaultStringResponse, err
}

return handleStringResponse(result)
}

// Pings the server with a custom message.
//
// Parameters:
//
// message - A message to include in the `PING` command.
//
// Return value:
//
// Returns the copy of message.
//
// For example:
//
// result, err := client.PingWithMessage("Hello")
//
// [valkey.io]: https://valkey.io/commands/ping/
func (client *baseClient) PingWithMessage(message string) (string, error) {
args := []string{message}

result, err := client.executeCommand(C.Ping, args)
if err != nil {
return defaultStringResponse, err
}

return handleStringResponse(result)
}

// Del removes the specified keys from the database. A key is ignored if it does not exist.
//
// Note:
Expand Down Expand Up @@ -5595,34 +5548,6 @@ func (client *baseClient) ObjectEncoding(key string) (Result[string], error) {
return handleStringOrNilResponse(result)
}

// Echo the provided message back.
// The command will be routed a random node.
//
// Parameters:
//
// message - The provided message.
//
// Return value:
//
// The provided message
//
// For example:
//
// result, err := client.Echo("Hello World")
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: Hello World
//
// [valkey.io]: https://valkey.io/commands/echo/
func (client *baseClient) Echo(message string) (Result[string], error) {
result, err := client.executeCommand(C.Echo, []string{message})
if err != nil {
return CreateNilStringResult(), err
}
return handleStringOrNilResponse(result)
}

// Destroys the consumer group `group` for the stream stored at `key`.
//
// See [valkey.io] for details.
Expand Down
16 changes: 16 additions & 0 deletions go/api/connection_management_cluster_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 {
Ping() (string, error)

PingWithOptions(pingOptions options.ClusterPingOptions) (string, error)
}
4 changes: 3 additions & 1 deletion go/api/connection_management_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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 standalone client.
//
// See [valkey.io] for details.
Expand All @@ -10,7 +12,7 @@ package api
type ConnectionManagementCommands interface {
Ping() (string, error)

PingWithMessage(message string) (string, error)
PingWithOptions(pingOptions options.PingOptions) (string, error)

Echo(message string) (Result[string], error)
}
4 changes: 4 additions & 0 deletions go/api/generic_cluster_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

package api

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

// GenericClusterCommands supports commands for the "Generic Commands" group for cluster client.
//
// See [valkey.io] for details.
//
// [valkey.io]: https://valkey.io/commands/#generic
type GenericClusterCommands interface {
CustomCommand(args []string) (ClusterValue[interface{}], error)

CustomCommandWithRoute(args []string, route config.Route) (ClusterValue[interface{}], error)
}
71 changes: 71 additions & 0 deletions go/api/glide_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package api
import "C"

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

Expand All @@ -19,6 +20,7 @@ type GlideClientCommands interface {
GenericCommands
ServerManagementCommands
BitmapCommands
ConnectionManagementCommands
}

// GlideClient implements standalone mode operations by extending baseClient functionality.
Expand Down Expand Up @@ -225,3 +227,72 @@ func (client *GlideClient) DBSize() (int64, error) {
}
return handleIntResponse(result)
}

// Echo the provided message back.
// The command will be routed a random node.
//
// Parameters:
//
// message - The provided message.
//
// Return value:
//
// The provided message
//
// For example:
//
// result, err := client.Echo("Hello World")
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: Hello World
//
// [valkey.io]: https://valkey.io/commands/echo/
func (client *GlideClient) Echo(message string) (Result[string], error) {
result, err := client.executeCommand(C.Echo, []string{message})
if err != nil {
return CreateNilStringResult(), err
}
return handleStringOrNilResponse(result)
}

// Pings the server.
//
// Return value:
//
// Returns "PONG".
//
// For example:
//
// result, err := client.Ping()
// fmt.Println(result) // Output: PONG
//
// [valkey.io]: https://valkey.io/commands/ping/
func (client *GlideClient) Ping() (string, error) {
return client.PingWithOptions(options.PingOptions{})
}

// Pings the server.
//
// Parameters:
//
// pingOptions - The PingOptions type.
//
// Return value:
//
// Returns the copy of message.
//
// For example:
//
// options := options.NewPingOptionsBuilder().SetMessage("hello")
// result, err := client.PingWithOptions(options)
// result: "hello"
//
// [valkey.io]: https://valkey.io/commands/ping/
func (client *GlideClient) PingWithOptions(pingOptions options.PingOptions) (string, error) {
result, err := client.executeCommand(C.Ping, pingOptions.ToArgs())
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}
106 changes: 106 additions & 0 deletions go/api/glide_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ package api
// #include "../lib.h"
import "C"

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

// GlideClusterClient interface compliance check.
var _ GlideClusterClientCommands = (*GlideClusterClient)(nil)

Expand All @@ -14,6 +19,7 @@ type GlideClusterClientCommands interface {
BaseClient
GenericClusterCommands
ServerManagementClusterCommands
ConnectionManagementClusterCommands
}

// GlideClusterClient implements cluster mode operations by extending baseClient functionality.
Expand Down Expand Up @@ -160,3 +166,103 @@ func (client *GlideClusterClient) InfoWithOptions(options ClusterInfoOptions) (C
}
return createClusterSingleValue[string](data), nil
}

// CustomCommandWithRoute executes a single command, specified by args, without checking inputs. Every part of the command,
// including the command name and subcommands, should be added as a separate value in args. The returning value depends on
// the executed command.
//
// See [Valkey GLIDE Wiki] for details on the restrictions and limitations of the custom command API.
//
// Parameters:
//
// args - Arguments for the custom command including the command name.
// route - Specifies the routing configuration for the command. The client will route the
// command to the nodes defined by route.
//
// Return value:
//
// The returning value depends on the executed command and route.
//
// For example:
//
// route := config.SimpleNodeRoute(config.RandomRoute)
// result, err := client.CustomCommandWithRoute([]string{"ping"}, route)
// result.SingleValue().(string): "PONG"
//
// [Valkey GLIDE Wiki]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#custom-command
func (client *GlideClusterClient) CustomCommandWithRoute(
args []string,
route config.Route,
) (ClusterValue[interface{}], error) {
res, err := client.executeCommandWithRoute(C.CustomCommand, args, route)
if err != nil {
return createEmptyClusterValue[interface{}](), err
}
data, err := handleInterfaceResponse(res)
if err != nil {
return createEmptyClusterValue[interface{}](), err
}
return createClusterValue[interface{}](data), nil
}

// Pings the server.
// The command will be routed to all primary nodes.
//
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
// Return value:
//
// Returns "PONG".
//
// For example:
//
// result, err := clusterClient.Ping()
// fmt.Println(result) // Output: PONG
//
// [valkey.io]: https://valkey.io/commands/ping/
func (client *GlideClusterClient) Ping() (string, error) {
result, err := client.executeCommand(C.Ping, []string{})
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}

// Pings the server.
// The command will be routed to all primary nodes, unless `Route` is provided in `pingOptions`.
//
// Parameters:
//
// pingOptions - The PingOptions type.
//
// Return value:
//
// Returns the copy of message.
//
// For example:
//
// route := config.Route(config.RandomRoute)
// opts := options.ClusterPingOptions{
// PingOptions: &options.PingOptions{
// Message: "Hello",
// },
// Route: &route,
// }
// result, err := clusterClient.PingWithOptions(opts)
// fmt.Println(result) // Output: Hello
//
// [valkey.io]: https://valkey.io/commands/ping/
func (client *GlideClusterClient) PingWithOptions(pingOptions options.ClusterPingOptions) (string, error) {
if pingOptions.Route == nil {
response, err := client.executeCommand(C.Ping, pingOptions.ToArgs())
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(response)
}

response, err := client.executeCommandWithRoute(C.Ping, pingOptions.ToArgs(), *pingOptions.Route)
if err != nil {
return defaultStringResponse, err
}

return handleStringResponse(response)
}
31 changes: 31 additions & 0 deletions go/api/options/ping_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package options

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

// Optional arguments for `Ping` for standalone client
type PingOptions struct {
Message string
}

// Optional arguments for `Ping` for cluster client
type ClusterPingOptions struct {
*PingOptions
// Specifies the routing configuration for the command.
// The client will route the command to the nodes defined by *Route*.
Route *config.Route
}

func (opts *PingOptions) ToArgs() []string {
if opts == nil {
return []string{}
}
args := []string{}
if opts.Message != "" {
args = append(args, opts.Message)
}
return args
}
Loading
Loading