Skip to content

Commit 2b908ac

Browse files
Merge pull request valkey-io#3077 from EdricCua/Go-Implement-ClientId
Go: Implement ClientId
2 parents ba08ade + d000e7c commit 2b908ac

10 files changed

+177
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Go/Core: Move FFI to a dedicated folder for reusability ([#3372](https://github.com/valkey-io/valkey-glide/pull/3372))
2929
* Go: Add `GeoPos` ([#3409](https://github.com/valkey-io/valkey-glide/pull/3409))
3030
* Go: Add `GeoDist` ([#3446](https://github.com/valkey-io/valkey-glide/pull/3446))
31+
* Go: Add `ClientId` ([#3077](https://github.com/valkey-io/valkey-glide/pull/3077))
3132

3233
#### Breaking Changes
3334

go/api/connection_management_cluster_commands.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ type ConnectionManagementClusterCommands interface {
1717
Echo(message string) (Result[string], error)
1818

1919
EchoWithOptions(echoOptions options.ClusterEchoOptions) (ClusterValue[string], error)
20+
21+
ClientId() (ClusterValue[int64], error)
22+
23+
ClientIdWithOptions(routeOptions options.RouteOption) (ClusterValue[int64], error)
2024
}

go/api/connection_management_cluster_commands_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,28 @@ func ExampleGlideClusterClient_EchoWithOptions() {
6363

6464
// Output: Hello World
6565
}
66+
67+
func ExampleGlideClusterClient_ClientId() {
68+
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
69+
result, err := client.ClientId()
70+
if err != nil {
71+
fmt.Println("Glide example failed with an error: ", err)
72+
}
73+
assert := result.IsSingleValue()
74+
fmt.Println(assert)
75+
76+
// Output: true
77+
}
78+
79+
func ExampleGlideClusterClient_ClientIdWithOptions() {
80+
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
81+
opts := options.RouteOption{Route: nil}
82+
result, err := client.ClientIdWithOptions(opts)
83+
if err != nil {
84+
fmt.Println("Glide example failed with an error: ", err)
85+
}
86+
assert := result.IsSingleValue()
87+
fmt.Println(assert)
88+
89+
// Output: true
90+
}

go/api/connection_management_commands.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ type ConnectionManagementCommands interface {
1515
PingWithOptions(pingOptions options.PingOptions) (string, error)
1616

1717
Echo(message string) (Result[string], error)
18+
19+
ClientId() (int64, error)
1820
}

go/api/connection_management_commands_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ func ExampleGlideClient_Echo() {
4141

4242
// Output: {Hello World false}
4343
}
44+
45+
func ExampleGlideClient_ClientId() {
46+
var client *GlideClient = getExampleGlideClient() // example helper function
47+
result, err := client.ClientId()
48+
if err != nil {
49+
fmt.Println("Glide example failed with an error: ", err)
50+
}
51+
assert := result > 0
52+
fmt.Println(assert)
53+
54+
// Output: true
55+
}

go/api/glide_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,18 @@ func (client *baseClient) LolwutWithOptions(opts options.LolwutOptions) (string,
352352
}
353353
return handleStringResponse(result)
354354
}
355+
356+
// Gets the current connection id.
357+
//
358+
// Return value:
359+
//
360+
// The id of the client.
361+
//
362+
// [valkey.io]: https://valkey.io/commands/client-id/
363+
func (client *GlideClient) ClientId() (int64, error) {
364+
result, err := client.executeCommand(C.ClientId, []string{})
365+
if err != nil {
366+
return defaultIntResponse, err
367+
}
368+
return handleIntResponse(result)
369+
}

go/api/glide_cluster_client.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,54 @@ func (client *GlideClusterClient) LolwutWithOptions(lolwutOptions options.Cluste
609609
}
610610
return createClusterSingleValue[string](data), nil
611611
}
612+
613+
// Gets the current connection id.
614+
//
615+
// Return value:
616+
//
617+
// The id of the client.
618+
//
619+
// [valkey.io]: https://valkey.io/commands/client-id/
620+
func (client *GlideClusterClient) ClientId() (ClusterValue[int64], error) {
621+
response, err := client.executeCommand(C.ClientId, []string{})
622+
if err != nil {
623+
return createEmptyClusterValue[int64](), err
624+
}
625+
data, err := handleIntResponse(response)
626+
if err != nil {
627+
return createEmptyClusterValue[int64](), err
628+
}
629+
return createClusterSingleValue[int64](data), nil
630+
}
631+
632+
// Gets the current connection id.
633+
//
634+
// Parameters:
635+
//
636+
// opts - Specifies the routing configuration for the command. The client will route the
637+
// command to the nodes defined by route.
638+
//
639+
// Return value:
640+
//
641+
// The id of the client.
642+
//
643+
// [valkey.io]: https://valkey.io/commands/client-id/
644+
func (client *GlideClusterClient) ClientIdWithOptions(opts options.RouteOption) (ClusterValue[int64], error) {
645+
response, err := client.executeCommandWithRoute(C.ClientId, []string{}, opts.Route)
646+
if err != nil {
647+
return createEmptyClusterValue[int64](), err
648+
}
649+
if opts.Route != nil &&
650+
(opts.Route).IsMultiNode() {
651+
data, err := handleStringIntMapResponse(response)
652+
if err != nil {
653+
return createEmptyClusterValue[int64](), err
654+
}
655+
return createClusterMultiValue[int64](data), nil
656+
}
657+
data, err := handleIntResponse(response)
658+
if err != nil {
659+
return createEmptyClusterValue[int64](), err
660+
}
661+
return createClusterSingleValue[int64](data), nil
662+
}

go/api/response_handlers.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,3 +1361,30 @@ func handleTimeClusterResponse(response *C.struct_CommandResponse) (ClusterValue
13611361
}
13621362
return createClusterSingleValue(data), nil
13631363
}
1364+
1365+
func handleStringIntMapResponse(response *C.struct_CommandResponse) (map[string]int64, error) {
1366+
defer C.free_command_response(response)
1367+
1368+
typeErr := checkResponseType(response, C.Map, false)
1369+
if typeErr != nil {
1370+
return nil, typeErr
1371+
}
1372+
1373+
data, err := parseMap(response)
1374+
if err != nil {
1375+
return nil, err
1376+
}
1377+
aMap := data.(map[string]interface{})
1378+
1379+
converted, err := mapConverter[int64]{
1380+
nil, false,
1381+
}.convert(aMap)
1382+
if err != nil {
1383+
return nil, err
1384+
}
1385+
result, ok := converted.(map[string]int64)
1386+
if !ok {
1387+
return nil, &errors.RequestError{Msg: fmt.Sprintf("unexpected type of map: %T", converted)}
1388+
}
1389+
return result, nil
1390+
}

go/integTest/cluster_commands_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,36 @@ func (suite *GlideTestSuite) TestLolwutWithOptions_WithRandomRoute() {
11631163
singleValue := result.SingleValue()
11641164
assert.Contains(suite.T(), singleValue, "Redis ver.")
11651165
}
1166+
1167+
func (suite *GlideTestSuite) TestClientIdCluster() {
1168+
client := suite.defaultClusterClient()
1169+
t := suite.T()
1170+
response, err := client.ClientId()
1171+
assert.NoError(t, err)
1172+
assert.True(t, response.IsSingleValue())
1173+
}
1174+
1175+
func (suite *GlideTestSuite) TestClientIdWithOptionsCluster() {
1176+
client := suite.defaultClusterClient()
1177+
t := suite.T()
1178+
1179+
// ClientId with option or with multiple options without route
1180+
opts := options.RouteOption{Route: nil}
1181+
response, err := client.ClientIdWithOptions(opts)
1182+
assert.NoError(t, err)
1183+
assert.True(t, response.IsSingleValue())
1184+
1185+
// same sections with random route
1186+
route := config.Route(config.RandomRoute)
1187+
opts = options.RouteOption{Route: route}
1188+
response, err = client.ClientIdWithOptions(opts)
1189+
assert.NoError(t, err)
1190+
assert.True(t, response.IsSingleValue())
1191+
1192+
// default sections, multi node route
1193+
route = config.Route(config.AllPrimaries)
1194+
opts = options.RouteOption{Route: route}
1195+
response, err = client.ClientIdWithOptions(opts)
1196+
assert.NoError(t, err)
1197+
assert.True(t, response.IsMultiValue())
1198+
}

go/integTest/standalone_commands_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,10 @@ func (suite *GlideTestSuite) TestLolwutWithOptions_EmptyArgs() {
813813
assert.NoError(suite.T(), err)
814814
assert.Contains(suite.T(), res, "Redis ver.")
815815
}
816+
817+
func (suite *GlideTestSuite) TestClientId() {
818+
client := suite.defaultClient()
819+
result, err := client.ClientId()
820+
assert.Nil(suite.T(), err)
821+
assert.Greater(suite.T(), result, int64(0))
822+
}

0 commit comments

Comments
 (0)