Skip to content

Commit 184772b

Browse files
Go: Fix ClientGetName and ClientSetName (valkey-io#4088)
Co-authored-by: Yury-Fridlyand <yury.fridlyand@improving.com>
1 parent 1aa4b88 commit 184772b

10 files changed

+137
-54
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
* Add support for Intel MAC (x86_64/amd64) ([#3482](https://github.com/valkey-io/valkey-glide/pull/3482))
120120
* Go, Java: Fix response handling for `customCommand` API for cluster client ([#3593](https://github.com/valkey-io/valkey-glide/pull/3593))
121121
* Java: Bump `netty` version ([#3804](https://github.com/valkey-io/valkey-glide/pull/3804))
122+
* Go: `ClientGetName` returns a nullable string ([#4088](https://github.com/valkey-io/valkey-glide/pull/4088))
122123

123124
#### Operational Enhancements
124125

go/connection_management_cluster_commands_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func ExampleClusterClient_ClientSetName() {
9494
if err != nil {
9595
fmt.Println("Glide example failed with an error: ", err)
9696
}
97-
fmt.Println(result.SingleValue())
97+
fmt.Println(result)
9898

9999
// Output: OK
100100
}
@@ -107,7 +107,7 @@ func ExampleClusterClient_ClientGetName() {
107107
if err != nil {
108108
fmt.Println("Glide example failed with an error: ", err)
109109
}
110-
fmt.Println(result.SingleValue() == connectionName)
110+
fmt.Println(result.Value() == connectionName)
111111

112112
// Output: true
113113
}
@@ -120,7 +120,7 @@ func ExampleClusterClient_ClientSetNameWithOptions() {
120120
if err != nil {
121121
fmt.Println("Glide example failed with an error: ", err)
122122
}
123-
fmt.Println(result.SingleValue())
123+
fmt.Println(result)
124124

125125
// Output: OK
126126
}
@@ -134,7 +134,7 @@ func ExampleClusterClient_ClientGetNameWithOptions() {
134134
if err != nil {
135135
fmt.Println("Glide example failed with an error: ", err)
136136
}
137-
fmt.Println(result.SingleValue() == connectionName)
137+
fmt.Println(result.SingleValue().Value() == connectionName)
138138

139139
// Output: true
140140
}

go/connection_management_commands_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func ExampleClient_ClientGetName() {
7676
if err != nil {
7777
fmt.Println("Glide example failed with an error: ", err)
7878
}
79-
fmt.Println(result == connectionName)
79+
fmt.Println(result.Value() == connectionName)
8080

8181
// Output: true
8282
}

go/glide_client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,16 @@ func (client *Client) ConfigResetStat(ctx context.Context) (string, error) {
566566
//
567567
// Return value:
568568
//
569-
// The name of the client connection as a string if a name is set, or nil if no name is assigned.
569+
// If a name is set, returns the name of the client connection as a models.Result[string].
570+
// Otherwise, returns [models.CreateNilStringResult()] if no name is assigned.
570571
//
571572
// [valkey.io]: https://valkey.io/commands/client-getname/
572-
func (client *Client) ClientGetName(ctx context.Context) (string, error) {
573+
func (client *Client) ClientGetName(ctx context.Context) (models.Result[string], error) {
573574
result, err := client.executeCommand(ctx, C.ClientGetName, []string{})
574575
if err != nil {
575-
return models.DefaultStringResponse, err
576+
return models.CreateNilStringResult(), err
576577
}
577-
return handleStringResponse(result)
578+
return handleStringOrNilResponse(result)
578579
}
579580

580581
// Set the name of the current connection.

go/glide_cluster_client.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,16 +1154,16 @@ func (client *ClusterClient) ConfigGetWithOptions(ctx context.Context,
11541154
// OK - when connection name is set
11551155
//
11561156
// [valkey.io]: https://valkey.io/commands/client-setname/
1157-
func (client *ClusterClient) ClientSetName(ctx context.Context, connectionName string) (models.ClusterValue[string], error) {
1157+
func (client *ClusterClient) ClientSetName(ctx context.Context, connectionName string) (string, error) {
11581158
response, err := client.executeCommand(ctx, C.ClientSetName, []string{connectionName})
11591159
if err != nil {
1160-
return models.CreateEmptyClusterValue[string](), err
1160+
return models.DefaultStringResponse, err
11611161
}
11621162
data, err := handleOkResponse(response)
11631163
if err != nil {
1164-
return models.CreateEmptyClusterValue[string](), err
1164+
return models.DefaultStringResponse, err
11651165
}
1166-
return models.CreateClusterSingleValue[string](data), nil
1166+
return data, nil
11671167
}
11681168

11691169
// Set the name of the current connection.
@@ -1183,27 +1183,20 @@ func (client *ClusterClient) ClientSetName(ctx context.Context, connectionName s
11831183
func (client *ClusterClient) ClientSetNameWithOptions(ctx context.Context,
11841184
connectionName string,
11851185
opts options.RouteOption,
1186-
) (models.ClusterValue[string], error) {
1186+
) (string, error) {
11871187
response, err := client.executeCommandWithRoute(ctx, C.ClientSetName, []string{connectionName}, opts.Route)
11881188
if err != nil {
1189-
return models.CreateEmptyClusterValue[string](), err
1190-
}
1191-
if opts.Route != nil &&
1192-
(opts.Route).IsMultiNode() {
1193-
data, err := handleStringToStringMapResponse(response)
1194-
if err != nil {
1195-
return models.CreateEmptyClusterValue[string](), err
1196-
}
1197-
return models.CreateClusterMultiValue[string](data), nil
1189+
return models.DefaultStringResponse, err
11981190
}
11991191
data, err := handleOkResponse(response)
12001192
if err != nil {
1201-
return models.CreateEmptyClusterValue[string](), err
1193+
return models.DefaultStringResponse, err
12021194
}
1203-
return models.CreateClusterSingleValue[string](data), nil
1195+
return data, nil
12041196
}
12051197

12061198
// Gets the name of the current connection.
1199+
// The command will be routed to a random node.
12071200
//
12081201
// See [valkey.io] for details.
12091202
//
@@ -1213,19 +1206,20 @@ func (client *ClusterClient) ClientSetNameWithOptions(ctx context.Context,
12131206
//
12141207
// Return value:
12151208
//
1216-
// The name of the client connection as a string if a name is set, or nil if no name is assigned.
1209+
// If a name is set, returns the name of the client connection as a models.Result[string].
1210+
// Otherwise, returns [models.CreateNilStringResult()] if no name is assigned.
12171211
//
12181212
// [valkey.io]: https://valkey.io/commands/client-getname/
1219-
func (client *ClusterClient) ClientGetName(ctx context.Context) (models.ClusterValue[string], error) {
1213+
func (client *ClusterClient) ClientGetName(ctx context.Context) (models.Result[string], error) {
12201214
response, err := client.executeCommand(ctx, C.ClientGetName, []string{})
12211215
if err != nil {
1222-
return models.CreateEmptyClusterValue[string](), err
1216+
return models.CreateNilStringResult(), err
12231217
}
1224-
data, err := handleStringResponse(response)
1218+
data, err := handleStringOrNilResponse(response)
12251219
if err != nil {
1226-
return models.CreateEmptyClusterValue[string](), err
1220+
return models.CreateNilStringResult(), err
12271221
}
1228-
return models.CreateClusterSingleValue[string](data), nil
1222+
return data, nil
12291223
}
12301224

12311225
// Gets the name of the current connection.
@@ -1240,30 +1234,31 @@ func (client *ClusterClient) ClientGetName(ctx context.Context) (models.ClusterV
12401234
//
12411235
// Return value:
12421236
//
1243-
// The name of the client connection as a string if a name is set, or nil if no name is assigned.
1237+
// If a name is set, returns the name of the client connection as a ClusterValue wrapped models.Result[string].
1238+
// Otherwise, returns [models.CreateEmptyClusterValue[models.Result[string]]()] if no name is assigned.
12441239
//
12451240
// [valkey.io]: https://valkey.io/commands/client-getname/
12461241
func (client *ClusterClient) ClientGetNameWithOptions(
12471242
ctx context.Context,
12481243
opts options.RouteOption,
1249-
) (models.ClusterValue[string], error) {
1244+
) (models.ClusterValue[models.Result[string]], error) {
12501245
response, err := client.executeCommandWithRoute(ctx, C.ClientGetName, []string{}, opts.Route)
12511246
if err != nil {
1252-
return models.CreateEmptyClusterValue[string](), err
1247+
return models.CreateEmptyClusterValue[models.Result[string]](), err
12531248
}
12541249
if opts.Route != nil &&
12551250
(opts.Route).IsMultiNode() {
1256-
data, err := handleStringToStringMapResponse(response)
1251+
data, err := handleStringToStringOrNilMapResponse(response)
12571252
if err != nil {
1258-
return models.CreateEmptyClusterValue[string](), err
1253+
return models.CreateEmptyClusterValue[models.Result[string]](), err
12591254
}
1260-
return models.CreateClusterMultiValue[string](data), nil
1255+
return models.CreateClusterMultiValue[models.Result[string]](data), nil
12611256
}
1262-
data, err := handleStringResponse(response)
1257+
data, err := handleStringOrNilResponse(response)
12631258
if err != nil {
1264-
return models.CreateEmptyClusterValue[string](), err
1259+
return models.CreateEmptyClusterValue[models.Result[string]](), err
12651260
}
1266-
return models.CreateClusterSingleValue[string](data), nil
1261+
return models.CreateClusterSingleValue[models.Result[string]](data), nil
12671262
}
12681263

12691264
// Rewrites the configuration file with the current configuration.

go/integTest/cluster_commands_test.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,14 +1312,51 @@ func (suite *GlideTestSuite) TestConfigSetGetWithOptions() {
13121312
}
13131313
}
13141314

1315+
func (suite *GlideTestSuite) TestClusterClientGetName() {
1316+
client := suite.defaultClusterClient()
1317+
t := suite.T()
1318+
1319+
response, err := client.ClientGetName(context.Background())
1320+
assert.NoError(t, err)
1321+
assert.True(t, response.IsNil())
1322+
}
1323+
1324+
func (suite *GlideTestSuite) TestClusterClientGetNameWithRoute() {
1325+
client := suite.defaultClusterClient()
1326+
t := suite.T()
1327+
1328+
route := config.Route(config.RandomRoute)
1329+
opts := options.RouteOption{Route: route}
1330+
1331+
response, err := client.ClientGetNameWithOptions(context.Background(), opts)
1332+
assert.NoError(t, err)
1333+
assert.True(t, response.IsSingleValue())
1334+
assert.True(t, response.SingleValue().IsNil())
1335+
}
1336+
1337+
func (suite *GlideTestSuite) TestClusterClientGetNameWithMultiNodeRoutes() {
1338+
client := suite.defaultClusterClient()
1339+
t := suite.T()
1340+
1341+
route := config.Route(config.AllPrimaries)
1342+
opts := options.RouteOption{Route: route}
1343+
1344+
response, err := client.ClientGetNameWithOptions(context.Background(), opts)
1345+
assert.NoError(t, err)
1346+
assert.True(t, response.IsMultiValue())
1347+
for _, value := range response.MultiValue() {
1348+
assert.True(t, value.IsNil())
1349+
}
1350+
}
1351+
13151352
func (suite *GlideTestSuite) TestClientSetGetName() {
13161353
client := suite.defaultClusterClient()
13171354
t := suite.T()
13181355
connectionName := "ConnectionName-" + uuid.NewString()
13191356
client.ClientSetName(context.Background(), connectionName)
13201357
response, err := client.ClientGetName(context.Background())
13211358
assert.NoError(t, err)
1322-
assert.True(t, response.IsSingleValue())
1359+
assert.Equal(t, connectionName, response.Value())
13231360
}
13241361

13251362
func (suite *GlideTestSuite) TestClientSetGetNameWithRoute() {
@@ -1330,22 +1367,32 @@ func (suite *GlideTestSuite) TestClientSetGetNameWithRoute() {
13301367
opts := options.RouteOption{Route: nil}
13311368
connectionName := "ConnectionName-" + uuid.NewString()
13321369
response, err := client.ClientSetNameWithOptions(context.Background(), connectionName, opts)
1370+
suite.verifyOK(response, err)
1371+
response2, err := client.ClientGetNameWithOptions(context.Background(), opts)
13331372
assert.NoError(t, err)
1334-
assert.True(t, response.IsSingleValue())
1335-
response, err = client.ClientGetNameWithOptions(context.Background(), opts)
1336-
assert.NoError(t, err)
1337-
assert.True(t, response.IsSingleValue())
1373+
assert.True(t, response2.IsSingleValue())
13381374

13391375
// same sections with random route
13401376
connectionName = "ConnectionName-" + uuid.NewString()
13411377
route := config.Route(config.RandomRoute)
13421378
opts = options.RouteOption{Route: route}
13431379
response, err = client.ClientSetNameWithOptions(context.Background(), connectionName, opts)
1380+
suite.verifyOK(response, err)
1381+
response2, err = client.ClientGetNameWithOptions(context.Background(), opts)
13441382
assert.NoError(t, err)
1345-
assert.True(t, response.IsSingleValue())
1346-
response, err = client.ClientGetNameWithOptions(context.Background(), opts)
1383+
assert.True(t, response2.IsSingleValue())
1384+
1385+
// same sections with multinode routes
1386+
connectionName = "ConnectionName-" + uuid.NewString()
1387+
route = config.Route(config.AllPrimaries)
1388+
opts = options.RouteOption{Route: route}
1389+
response, err = client.ClientSetNameWithOptions(context.Background(), connectionName, opts)
1390+
suite.verifyOK(response, err)
1391+
response2, err = client.ClientGetNameWithOptions(context.Background(), opts)
13471392
assert.NoError(t, err)
1348-
assert.True(t, response.IsSingleValue())
1393+
for _, data := range response2.MultiValue() {
1394+
assert.Equal(t, connectionName, data.Value())
1395+
}
13491396
}
13501397

13511398
func (suite *GlideTestSuite) TestConfigRewriteCluster() {

go/integTest/standalone_commands_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,14 +836,23 @@ func (suite *GlideTestSuite) TestConfigResetStat() {
836836
suite.verifyOK(client.ConfigResetStat(context.Background()))
837837
}
838838

839+
func (suite *GlideTestSuite) TestClientGetName() {
840+
client := suite.defaultClient()
841+
t := suite.T()
842+
843+
result, err := client.ClientGetName(context.Background())
844+
assert.Nil(t, err)
845+
assert.True(t, result.IsNil())
846+
}
847+
839848
func (suite *GlideTestSuite) TestClientGetSetName() {
840849
client := suite.defaultClient()
841850
t := suite.T()
842851

843852
suite.verifyOK(client.ClientSetName(context.Background(), "ConnectionName"))
844853
result, err := client.ClientGetName(context.Background())
845854
assert.Nil(t, err)
846-
assert.Equal(t, result, "ConnectionName")
855+
assert.Equal(t, result.Value(), "ConnectionName")
847856
}
848857

849858
func (suite *GlideTestSuite) TestMove() {

go/internal/interfaces/connection_management_cluster_commands.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ type ConnectionManagementClusterCommands interface {
2727

2828
ClientIdWithOptions(ctx context.Context, routeOptions options.RouteOption) (models.ClusterValue[int64], error)
2929

30-
ClientSetName(ctx context.Context, connectionName string) (models.ClusterValue[string], error)
30+
ClientSetName(ctx context.Context, connectionName string) (string, error)
3131

3232
ClientSetNameWithOptions(
3333
ctx context.Context,
3434
connectionName string,
3535
routeOptions options.RouteOption,
36-
) (models.ClusterValue[string], error)
36+
) (string, error)
3737

38-
ClientGetName(ctx context.Context) (models.ClusterValue[string], error)
38+
ClientGetName(ctx context.Context) (models.Result[string], error)
3939

40-
ClientGetNameWithOptions(ctx context.Context, routeOptions options.RouteOption) (models.ClusterValue[string], error)
40+
ClientGetNameWithOptions(
41+
ctx context.Context,
42+
routeOptions options.RouteOption,
43+
) (models.ClusterValue[models.Result[string]], error)
4144
}

go/internal/interfaces/connection_management_commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ConnectionManagementCommands interface {
2323

2424
ClientId(ctx context.Context) (int64, error)
2525

26-
ClientGetName(ctx context.Context) (string, error)
26+
ClientGetName(ctx context.Context) (models.Result[string], error)
2727

2828
ClientSetName(ctx context.Context, connectionName string) (string, error)
2929
}

go/response_handlers.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,33 @@ func handleStringToStringMapResponse(response *C.struct_CommandResponse) (map[st
742742
return result, nil
743743
}
744744

745+
func handleStringToStringOrNilMapResponse(response *C.struct_CommandResponse) (map[string]models.Result[string], error) {
746+
defer C.free_command_response(response)
747+
748+
typeErr := checkResponseType(response, C.Map, false)
749+
if typeErr != nil {
750+
return nil, typeErr
751+
}
752+
753+
data, err := parseMap(response)
754+
if err != nil {
755+
return nil, err
756+
}
757+
aMap := data.(map[string]any)
758+
result := map[string]models.Result[string]{}
759+
760+
// Transform into Result[string]
761+
for nodeAddr, nodeData := range aMap {
762+
if nodeData == nil {
763+
result[nodeAddr] = models.CreateNilStringResult()
764+
continue
765+
}
766+
result[nodeAddr] = models.CreateStringResult(nodeData.(string))
767+
}
768+
769+
return result, nil
770+
}
771+
745772
func handleStringToStringArrayMapOrNilResponse(
746773
response *C.struct_CommandResponse,
747774
) (map[string][]string, error) {

0 commit comments

Comments
 (0)