Skip to content

Commit d69301c

Browse files
Go: Add GeoHash (#3439)
* Go: Add Geo hash command Signed-off-by: Prateek Kumar <prateek.kumar@improving.com>
1 parent 68ee56a commit d69301c

5 files changed

Lines changed: 120 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Go: Add `FLUSHALL` ([#3117](https://github.com/valkey-io/valkey-glide/pull/3117))
2121
* Go: Add `FLUSHDB` ([#3117](https://github.com/valkey-io/valkey-glide/pull/3117))
2222
* Go: Add password update api ([#3346](https://github.com/valkey-io/valkey-glide/pull/3346))
23+
* Go: Add `GeoHash` ([#3439](https://github.com/valkey-io/valkey-glide/pull/3439))
2324
* Go/Core: Move FFI to a dedicated folder for reusability ([#3372](https://github.com/valkey-io/valkey-glide/pull/3372))
2425

2526
#### Breaking Changes

go/api/base_client.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6617,3 +6617,31 @@ func (client *baseClient) GeoAddWithOptions(
66176617
}
66186618
return handleIntResponse(result)
66196619
}
6620+
6621+
// Returns the GeoHash strings representing the positions of all the specified
6622+
// `members` in the sorted set stored at the `key`.
6623+
//
6624+
// See [valkey.io] for details.
6625+
//
6626+
// Parameters:
6627+
//
6628+
// key - The key of the sorted set.
6629+
// members - The array of members whose GeoHash strings are to be retrieved.
6630+
//
6631+
// Returns value:
6632+
//
6633+
// An array of GeoHash strings representing the positions of the specified members stored
6634+
// at key. If a member does not exist in the sorted set, a `null` value is returned
6635+
// for that member.
6636+
//
6637+
// [valkey.io]: https://valkey.io/commands/geohash/
6638+
func (client *baseClient) GeoHash(key string, members []string) ([]string, error) {
6639+
result, err := client.executeCommand(
6640+
C.GeoHash,
6641+
append([]string{key}, members...),
6642+
)
6643+
if err != nil {
6644+
return nil, err
6645+
}
6646+
return handleStringArrayResponse(result)
6647+
}

go/api/geospacial_commands.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ type GeoSpatialCommands interface {
2020
membersToGeospatialData map[string]options.GeospatialData,
2121
options options.GeoAddOptions,
2222
) (int64, error)
23+
24+
GeoHash(key string, members []string) ([]string, error)
2325
}

go/api/geospacial_commands_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,53 @@ func ExampleGlideClusterClient_GeoAdd() {
4646
// Output:
4747
// 2
4848
}
49+
50+
func ExampleGlideClient_GeoHash() {
51+
client := getExampleGlideClient()
52+
key := uuid.New().String()
53+
membersToCoordinates := map[string]options.GeospatialData{
54+
"Palermo": {Longitude: 13.361389, Latitude: 38.115556},
55+
"Catania": {Longitude: 15.087269, Latitude: 37.502669},
56+
}
57+
58+
// Add the coordinates
59+
_, err := client.GeoAdd(key, membersToCoordinates)
60+
if err != nil {
61+
fmt.Println("Glide example failed with an error: ", err)
62+
}
63+
64+
// Test getting geohash for multiple members
65+
geoHashResults, err := client.GeoHash(key, []string{"Palermo", "Catania"})
66+
if err != nil {
67+
fmt.Println("Glide example failed with an error: ", err)
68+
}
69+
fmt.Println(geoHashResults)
70+
71+
// Output:
72+
// [sqc8b49rny0 sqdtr74hyu0]
73+
}
74+
75+
func ExampleGlideClusterClient_GeoHash() {
76+
client := getExampleGlideClusterClient()
77+
key := uuid.New().String()
78+
membersToCoordinates := map[string]options.GeospatialData{
79+
"Palermo": {Longitude: 13.361389, Latitude: 38.115556},
80+
"Catania": {Longitude: 15.087269, Latitude: 37.502669},
81+
}
82+
83+
// Add the coordinates
84+
_, err := client.GeoAdd(key, membersToCoordinates)
85+
if err != nil {
86+
fmt.Println("Glide example failed with an error: ", err)
87+
}
88+
89+
// Test getting geohash for multiple members
90+
geoHashResults, err := client.GeoHash(key, []string{"Palermo", "Catania"})
91+
if err != nil {
92+
fmt.Println("Glide example failed with an error: ", err)
93+
}
94+
fmt.Println(geoHashResults)
95+
96+
// Output:
97+
// [sqc8b49rny0 sqdtr74hyu0]
98+
}

go/integTest/shared_commands_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9169,6 +9169,45 @@ func (suite *GlideTestSuite) TestGeoAdd_InvalidArgs() {
91699169
})
91709170
}
91719171

9172+
func (suite *GlideTestSuite) TestGeoHash() {
9173+
suite.runWithDefaultClients(func(client api.BaseClient) {
9174+
key1 := uuid.New().String()
9175+
t := suite.T()
9176+
9177+
// Add some locations to the geo index
9178+
membersToCoordinates := map[string]options.GeospatialData{
9179+
"Palermo": {Longitude: 13.361389, Latitude: 38.115556},
9180+
"Catania": {Longitude: 15.087269, Latitude: 37.502669},
9181+
}
9182+
9183+
// Add the coordinates
9184+
result, err := client.GeoAdd(key1, membersToCoordinates)
9185+
assert.NoError(t, err)
9186+
assert.Equal(t, int64(2), result)
9187+
9188+
// Test getting geohash for multiple members
9189+
geoHashResults, err := client.GeoHash(key1, []string{"Palermo", "Catania"})
9190+
fmt.Println(geoHashResults)
9191+
assert.NoError(t, err)
9192+
assert.Equal(t, 2, len(geoHashResults))
9193+
assert.Equal(t, geoHashResults[0], "sqc8b49rny0")
9194+
assert.Equal(t, geoHashResults[1], "sqdtr74hyu0")
9195+
9196+
// Test getting geohash for empty members
9197+
geoHashResults, err = client.GeoHash(key1, []string{})
9198+
assert.NoError(t, err)
9199+
assert.Equal(t, 0, len(geoHashResults))
9200+
9201+
// Test with wrong key type
9202+
wrongKey := "{testKey}:3-" + uuid.New().String()
9203+
_, err = client.Set(wrongKey, "value")
9204+
assert.NoError(t, err)
9205+
_, err = client.GeoHash(wrongKey, []string{"Palermo"})
9206+
assert.Error(t, err)
9207+
assert.IsType(t, &errors.RequestError{}, err)
9208+
})
9209+
}
9210+
91729211
func (suite *GlideTestSuite) TestGetSet_SendLargeValues() {
91739212
suite.runWithDefaultClients(func(client api.BaseClient) {
91749213
key := suite.GenerateLargeUuid()

0 commit comments

Comments
 (0)