Skip to content

Commit

Permalink
Go: Added config folder for request_routing_config_test.go and errors…
Browse files Browse the repository at this point in the history
… folder for errors.go (#2996)

* Implemented Time Command

Signed-off-by: Niharika Bhavaraju <[email protected]>

* Fixed code review changes

Signed-off-by: Niharika Bhavaraju <[email protected]>

* Added Time command (cluster)

Signed-off-by: Niharika Bhavaraju <[email protected]>

* Fixed code review comments

Signed-off-by: Niharika Bhavaraju <[email protected]>

* Added route,errors folders

Signed-off-by: Niharika Bhavaraju <[email protected]>

* Fixed failing tests

Signed-off-by: Niharika Bhavaraju <[email protected]>

---------

Signed-off-by: Niharika Bhavaraju <[email protected]>
  • Loading branch information
niharikabhavaraju authored and EdricCua committed Jan 24, 2025
1 parent 1ef51a9 commit 039d55e
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 207 deletions.
27 changes: 15 additions & 12 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ package api
import "C"

import (
"errors"
"fmt"
"math"
"strconv"
"unsafe"

"github.com/valkey-io/valkey-glide/go/glide/api/config"
"github.com/valkey-io/valkey-glide/go/glide/api/errors"
"github.com/valkey-io/valkey-glide/go/glide/api/options"
"github.com/valkey-io/valkey-glide/go/glide/protobuf"
"github.com/valkey-io/valkey-glide/go/glide/utils"
Expand Down Expand Up @@ -54,8 +55,10 @@ func successCallback(channelPtr unsafe.Pointer, cResponse *C.struct_CommandRespo

//export failureCallback
func failureCallback(channelPtr unsafe.Pointer, cErrorMessage *C.char, cErrorType C.RequestErrorType) {
defer C.free_error_message(cErrorMessage)
msg := C.GoString(cErrorMessage)
resultChannel := *(*chan payload)(channelPtr)
resultChannel <- payload{value: nil, error: goError(cErrorType, cErrorMessage)}
resultChannel <- payload{value: nil, error: errors.GoError(uint32(cErrorType), msg)}
}

type clientConfiguration interface {
Expand Down Expand Up @@ -92,7 +95,7 @@ func createClient(config clientConfiguration) (*baseClient, error) {
cErr := cResponse.connection_error_message
if cErr != nil {
message := C.GoString(cErr)
return nil, &ConnectionError{message}
return nil, &errors.ConnectionError{Msg: message}
}

return &baseClient{cResponse.conn_ptr}, nil
Expand All @@ -115,10 +118,10 @@ func (client *baseClient) executeCommand(requestType C.RequestType, args []strin
func (client *baseClient) executeCommandWithRoute(
requestType C.RequestType,
args []string,
route route,
route config.Route,
) (*C.struct_CommandResponse, error) {
if client.coreClient == nil {
return nil, &ClosingError{"ExecuteCommand failed. The client is closed."}
return nil, &errors.ClosingError{Msg: "ExecuteCommand failed. The client is closed."}
}
var cArgsPtr *C.uintptr_t = nil
var argLengthsPtr *C.ulong = nil
Expand All @@ -134,9 +137,9 @@ func (client *baseClient) executeCommandWithRoute(
var routeBytesPtr *C.uchar = nil
var routeBytesCount C.uintptr_t = 0
if route != nil {
routeProto, err := route.toRoutesProtobuf()
routeProto, err := route.ToRoutesProtobuf()
if err != nil {
return nil, &RequestError{"ExecuteCommand failed due to invalid route"}
return nil, &errors.RequestError{Msg: "ExecuteCommand failed due to invalid route"}
}
msg, err := proto.Marshal(routeProto)
if err != nil {
Expand Down Expand Up @@ -806,7 +809,7 @@ func (client *baseClient) LCS(key1 string, key2 string) (string, error) {
// [valkey.io]: https://valkey.io/commands/getdel/
func (client *baseClient) GetDel(key string) (Result[string], error) {
if key == "" {
return CreateNilStringResult(), errors.New("key is required")
return CreateNilStringResult(), &errors.RequestError{Msg: "key is required"}
}

result, err := client.executeCommand(C.GetDel, []string{key})
Expand Down Expand Up @@ -2758,7 +2761,7 @@ func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map

// Check for potential length overflow.
if len(keys) > math.MaxInt-2 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 2 more arguments with the keys provided.
Expand Down Expand Up @@ -2811,7 +2814,7 @@ func (client *baseClient) LMPopCount(

// Check for potential length overflow.
if len(keys) > math.MaxInt-4 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 4 more arguments with the keys provided.
Expand Down Expand Up @@ -2871,7 +2874,7 @@ func (client *baseClient) BLMPop(

// Check for potential length overflow.
if len(keys) > math.MaxInt-3 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 3 more arguments with the keys provided.
Expand Down Expand Up @@ -2935,7 +2938,7 @@ func (client *baseClient) BLMPopCount(

// Check for potential length overflow.
if len(keys) > math.MaxInt-5 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 5 more arguments with the keys provided.
Expand Down
11 changes: 6 additions & 5 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package api
import (
"strconv"

"github.com/valkey-io/valkey-glide/go/glide/api/errors"
"github.com/valkey-io/valkey-glide/go/glide/utils"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func (opts *SetOptions) toArgs() ([]string, error) {
case KeepExisting:
args = append(args, string(opts.Expiry.Type))
default:
err = &RequestError{"Invalid expiry type"}
err = &errors.RequestError{Msg: "Invalid expiry type"}
}
}

Expand Down Expand Up @@ -101,7 +102,7 @@ func (opts *GetExOptions) toArgs() ([]string, error) {
case Persist:
args = append(args, string(opts.Expiry.Type))
default:
err = &RequestError{"Invalid expiry type"}
err = &errors.RequestError{Msg: "Invalid expiry type"}
}
}

Expand Down Expand Up @@ -144,7 +145,7 @@ func (expireCondition ExpireCondition) toString() (string, error) {
case NewExpiryLessThanCurrent:
return string(NewExpiryLessThanCurrent), nil
default:
return "", &RequestError{"Invalid expire condition"}
return "", &errors.RequestError{Msg: "Invalid expire condition"}
}
}

Expand Down Expand Up @@ -254,7 +255,7 @@ func (insertPosition InsertPosition) toString() (string, error) {
case After:
return string(After), nil
default:
return "", &RequestError{"Invalid insert position"}
return "", &errors.RequestError{Msg: "Invalid insert position"}
}
}

Expand All @@ -275,7 +276,7 @@ func (listDirection ListDirection) toString() (string, error) {
case Right:
return string(Right), nil
default:
return "", &RequestError{"Invalid list direction"}
return "", &errors.RequestError{Msg: "Invalid list direction"}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api
package config

import (
"fmt"
"strconv"
"strings"

"github.com/valkey-io/valkey-glide/go/glide/api/errors"
"github.com/valkey-io/valkey-glide/go/glide/protobuf"
)

// Request routing basic interface. Please use one of the following:
// - [api.SimpleNodeRoute]
// - [api.SlotIdRoute]
// - [api.SlotKeyRoute]
// - [api.ByAddressRoute]
type route interface {
toRoutesProtobuf() (*protobuf.Routes, error)
// - [config.SimpleNodeRoute]
// - [config.SlotIdRoute]
// - [config.SlotKeyRoute]
// - [config.ByAddressRoute]
type Route interface {
ToRoutesProtobuf() (*protobuf.Routes, error)
}

type SimpleNodeRoute int
Expand All @@ -32,7 +33,7 @@ const (
RandomRoute
)

func (simpleNodeRoute SimpleNodeRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (simpleNodeRoute SimpleNodeRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
simpleRouteProto, err := mapSimpleNodeRoute(simpleNodeRoute)
if err != nil {
return nil, err
Expand All @@ -55,7 +56,7 @@ func mapSimpleNodeRoute(simpleNodeRoute SimpleNodeRoute) (protobuf.SimpleRoutes,
case RandomRoute:
return protobuf.SimpleRoutes_Random, nil
default:
return protobuf.SimpleRoutes_Random, &RequestError{"Invalid simple node route"}
return protobuf.SimpleRoutes_Random, &errors.RequestError{Msg: "Invalid simple node route"}
}
}

Expand All @@ -76,7 +77,7 @@ func mapSlotType(slotType SlotType) (protobuf.SlotTypes, error) {
case SlotTypeReplica:
return protobuf.SlotTypes_Replica, nil
default:
return protobuf.SlotTypes_Primary, &RequestError{"Invalid slot type"}
return protobuf.SlotTypes_Primary, &errors.RequestError{Msg: "Invalid slot type"}
}
}

Expand All @@ -94,7 +95,7 @@ func NewSlotIdRoute(slotType SlotType, slotId int32) *SlotIdRoute {
return &SlotIdRoute{slotType: slotType, slotID: slotId}
}

func (slotIdRoute *SlotIdRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (slotIdRoute *SlotIdRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
slotType, err := mapSlotType(slotIdRoute.slotType)
if err != nil {
return nil, err
Expand Down Expand Up @@ -124,7 +125,7 @@ func NewSlotKeyRoute(slotType SlotType, slotKey string) *SlotKeyRoute {
return &SlotKeyRoute{slotType: slotType, slotKey: slotKey}
}

func (slotKeyRoute *SlotKeyRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (slotKeyRoute *SlotKeyRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
slotType, err := mapSlotType(slotKeyRoute.slotType)
if err != nil {
return nil, err
Expand Down Expand Up @@ -159,17 +160,17 @@ func NewByAddressRoute(host string, port int32) *ByAddressRoute {
func NewByAddressRouteWithHost(host string) (*ByAddressRoute, error) {
split := strings.Split(host, ":")
if len(split) != 2 {
return nil, &RequestError{
fmt.Sprintf(
return nil, &errors.RequestError{
Msg: fmt.Sprintf(
"no port provided, or host is not in the expected format 'hostname:port'. Received: %s", host,
),
}
}

port, err := strconv.ParseInt(split[1], 10, 32)
if err != nil {
return nil, &RequestError{
fmt.Sprintf(
return nil, &errors.RequestError{
Msg: fmt.Sprintf(
"port must be a valid integer. Received: %s", split[1],
),
}
Expand All @@ -178,7 +179,7 @@ func NewByAddressRouteWithHost(host string) (*ByAddressRoute, error) {
return &ByAddressRoute{host: split[0], port: int32(port)}, nil
}

func (byAddressRoute *ByAddressRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (byAddressRoute *ByAddressRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
request := &protobuf.Routes{
Value: &protobuf.Routes_ByAddressRoute{
ByAddressRoute: &protobuf.ByAddressRoute{
Expand Down
28 changes: 13 additions & 15 deletions go/api/errors.go → go/api/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api
package errors

// #cgo LDFLAGS: -L../target/release -lglide_rs
// #include "../lib.h"
// #include "../../lib.h"
import "C"

// ConnectionError is a client error that occurs when there is an error while connecting or when a connection
// disconnects.
type ConnectionError struct {
msg string
Msg string
}

func (e *ConnectionError) Error() string { return e.msg }
func (e *ConnectionError) Error() string { return e.Msg }

// RequestError is a client error that occurs when an error is reported during a request.
type RequestError struct {
msg string
Msg string
}

func (e *RequestError) Error() string { return e.msg }
func (e *RequestError) Error() string { return e.Msg }

// ExecAbortError is a client error that occurs when a transaction is aborted.
type ExecAbortError struct {
Expand All @@ -44,22 +44,20 @@ func (e *DisconnectError) Error() string { return e.msg }

// ClosingError is a client error that indicates that the client has closed and is no longer usable.
type ClosingError struct {
msg string
Msg string
}

func (e *ClosingError) Error() string { return e.msg }
func (e *ClosingError) Error() string { return e.Msg }

func goError(cErrorType C.RequestErrorType, cErrorMessage *C.char) error {
defer C.free_error_message(cErrorMessage)
msg := C.GoString(cErrorMessage)
func GoError(cErrorType uint32, errorMessage string) error {
switch cErrorType {
case C.ExecAbort:
return &ExecAbortError{msg}
return &ExecAbortError{errorMessage}
case C.Timeout:
return &TimeoutError{msg}
return &TimeoutError{errorMessage}
case C.Disconnect:
return &DisconnectError{msg}
return &DisconnectError{errorMessage}
default:
return &RequestError{msg}
return &RequestError{errorMessage}
}
}
Loading

0 comments on commit 039d55e

Please sign in to comment.