Skip to content

Commit 76dc3b7

Browse files
committed
fix: Go 1.18 supports was broken because of slices and maps usage
These packages were only added to Go 1.21
1 parent 7a14376 commit 76dc3b7

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

doctests/query_em_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package example_commands_test
55
import (
66
"context"
77
"fmt"
8-
"slices"
8+
"sort"
99
"strings"
1010

1111
"github.com/redis/go-redis/v9"
@@ -279,8 +279,8 @@ func ExampleClient_query_em() {
279279
fmt.Println(res3.Total) // >>> 5
280280

281281
docs := res3.Docs
282-
slices.SortFunc(docs, func(a, b redis.Document) int {
283-
return strings.Compare(a.ID, b.ID)
282+
sort.Slice(docs, func(i, j int) bool {
283+
return strings.Compare(docs[i].ID, docs[j].ID) < 0
284284
})
285285

286286
for _, doc := range docs {

doctests/timeseries_tut_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ package example_commands_test
55
import (
66
"context"
77
"fmt"
8-
"maps"
98
"math"
10-
"slices"
119
"sort"
1210

1311
"github.com/redis/go-redis/v9"
@@ -417,7 +415,11 @@ func ExampleClient_timeseries_query_multi() {
417415
panic(err)
418416
}
419417

420-
res28Keys := slices.Collect(maps.Keys(res28))
418+
var res28Keys []string
419+
for k := range res28 {
420+
res28Keys = append(res28Keys, k)
421+
}
422+
421423
sort.Strings(res28Keys)
422424

423425
for _, k := range res28Keys {
@@ -457,7 +459,10 @@ func ExampleClient_timeseries_query_multi() {
457459
panic(err)
458460
}
459461

460-
res29Keys := slices.Collect(maps.Keys(res29))
462+
var res29Keys []string
463+
for k := range res29 {
464+
res29Keys = append(res29Keys, k)
465+
}
461466
sort.Strings(res29Keys)
462467

463468
for _, k := range res29Keys {
@@ -505,7 +510,10 @@ func ExampleClient_timeseries_query_multi() {
505510
panic(err)
506511
}
507512

508-
res30Keys := slices.Collect(maps.Keys(res30))
513+
var res30Keys []string
514+
for k := range res30 {
515+
res30Keys = append(res30Keys, k)
516+
}
509517
sort.Strings(res30Keys)
510518

511519
for _, k := range res30Keys {
@@ -550,7 +558,10 @@ func ExampleClient_timeseries_query_multi() {
550558
panic(err)
551559
}
552560

553-
res31Keys := slices.Collect(maps.Keys(res31))
561+
var res31Keys []string
562+
for k := range res31 {
563+
res31Keys = append(res31Keys, k)
564+
}
554565
sort.Strings(res31Keys)
555566

556567
for _, k := range res31Keys {
@@ -857,7 +868,10 @@ func ExampleClient_timeseries_aggmulti() {
857868
panic(err)
858869
}
859870

860-
res44Keys := slices.Collect(maps.Keys(res44))
871+
var res44Keys []string
872+
for k := range res44 {
873+
res44Keys = append(res44Keys, k)
874+
}
861875
sort.Strings(res44Keys)
862876

863877
for _, k := range res44Keys {
@@ -905,7 +919,10 @@ func ExampleClient_timeseries_aggmulti() {
905919
panic(err)
906920
}
907921

908-
res45Keys := slices.Collect(maps.Keys(res45))
922+
var res45Keys []string
923+
for k := range res45 {
924+
res45Keys = append(res45Keys, k)
925+
}
909926
sort.Strings(res45Keys)
910927

911928
for _, k := range res45Keys {

maintnotifications/hooks.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package maintnotifications
22

33
import (
44
"context"
5-
"slices"
65

76
"github.com/redis/go-redis/v9/internal"
87
"github.com/redis/go-redis/v9/internal/maintnotifications/logs"
@@ -15,6 +14,17 @@ type LoggingHook struct {
1514
LogLevel int // 0=Error, 1=Warn, 2=Info, 3=Debug
1615
}
1716

17+
// slicesContains is an helper function to check if a slice contains a specific string.
18+
// TODO: Replace with slices.Contains when we move to Go 1.21+
19+
func slicesContains(slice []string, item string) bool {
20+
for _, s := range slice {
21+
if s == item {
22+
return true
23+
}
24+
}
25+
return false
26+
}
27+
1828
// PreHook logs the notification before processing and allows modification.
1929
func (lh *LoggingHook) PreHook(ctx context.Context, notificationCtx push.NotificationHandlerContext, notificationType string, notification []interface{}) ([]interface{}, bool) {
2030
if lh.LogLevel >= 2 { // Info level
@@ -24,7 +34,7 @@ func (lh *LoggingHook) PreHook(ctx context.Context, notificationCtx push.Notific
2434
connID = conn.GetID()
2535
}
2636
seqID := int64(0)
27-
if slices.Contains(maintenanceNotificationTypes, notificationType) {
37+
if slicesContains(maintenanceNotificationTypes, notificationType) {
2838
// seqID is the second element in the notification array
2939
if len(notification) > 1 {
3040
if parsedSeqID, ok := notification[1].(int64); !ok {

osscluster_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"net"
9-
"slices"
109
"strconv"
1110
"strings"
1211
"sync"
@@ -107,6 +106,18 @@ func (s *clusterScenario) Close() error {
107106
return nil
108107
}
109108

109+
// slicesContains is an helper function to check if a slice contains a specific string.
110+
//
111+
// TODO: Replace with slices.Contains when we move to Go 1.21+
112+
func slicesContains(slice []string, item string) bool {
113+
for _, s := range slice {
114+
if s == item {
115+
return true
116+
}
117+
}
118+
return false
119+
}
120+
110121
func configureClusterTopology(ctx context.Context, scenario *clusterScenario) error {
111122
allowErrs := []string{
112123
"ERR Slot 0 is already busy",
@@ -131,8 +142,10 @@ func configureClusterTopology(ctx context.Context, scenario *clusterScenario) er
131142
slots := scenario.slots()
132143
for pos, master := range scenario.masters() {
133144
err := master.ClusterAddSlotsRange(ctx, slots[pos], slots[pos+1]-1).Err()
134-
if err != nil && slices.Contains(allowErrs, err.Error()) == false {
135-
return err
145+
if err != nil {
146+
if !slicesContains(allowErrs, err.Error()) {
147+
return err
148+
}
136149
}
137150
}
138151

0 commit comments

Comments
 (0)