Skip to content

Commit a1360c2

Browse files
Go: Added config folder for request_routing_config_test.go and errors folder for errors.go (valkey-io#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]>
1 parent a8d4b16 commit a1360c2

File tree

9 files changed

+218
-207
lines changed

9 files changed

+218
-207
lines changed

go/api/base_client.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ package api
1010
import "C"
1111

1212
import (
13-
"errors"
1413
"fmt"
1514
"math"
1615
"strconv"
1716
"unsafe"
1817

18+
"github.com/valkey-io/valkey-glide/go/glide/api/config"
19+
"github.com/valkey-io/valkey-glide/go/glide/api/errors"
1920
"github.com/valkey-io/valkey-glide/go/glide/api/options"
2021
"github.com/valkey-io/valkey-glide/go/glide/protobuf"
2122
"github.com/valkey-io/valkey-glide/go/glide/utils"
@@ -54,8 +55,10 @@ func successCallback(channelPtr unsafe.Pointer, cResponse *C.struct_CommandRespo
5455

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

6164
type clientConfiguration interface {
@@ -92,7 +95,7 @@ func createClient(config clientConfiguration) (*baseClient, error) {
9295
cErr := cResponse.connection_error_message
9396
if cErr != nil {
9497
message := C.GoString(cErr)
95-
return nil, &ConnectionError{message}
98+
return nil, &errors.ConnectionError{Msg: message}
9699
}
97100

98101
return &baseClient{cResponse.conn_ptr}, nil
@@ -115,10 +118,10 @@ func (client *baseClient) executeCommand(requestType C.RequestType, args []strin
115118
func (client *baseClient) executeCommandWithRoute(
116119
requestType C.RequestType,
117120
args []string,
118-
route route,
121+
route config.Route,
119122
) (*C.struct_CommandResponse, error) {
120123
if client.coreClient == nil {
121-
return nil, &ClosingError{"ExecuteCommand failed. The client is closed."}
124+
return nil, &errors.ClosingError{Msg: "ExecuteCommand failed. The client is closed."}
122125
}
123126
var cArgsPtr *C.uintptr_t = nil
124127
var argLengthsPtr *C.ulong = nil
@@ -134,9 +137,9 @@ func (client *baseClient) executeCommandWithRoute(
134137
var routeBytesPtr *C.uchar = nil
135138
var routeBytesCount C.uintptr_t = 0
136139
if route != nil {
137-
routeProto, err := route.toRoutesProtobuf()
140+
routeProto, err := route.ToRoutesProtobuf()
138141
if err != nil {
139-
return nil, &RequestError{"ExecuteCommand failed due to invalid route"}
142+
return nil, &errors.RequestError{Msg: "ExecuteCommand failed due to invalid route"}
140143
}
141144
msg, err := proto.Marshal(routeProto)
142145
if err != nil {
@@ -806,7 +809,7 @@ func (client *baseClient) LCS(key1 string, key2 string) (string, error) {
806809
// [valkey.io]: https://valkey.io/commands/getdel/
807810
func (client *baseClient) GetDel(key string) (Result[string], error) {
808811
if key == "" {
809-
return CreateNilStringResult(), errors.New("key is required")
812+
return CreateNilStringResult(), &errors.RequestError{Msg: "key is required"}
810813
}
811814

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

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

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

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

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

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

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

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

29412944
// args slice will have 5 more arguments with the keys provided.

go/api/command_options.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package api
55
import (
66
"strconv"
77

8+
"github.com/valkey-io/valkey-glide/go/glide/api/errors"
89
"github.com/valkey-io/valkey-glide/go/glide/utils"
910
)
1011

@@ -63,7 +64,7 @@ func (opts *SetOptions) toArgs() ([]string, error) {
6364
case KeepExisting:
6465
args = append(args, string(opts.Expiry.Type))
6566
default:
66-
err = &RequestError{"Invalid expiry type"}
67+
err = &errors.RequestError{Msg: "Invalid expiry type"}
6768
}
6869
}
6970

@@ -101,7 +102,7 @@ func (opts *GetExOptions) toArgs() ([]string, error) {
101102
case Persist:
102103
args = append(args, string(opts.Expiry.Type))
103104
default:
104-
err = &RequestError{"Invalid expiry type"}
105+
err = &errors.RequestError{Msg: "Invalid expiry type"}
105106
}
106107
}
107108

@@ -144,7 +145,7 @@ func (expireCondition ExpireCondition) toString() (string, error) {
144145
case NewExpiryLessThanCurrent:
145146
return string(NewExpiryLessThanCurrent), nil
146147
default:
147-
return "", &RequestError{"Invalid expire condition"}
148+
return "", &errors.RequestError{Msg: "Invalid expire condition"}
148149
}
149150
}
150151

@@ -254,7 +255,7 @@ func (insertPosition InsertPosition) toString() (string, error) {
254255
case After:
255256
return string(After), nil
256257
default:
257-
return "", &RequestError{"Invalid insert position"}
258+
return "", &errors.RequestError{Msg: "Invalid insert position"}
258259
}
259260
}
260261

@@ -275,7 +276,7 @@ func (listDirection ListDirection) toString() (string, error) {
275276
case Right:
276277
return string(Right), nil
277278
default:
278-
return "", &RequestError{"Invalid list direction"}
279+
return "", &errors.RequestError{Msg: "Invalid list direction"}
279280
}
280281
}
281282

go/api/request_routing_config.go renamed to go/api/config/request_routing_config.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
22

3-
package api
3+
package config
44

55
import (
66
"fmt"
77
"strconv"
88
"strings"
99

10+
"github.com/valkey-io/valkey-glide/go/glide/api/errors"
1011
"github.com/valkey-io/valkey-glide/go/glide/protobuf"
1112
)
1213

1314
// Request routing basic interface. Please use one of the following:
14-
// - [api.SimpleNodeRoute]
15-
// - [api.SlotIdRoute]
16-
// - [api.SlotKeyRoute]
17-
// - [api.ByAddressRoute]
18-
type route interface {
19-
toRoutesProtobuf() (*protobuf.Routes, error)
15+
// - [config.SimpleNodeRoute]
16+
// - [config.SlotIdRoute]
17+
// - [config.SlotKeyRoute]
18+
// - [config.ByAddressRoute]
19+
type Route interface {
20+
ToRoutesProtobuf() (*protobuf.Routes, error)
2021
}
2122

2223
type SimpleNodeRoute int
@@ -32,7 +33,7 @@ const (
3233
RandomRoute
3334
)
3435

35-
func (simpleNodeRoute SimpleNodeRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
36+
func (simpleNodeRoute SimpleNodeRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
3637
simpleRouteProto, err := mapSimpleNodeRoute(simpleNodeRoute)
3738
if err != nil {
3839
return nil, err
@@ -55,7 +56,7 @@ func mapSimpleNodeRoute(simpleNodeRoute SimpleNodeRoute) (protobuf.SimpleRoutes,
5556
case RandomRoute:
5657
return protobuf.SimpleRoutes_Random, nil
5758
default:
58-
return protobuf.SimpleRoutes_Random, &RequestError{"Invalid simple node route"}
59+
return protobuf.SimpleRoutes_Random, &errors.RequestError{Msg: "Invalid simple node route"}
5960
}
6061
}
6162

@@ -76,7 +77,7 @@ func mapSlotType(slotType SlotType) (protobuf.SlotTypes, error) {
7677
case SlotTypeReplica:
7778
return protobuf.SlotTypes_Replica, nil
7879
default:
79-
return protobuf.SlotTypes_Primary, &RequestError{"Invalid slot type"}
80+
return protobuf.SlotTypes_Primary, &errors.RequestError{Msg: "Invalid slot type"}
8081
}
8182
}
8283

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

97-
func (slotIdRoute *SlotIdRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
98+
func (slotIdRoute *SlotIdRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
9899
slotType, err := mapSlotType(slotIdRoute.slotType)
99100
if err != nil {
100101
return nil, err
@@ -124,7 +125,7 @@ func NewSlotKeyRoute(slotType SlotType, slotKey string) *SlotKeyRoute {
124125
return &SlotKeyRoute{slotType: slotType, slotKey: slotKey}
125126
}
126127

127-
func (slotKeyRoute *SlotKeyRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
128+
func (slotKeyRoute *SlotKeyRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
128129
slotType, err := mapSlotType(slotKeyRoute.slotType)
129130
if err != nil {
130131
return nil, err
@@ -159,17 +160,17 @@ func NewByAddressRoute(host string, port int32) *ByAddressRoute {
159160
func NewByAddressRouteWithHost(host string) (*ByAddressRoute, error) {
160161
split := strings.Split(host, ":")
161162
if len(split) != 2 {
162-
return nil, &RequestError{
163-
fmt.Sprintf(
163+
return nil, &errors.RequestError{
164+
Msg: fmt.Sprintf(
164165
"no port provided, or host is not in the expected format 'hostname:port'. Received: %s", host,
165166
),
166167
}
167168
}
168169

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

181-
func (byAddressRoute *ByAddressRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
182+
func (byAddressRoute *ByAddressRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
182183
request := &protobuf.Routes{
183184
Value: &protobuf.Routes_ByAddressRoute{
184185
ByAddressRoute: &protobuf.ByAddressRoute{

go/api/errors.go renamed to go/api/errors/errors.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
22

3-
package api
3+
package errors
44

55
// #cgo LDFLAGS: -L../target/release -lglide_rs
6-
// #include "../lib.h"
6+
// #include "../../lib.h"
77
import "C"
88

99
// ConnectionError is a client error that occurs when there is an error while connecting or when a connection
1010
// disconnects.
1111
type ConnectionError struct {
12-
msg string
12+
Msg string
1313
}
1414

15-
func (e *ConnectionError) Error() string { return e.msg }
15+
func (e *ConnectionError) Error() string { return e.Msg }
1616

1717
// RequestError is a client error that occurs when an error is reported during a request.
1818
type RequestError struct {
19-
msg string
19+
Msg string
2020
}
2121

22-
func (e *RequestError) Error() string { return e.msg }
22+
func (e *RequestError) Error() string { return e.Msg }
2323

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

4545
// ClosingError is a client error that indicates that the client has closed and is no longer usable.
4646
type ClosingError struct {
47-
msg string
47+
Msg string
4848
}
4949

50-
func (e *ClosingError) Error() string { return e.msg }
50+
func (e *ClosingError) Error() string { return e.Msg }
5151

52-
func goError(cErrorType C.RequestErrorType, cErrorMessage *C.char) error {
53-
defer C.free_error_message(cErrorMessage)
54-
msg := C.GoString(cErrorMessage)
52+
func GoError(cErrorType uint32, errorMessage string) error {
5553
switch cErrorType {
5654
case C.ExecAbort:
57-
return &ExecAbortError{msg}
55+
return &ExecAbortError{errorMessage}
5856
case C.Timeout:
59-
return &TimeoutError{msg}
57+
return &TimeoutError{errorMessage}
6058
case C.Disconnect:
61-
return &DisconnectError{msg}
59+
return &DisconnectError{errorMessage}
6260
default:
63-
return &RequestError{msg}
61+
return &RequestError{errorMessage}
6462
}
6563
}

0 commit comments

Comments
 (0)