Skip to content

Commit 44963fd

Browse files
authored
Go: Fix Ordering of Sorted Set With Scores Commands (valkey-io#3712)
1 parent 47c5939 commit 44963fd

File tree

6 files changed

+64
-37
lines changed

6 files changed

+64
-37
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@
7171

7272
* Go: Drop support for Go 1.20 ([#3513](https://github.com/valkey-io/valkey-glide/pull/3513))
7373
* Java: Deprecate `Transaction` and `ClusterTransaction` ([#3561](https://github.com/valkey-io/valkey-glide/pull/3561))
74+
* Go: Fix response handler for `ZRangeWithScores` to return an ordered result ([#3694](https://github.com/valkey-io/valkey-glide/pull/3694))
75+
* Go: Fix response handler for other sorted set with scores commands to return an ordered result ([#3712](https://github.com/valkey-io/valkey-glide/pull/3712))
7476

7577
#### Fixes
7678

7779
* Go, Java: Fix response handling for `customCommand` API for cluster client ([#3593](https://github.com/valkey-io/valkey-glide/pull/3593))
78-
* Go: Fix response handler for `ZRangeWithScores` to return an ordered result ([#3694](https://github.com/valkey-io/valkey-glide/pull/3694))
7980

8081
#### Operational Enhancements
8182

go/api/base_client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4248,7 +4248,7 @@ func (client *baseClient) ZRangeWithScores(
42484248
}
42494249
}
42504250

4251-
return handleZRangeWithScoresResponse(result, needsReverse)
4251+
return handleSortedSetWithScoresResponse(result, needsReverse)
42524252
}
42534253

42544254
// Stores a specified range of elements from the sorted set at `key`, into a new
@@ -6397,13 +6397,13 @@ func (client *baseClient) ZInter(keys options.KeyArray) ([]string, error) {
63976397
//
63986398
// Return value:
63996399
//
6400-
// A map of members to their scores.
6400+
// An array of members to their scores.
64016401
//
64026402
// [valkey.io]: https://valkey.io/commands/zinter/
64036403
func (client *baseClient) ZInterWithScores(
64046404
keysOrWeightedKeys options.KeysOrWeightedKeys,
64056405
zInterOptions options.ZInterOptions,
6406-
) (map[string]float64, error) {
6406+
) ([]MemberAndScore, error) {
64076407
args, err := keysOrWeightedKeys.ToArgs()
64086408
if err != nil {
64096409
return nil, err
@@ -6418,7 +6418,7 @@ func (client *baseClient) ZInterWithScores(
64186418
if err != nil {
64196419
return nil, err
64206420
}
6421-
return handleStringDoubleMapResponse(result)
6421+
return handleSortedSetWithScoresResponse(result, false)
64226422
}
64236423

64246424
// Computes the intersection of sorted sets given by the specified `keysOrWeightedKeys`
@@ -6537,19 +6537,19 @@ func (client *baseClient) ZDiff(keys []string) ([]string, error) {
65376537
//
65386538
// Return value:
65396539
//
6540-
// A `Map` of elements and their scores representing the difference between the sorted sets.
6540+
// An `Array` of elements and their scores representing the difference between the sorted sets.
65416541
// If the first `key` does not exist, it is treated as an empty sorted set, and the
6542-
// command returns an empty `Map`.
6542+
// command returns an empty `Array`.
65436543
//
65446544
// [valkey.io]: https://valkey.io/commands/zdiff/
6545-
func (client *baseClient) ZDiffWithScores(keys []string) (map[string]float64, error) {
6545+
func (client *baseClient) ZDiffWithScores(keys []string) ([]MemberAndScore, error) {
65466546
args := append([]string{}, strconv.Itoa(len(keys)))
65476547
args = append(args, keys...)
65486548
result, err := client.executeCommand(C.ZDiff, append(args, options.WithScoresKeyword))
65496549
if err != nil {
65506550
return nil, err
65516551
}
6552-
return handleStringDoubleMapResponse(result)
6552+
return handleSortedSetWithScoresResponse(result, false)
65536553
}
65546554

65556555
// Calculates the difference between the first sorted set and all the successive sorted sets at
@@ -6643,7 +6643,7 @@ func (client *baseClient) ZUnion(keys options.KeyArray) ([]string, error) {
66436643
func (client *baseClient) ZUnionWithScores(
66446644
keysOrWeightedKeys options.KeysOrWeightedKeys,
66456645
zUnionOptions *options.ZUnionOptions,
6646-
) (map[string]float64, error) {
6646+
) ([]MemberAndScore, error) {
66476647
args, err := keysOrWeightedKeys.ToArgs()
66486648
if err != nil {
66496649
return nil, err
@@ -6658,7 +6658,7 @@ func (client *baseClient) ZUnionWithScores(
66586658
if err != nil {
66596659
return nil, err
66606660
}
6661-
return handleStringDoubleMapResponse(result)
6661+
return handleSortedSetWithScoresResponse(result, false)
66626662
}
66636663

66646664
// Computes the union of sorted sets given by the specified `KeysOrWeightedKeys`, and

go/api/response_handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ func handleFunctionListMultiNodeResponse(response *C.struct_CommandResponse) (ma
16541654
return multiNodeLibs, nil
16551655
}
16561656

1657-
func handleZRangeWithScoresResponse(response *C.struct_CommandResponse, reverse bool) ([]MemberAndScore, error) {
1657+
func handleSortedSetWithScoresResponse(response *C.struct_CommandResponse, reverse bool) ([]MemberAndScore, error) {
16581658
defer C.free_command_response(response)
16591659

16601660
typeErr := checkResponseType(response, C.Map, false)

go/api/sorted_set_commands.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type SortedSetCommands interface {
8585

8686
ZDiff(keys []string) ([]string, error)
8787

88-
ZDiffWithScores(keys []string) (map[string]float64, error)
88+
ZDiffWithScores(keys []string) ([]MemberAndScore, error)
8989

9090
ZRandMember(key string) (Result[string], error)
9191

@@ -99,7 +99,7 @@ type SortedSetCommands interface {
9999

100100
ZInter(keys options.KeyArray) ([]string, error)
101101

102-
ZInterWithScores(keysOrWeightedKeys options.KeysOrWeightedKeys, options options.ZInterOptions) (map[string]float64, error)
102+
ZInterWithScores(keysOrWeightedKeys options.KeysOrWeightedKeys, options options.ZInterOptions) ([]MemberAndScore, error)
103103

104104
ZInterStore(destination string, keysOrWeightedKeys options.KeysOrWeightedKeys) (int64, error)
105105

@@ -111,7 +111,7 @@ type SortedSetCommands interface {
111111

112112
ZUnion(keys options.KeyArray) ([]string, error)
113113

114-
ZUnionWithScores(keysOrWeightedKeys options.KeysOrWeightedKeys, options *options.ZUnionOptions) (map[string]float64, error)
114+
ZUnionWithScores(keysOrWeightedKeys options.KeysOrWeightedKeys, options *options.ZUnionOptions) ([]MemberAndScore, error)
115115

116116
ZUnionStore(destination string, keysOrWeightedKeys options.KeysOrWeightedKeys) (int64, error)
117117

go/api/sorted_set_commands_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ func ExampleGlideClient_ZInterWithScores() {
11451145
fmt.Println("Glide example failed with an error: ", err)
11461146
}
11471147
fmt.Println(result)
1148-
// Output: map[b:3.5 c:5.5 d:7]
1148+
// Output: [{b 3.5} {c 5.5} {d 7}]
11491149
}
11501150

11511151
func ExampleGlideClusterClient_ZInterWithScores() {
@@ -1164,7 +1164,7 @@ func ExampleGlideClusterClient_ZInterWithScores() {
11641164
}
11651165
fmt.Println(result)
11661166

1167-
// Output: map[b:3.5 c:5.5 d:7]
1167+
// Output: [{b 3.5} {c 5.5} {d 7}]
11681168
}
11691169

11701170
func ExampleGlideClient_ZInterStore() {
@@ -1276,7 +1276,7 @@ func ExampleGlideClient_ZDiffWithScores() {
12761276
fmt.Println("Glide example failed with an error: ", err)
12771277
}
12781278
fmt.Println(result)
1279-
// Output: map[a:1]
1279+
// Output: [{a 1}]
12801280
}
12811281

12821282
func ExampleGlideClusterClient_ZDiffWithScores() {
@@ -1290,7 +1290,7 @@ func ExampleGlideClusterClient_ZDiffWithScores() {
12901290
}
12911291
fmt.Println(result)
12921292

1293-
// Output: map[a:1]
1293+
// Output: [{a 1}]
12941294
}
12951295

12961296
func ExampleGlideClient_ZDiffStore() {
@@ -1386,8 +1386,7 @@ func ExampleGlideClient_ZUnionWithScores() {
13861386
)
13871387
fmt.Println(zUnionResult)
13881388

1389-
// Output:
1390-
// map[one:1 three:3 two:5.5]
1389+
// Output: [{one 1} {three 3} {two 5.5}]
13911390
}
13921391

13931392
func ExampleGlideClusterClient_ZUnionWithScores() {
@@ -1411,8 +1410,7 @@ func ExampleGlideClusterClient_ZUnionWithScores() {
14111410
)
14121411
fmt.Println(zUnionResult)
14131412

1414-
// Output:
1415-
// map[one:1 three:3 two:5.5]
1413+
// Output: [{one 1} {three 3} {two 5.5}]
14161414
}
14171415

14181416
func ExampleGlideClient_ZUnionStore() {

go/integTest/shared_commands_test.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8787,31 +8787,31 @@ func (suite *GlideTestSuite) TestZInter() {
87878787
*options.NewZInterOptions().SetAggregate(options.AggregateSum),
87888788
)
87898789
assert.NoError(suite.T(), err)
8790-
assert.Equal(suite.T(), map[string]float64{"two": 5.5}, zinterWithScoresResult)
8790+
assert.Equal(suite.T(), []api.MemberAndScore{{Member: "two", Score: 5.5}}, zinterWithScoresResult)
87918791

87928792
// intersect results with max aggregate
87938793
zinterWithMaxAggregateResult, err := client.ZInterWithScores(
87948794
options.KeyArray{Keys: []string{key1, key2}},
87958795
*options.NewZInterOptions().SetAggregate(options.AggregateMax),
87968796
)
87978797
assert.NoError(suite.T(), err)
8798-
assert.Equal(suite.T(), map[string]float64{"two": 3.5}, zinterWithMaxAggregateResult)
8798+
assert.Equal(suite.T(), []api.MemberAndScore{{Member: "two", Score: 3.5}}, zinterWithMaxAggregateResult)
87998799

88008800
// intersect results with min aggregate
88018801
zinterWithMinAggregateResult, err := client.ZInterWithScores(
88028802
options.KeyArray{Keys: []string{key1, key2}},
88038803
*options.NewZInterOptions().SetAggregate(options.AggregateMin),
88048804
)
88058805
assert.NoError(suite.T(), err)
8806-
assert.Equal(suite.T(), map[string]float64{"two": 2.0}, zinterWithMinAggregateResult)
8806+
assert.Equal(suite.T(), []api.MemberAndScore{{Member: "two", Score: 2.0}}, zinterWithMinAggregateResult)
88078807

88088808
// intersect results with sum aggregate
88098809
zinterWithSumAggregateResult, err := client.ZInterWithScores(
88108810
options.KeyArray{Keys: []string{key1, key2}},
88118811
*options.NewZInterOptions().SetAggregate(options.AggregateSum),
88128812
)
88138813
assert.NoError(suite.T(), err)
8814-
assert.Equal(suite.T(), map[string]float64{"two": 5.5}, zinterWithSumAggregateResult)
8814+
assert.Equal(suite.T(), []api.MemberAndScore{{Member: "two", Score: 5.5}}, zinterWithSumAggregateResult)
88158815

88168816
// Scores are multiplied by a 2.0 weight for key1 and key2 during aggregation
88178817
zinterWithWeightedKeysResult, err := client.ZInterWithScores(
@@ -8824,7 +8824,7 @@ func (suite *GlideTestSuite) TestZInter() {
88248824
*options.NewZInterOptions().SetAggregate(options.AggregateSum),
88258825
)
88268826
assert.NoError(suite.T(), err)
8827-
assert.Equal(suite.T(), map[string]float64{"two": 11.0}, zinterWithWeightedKeysResult)
8827+
assert.Equal(suite.T(), []api.MemberAndScore{{Member: "two", Score: 11.0}}, zinterWithWeightedKeysResult)
88288828

88298829
// non-existent key - empty intersection
88308830
zinterWithNonExistentKeyResult, err := client.ZInterWithScores(
@@ -9021,13 +9021,17 @@ func (suite *GlideTestSuite) TestZDiff() {
90219021

90229022
zDiffResultWithScores, err := client.ZDiffWithScores([]string{key1, key2})
90239023
assert.NoError(t, err)
9024-
assert.Equal(t, map[string]float64{"one": 1.0, "three": 3.0}, zDiffResultWithScores)
9024+
assert.Equal(
9025+
t,
9026+
[]api.MemberAndScore{{Member: "one", Score: 1.0}, {Member: "three", Score: 3.0}},
9027+
zDiffResultWithScores,
9028+
)
90259029
zDiffResultWithScores, err = client.ZDiffWithScores([]string{key1, key3})
90269030
assert.NoError(t, err)
9027-
assert.Equal(t, map[string]float64{}, zDiffResultWithScores)
9031+
assert.Equal(t, []api.MemberAndScore{}, zDiffResultWithScores)
90289032
zDiffResultWithScores, err = client.ZDiffWithScores([]string{nonExistentKey, key3})
90299033
assert.NoError(t, err)
9030-
assert.Equal(t, map[string]float64{}, zDiffResultWithScores)
9034+
assert.Equal(t, []api.MemberAndScore{}, zDiffResultWithScores)
90319035

90329036
// Key exists, but it is not a set
90339037
setResult, _ := client.Set(nonExistentKey, "bar")
@@ -9156,31 +9160,47 @@ func (suite *GlideTestSuite) TestZUnionAndZUnionWithScores() {
91569160
options.NewZUnionOptionsBuilder().SetAggregate(options.AggregateSum),
91579161
)
91589162
assert.NoError(suite.T(), err)
9159-
assert.Equal(suite.T(), map[string]float64{"one": 1.0, "two": 5.5, "three": 3.0}, zUnionWithScoresResult)
9163+
assert.Equal(
9164+
suite.T(),
9165+
[]api.MemberAndScore{{Member: "one", Score: 1.0}, {Member: "three", Score: 3.0}, {Member: "two", Score: 5.5}},
9166+
zUnionWithScoresResult,
9167+
)
91609168

91619169
// Union results with max aggregate
91629170
zUnionWithMaxAggregateResult, err := client.ZUnionWithScores(
91639171
options.KeyArray{Keys: []string{key1, key2}},
91649172
options.NewZUnionOptionsBuilder().SetAggregate(options.AggregateMax),
91659173
)
91669174
assert.NoError(suite.T(), err)
9167-
assert.Equal(suite.T(), map[string]float64{"one": 1.0, "two": 3.5, "three": 3.0}, zUnionWithMaxAggregateResult)
9175+
assert.Equal(
9176+
suite.T(),
9177+
[]api.MemberAndScore{{Member: "one", Score: 1.0}, {Member: "three", Score: 3.0}, {Member: "two", Score: 3.5}},
9178+
zUnionWithMaxAggregateResult,
9179+
)
91689180

91699181
// Union results with min aggregate
91709182
zUnionWithMinAggregateResult, err := client.ZUnionWithScores(
91719183
options.KeyArray{Keys: []string{key1, key2}},
91729184
options.NewZUnionOptionsBuilder().SetAggregate(options.AggregateMin),
91739185
)
91749186
assert.NoError(suite.T(), err)
9175-
assert.Equal(suite.T(), map[string]float64{"one": 1.0, "two": 2.0, "three": 3.0}, zUnionWithMinAggregateResult)
9187+
assert.Equal(
9188+
suite.T(),
9189+
[]api.MemberAndScore{{Member: "one", Score: 1.0}, {Member: "two", Score: 2.0}, {Member: "three", Score: 3.0}},
9190+
zUnionWithMinAggregateResult,
9191+
)
91769192

91779193
// Union results with sum aggregate
91789194
zUnionWithSumAggregateResult, err := client.ZUnionWithScores(
91799195
options.KeyArray{Keys: []string{key1, key2}},
91809196
options.NewZUnionOptionsBuilder().SetAggregate(options.AggregateSum),
91819197
)
91829198
assert.NoError(suite.T(), err)
9183-
assert.Equal(suite.T(), map[string]float64{"one": 1.0, "two": 5.5, "three": 3.0}, zUnionWithSumAggregateResult)
9199+
assert.Equal(
9200+
suite.T(),
9201+
[]api.MemberAndScore{{Member: "one", Score: 1.0}, {Member: "three", Score: 3.0}, {Member: "two", Score: 5.5}},
9202+
zUnionWithSumAggregateResult,
9203+
)
91849204

91859205
// Scores are multiplied by a 2.0 weight for key1 and key2 during aggregation
91869206
zUnionWithWeightedKeysResult, err := client.ZUnionWithScores(
@@ -9193,15 +9213,23 @@ func (suite *GlideTestSuite) TestZUnionAndZUnionWithScores() {
91939213
options.NewZUnionOptionsBuilder().SetAggregate(options.AggregateSum),
91949214
)
91959215
assert.NoError(suite.T(), err)
9196-
assert.Equal(suite.T(), map[string]float64{"one": 3.0, "two": 13.0, "three": 6.0}, zUnionWithWeightedKeysResult)
9216+
assert.Equal(
9217+
suite.T(),
9218+
[]api.MemberAndScore{{Member: "one", Score: 3.0}, {Member: "three", Score: 6.0}, {Member: "two", Score: 13.0}},
9219+
zUnionWithWeightedKeysResult,
9220+
)
91979221

91989222
// non-existent key - empty union
91999223
zUnionWithNonExistentKeyResult, err := client.ZUnionWithScores(
92009224
options.KeyArray{Keys: []string{key1, key3}},
92019225
options.NewZUnionOptionsBuilder().SetAggregate(options.AggregateSum),
92029226
)
92039227
assert.NoError(suite.T(), err)
9204-
assert.Equal(suite.T(), map[string]float64{"one": 1.0, "two": 2.0}, zUnionWithNonExistentKeyResult)
9228+
assert.Equal(
9229+
suite.T(),
9230+
[]api.MemberAndScore{{Member: "one", Score: 1.0}, {Member: "two", Score: 2.0}},
9231+
zUnionWithNonExistentKeyResult,
9232+
)
92059233

92069234
// empty key list - empty union
92079235
zUnionWithEmptyKeyArray, err := client.ZUnionWithScores(options.KeyArray{Keys: []string{}},

0 commit comments

Comments
 (0)