Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eb8d628

Browse files
committedAug 17, 2022
Merge branch 'master' into sz/master
2 parents 59798f9 + c561f3c commit eb8d628

File tree

16 files changed

+191
-64
lines changed

16 files changed

+191
-64
lines changed
 

‎.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
go-version: [1.17.x, 1.18.x]
16+
go-version: [1.18.x, 1.19.x]
1717

1818
services:
1919
redis:

‎Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ test: testdeps
66
go test ./... -run=NONE -bench=. -benchmem
77
env GOOS=linux GOARCH=386 go test ./...
88
go vet
9+
cd internal/customvet && go build .
10+
go vet -vettool ./internal/customvet/customvet
911

1012
testdeps: testdata/redis/src/redis-server
1113

‎command.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func cmdString(cmd Cmder, val interface{}) string {
104104
b = internal.AppendArg(b, val)
105105
}
106106

107-
return internal.String(b)
107+
return util.BytesToString(b)
108108
}
109109

110110
//------------------------------------------------------------------------------
@@ -1093,6 +1093,10 @@ func NewKeyValueSliceCmd(ctx context.Context, args ...interface{}) *KeyValueSlic
10931093
}
10941094
}
10951095

1096+
func (cmd *KeyValueSliceCmd) SetVal(val []KeyValue) {
1097+
cmd.val = val
1098+
}
1099+
10961100
func (cmd *KeyValueSliceCmd) Val() []KeyValue {
10971101
return cmd.val
10981102
}
@@ -1288,40 +1292,40 @@ func (cmd *MapStringStringCmd) readReply(rd *proto.Reader) error {
12881292

12891293
//------------------------------------------------------------------------------
12901294

1291-
type StringIntMapCmd struct {
1295+
type MapStringIntCmd struct {
12921296
baseCmd
12931297

12941298
val map[string]int64
12951299
}
12961300

1297-
var _ Cmder = (*StringIntMapCmd)(nil)
1301+
var _ Cmder = (*MapStringIntCmd)(nil)
12981302

1299-
func NewStringIntMapCmd(ctx context.Context, args ...interface{}) *StringIntMapCmd {
1300-
return &StringIntMapCmd{
1303+
func NewMapStringIntCmd(ctx context.Context, args ...interface{}) *MapStringIntCmd {
1304+
return &MapStringIntCmd{
13011305
baseCmd: baseCmd{
13021306
ctx: ctx,
13031307
args: args,
13041308
},
13051309
}
13061310
}
13071311

1308-
func (cmd *StringIntMapCmd) SetVal(val map[string]int64) {
1312+
func (cmd *MapStringIntCmd) SetVal(val map[string]int64) {
13091313
cmd.val = val
13101314
}
13111315

1312-
func (cmd *StringIntMapCmd) Val() map[string]int64 {
1316+
func (cmd *MapStringIntCmd) Val() map[string]int64 {
13131317
return cmd.val
13141318
}
13151319

1316-
func (cmd *StringIntMapCmd) Result() (map[string]int64, error) {
1320+
func (cmd *MapStringIntCmd) Result() (map[string]int64, error) {
13171321
return cmd.val, cmd.err
13181322
}
13191323

1320-
func (cmd *StringIntMapCmd) String() string {
1324+
func (cmd *MapStringIntCmd) String() string {
13211325
return cmdString(cmd, cmd.val)
13221326
}
13231327

1324-
func (cmd *StringIntMapCmd) readReply(rd *proto.Reader) error {
1328+
func (cmd *MapStringIntCmd) readReply(rd *proto.Reader) error {
13251329
n, err := rd.ReadMapLen()
13261330
if err != nil {
13271331
return err
@@ -3117,6 +3121,10 @@ func NewGeoSearchLocationCmd(
31173121
}
31183122
}
31193123

3124+
func (cmd *GeoSearchLocationCmd) SetVal(val []GeoLocation) {
3125+
cmd.val = val
3126+
}
3127+
31203128
func (cmd *GeoSearchLocationCmd) Val() []GeoLocation {
31213129
return cmd.val
31223130
}
@@ -3573,6 +3581,10 @@ func NewMapStringInterfaceCmd(ctx context.Context, args ...interface{}) *MapStri
35733581
}
35743582
}
35753583

3584+
func (cmd *MapStringInterfaceCmd) SetVal(val map[string]interface{}) {
3585+
cmd.val = val
3586+
}
3587+
35763588
func (cmd *MapStringInterfaceCmd) Val() map[string]interface{} {
35773589
return cmd.val
35783590
}
@@ -3633,6 +3645,10 @@ func NewMapStringStringSliceCmd(ctx context.Context, args ...interface{}) *MapSt
36333645
}
36343646
}
36353647

3648+
func (cmd *MapStringStringSliceCmd) SetVal(val []map[string]string) {
3649+
cmd.val = val
3650+
}
3651+
36363652
func (cmd *MapStringStringSliceCmd) Val() []map[string]string {
36373653
return cmd.val
36383654
}

‎commands.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ type Cmdable interface {
346346

347347
Publish(ctx context.Context, channel string, message interface{}) *IntCmd
348348
PubSubChannels(ctx context.Context, pattern string) *StringSliceCmd
349-
PubSubNumSub(ctx context.Context, channels ...string) *StringIntMapCmd
349+
PubSubNumSub(ctx context.Context, channels ...string) *MapStringIntCmd
350350
PubSubNumPat(ctx context.Context) *IntCmd
351351

352352
ClusterSlots(ctx context.Context) *ClusterSlotsCmd
@@ -3088,14 +3088,14 @@ func (c cmdable) PubSubChannels(ctx context.Context, pattern string) *StringSlic
30883088
return cmd
30893089
}
30903090

3091-
func (c cmdable) PubSubNumSub(ctx context.Context, channels ...string) *StringIntMapCmd {
3091+
func (c cmdable) PubSubNumSub(ctx context.Context, channels ...string) *MapStringIntCmd {
30923092
args := make([]interface{}, 2+len(channels))
30933093
args[0] = "pubsub"
30943094
args[1] = "numsub"
30953095
for i, channel := range channels {
30963096
args[2+i] = channel
30973097
}
3098-
cmd := NewStringIntMapCmd(ctx, args...)
3098+
cmd := NewMapStringIntCmd(ctx, args...)
30993099
_ = c(ctx, cmd)
31003100
return cmd
31013101
}

‎internal/arg.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import (
44
"fmt"
55
"strconv"
66
"time"
7+
8+
"github.com/go-redis/redis/v9/internal/util"
79
)
810

911
func AppendArg(b []byte, v interface{}) []byte {
1012
switch v := v.(type) {
1113
case nil:
1214
return append(b, "<nil>"...)
1315
case string:
14-
return appendUTF8String(b, Bytes(v))
16+
return appendUTF8String(b, util.StringToBytes(v))
1517
case []byte:
1618
return appendUTF8String(b, v)
1719
case int:

‎internal/customvet/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/customvet
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package setval
2+
3+
import (
4+
"go/ast"
5+
"go/token"
6+
"go/types"
7+
"golang.org/x/tools/go/analysis"
8+
)
9+
10+
var Analyzer = &analysis.Analyzer{
11+
Name: "setval",
12+
Doc: "find Cmder types that are missing a SetVal method",
13+
14+
Run: func(pass *analysis.Pass) (interface{}, error) {
15+
cmderTypes := make(map[string]token.Pos)
16+
typesWithSetValMethod := make(map[string]bool)
17+
18+
for _, file := range pass.Files {
19+
for _, decl := range file.Decls {
20+
funcName, receiverType := parseFuncDecl(decl, pass.TypesInfo)
21+
22+
switch funcName {
23+
case "Result":
24+
cmderTypes[receiverType] = decl.Pos()
25+
case "SetVal":
26+
typesWithSetValMethod[receiverType] = true
27+
}
28+
}
29+
}
30+
31+
for cmder, pos := range cmderTypes {
32+
if !typesWithSetValMethod[cmder] {
33+
pass.Reportf(pos, "%s is missing a SetVal method", cmder)
34+
}
35+
}
36+
37+
return nil, nil
38+
},
39+
}
40+
41+
func parseFuncDecl(decl ast.Decl, typesInfo *types.Info) (funcName, receiverType string) {
42+
funcDecl, ok := decl.(*ast.FuncDecl)
43+
if !ok {
44+
return "", "" // Not a function declaration.
45+
}
46+
47+
if funcDecl.Recv == nil {
48+
return "", "" // Not a method.
49+
}
50+
51+
if len(funcDecl.Recv.List) != 1 {
52+
return "", "" // Unexpected number of receiver arguments. (Can this happen?)
53+
}
54+
55+
receiverTypeObj := typesInfo.TypeOf(funcDecl.Recv.List[0].Type)
56+
if receiverTypeObj == nil {
57+
return "", "" // Unable to determine the receiver type.
58+
}
59+
60+
return funcDecl.Name.Name, receiverTypeObj.String()
61+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package setval_test
2+
3+
import (
4+
"github.com/go-redis/redis/internal/customvet/checks/setval"
5+
"golang.org/x/tools/go/analysis/analysistest"
6+
"testing"
7+
)
8+
9+
func Test(t *testing.T) {
10+
testdata := analysistest.TestData()
11+
analysistest.Run(t, testdata, setval.Analyzer, "a")
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package a
2+
3+
type GoodCmd struct {
4+
val int
5+
}
6+
7+
func (c *GoodCmd) SetVal(val int) {
8+
c.val = val
9+
}
10+
11+
func (c *GoodCmd) Result() (int, error) {
12+
return c.val, nil
13+
}
14+
15+
type BadCmd struct {
16+
val int
17+
}
18+
19+
func (c *BadCmd) Result() (int, error) { // want "\\*a.BadCmd is missing a SetVal method"
20+
return c.val, nil
21+
}
22+
23+
type NotACmd struct {
24+
val int
25+
}
26+
27+
func (c *NotACmd) Val() int {
28+
return c.val
29+
}

‎internal/customvet/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/go-redis/redis/internal/customvet
2+
3+
go 1.17
4+
5+
require golang.org/x/tools v0.1.12
6+
7+
require (
8+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
9+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
10+
)

‎internal/customvet/go.sum

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
4+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
5+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
6+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
7+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
8+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
9+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
10+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
11+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
12+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
13+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
16+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
18+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
19+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
20+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
21+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
22+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
23+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
24+
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
25+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
26+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

‎internal/customvet/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"github.com/go-redis/redis/internal/customvet/checks/setval"
5+
"golang.org/x/tools/go/analysis/multichecker"
6+
)
7+
8+
func main() {
9+
multichecker.Main(
10+
setval.Analyzer,
11+
)
12+
}

‎internal/safe.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎internal/unsafe.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎iterator.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,21 @@ package redis
22

33
import (
44
"context"
5-
"sync"
65
)
76

87
// ScanIterator is used to incrementally iterate over a collection of elements.
9-
// It's safe for concurrent use by multiple goroutines.
108
type ScanIterator struct {
11-
mu sync.Mutex // protects Scanner and pos
129
cmd *ScanCmd
1310
pos int
1411
}
1512

1613
// Err returns the last iterator error, if any.
1714
func (it *ScanIterator) Err() error {
18-
it.mu.Lock()
19-
err := it.cmd.Err()
20-
it.mu.Unlock()
21-
return err
15+
return it.cmd.Err()
2216
}
2317

2418
// Next advances the cursor and returns true if more values can be read.
2519
func (it *ScanIterator) Next(ctx context.Context) bool {
26-
it.mu.Lock()
27-
defer it.mu.Unlock()
28-
2920
// Instantly return on errors.
3021
if it.cmd.Err() != nil {
3122
return false
@@ -68,10 +59,8 @@ func (it *ScanIterator) Next(ctx context.Context) bool {
6859
// Val returns the key/field at the current cursor position.
6960
func (it *ScanIterator) Val() string {
7061
var v string
71-
it.mu.Lock()
7262
if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) {
7363
v = it.cmd.page[it.pos-1]
7464
}
75-
it.mu.Unlock()
7665
return v
7766
}

‎result.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ func NewBoolSliceResult(val []bool, err error) *BoolSliceCmd {
8383
}
8484

8585
// NewStringStringMapResult returns a StringStringMapCmd initialised with val and err for testing.
86-
func NewStringStringMapResult(val map[string]string, err error) *MapStringStringCmd {
86+
func NewMapStringStringResult(val map[string]string, err error) *MapStringStringCmd {
8787
var cmd MapStringStringCmd
8888
cmd.val = val
8989
cmd.SetErr(err)
9090
return &cmd
9191
}
9292

93-
// NewStringIntMapCmdResult returns a StringIntMapCmd initialised with val and err for testing.
94-
func NewStringIntMapCmdResult(val map[string]int64, err error) *StringIntMapCmd {
95-
var cmd StringIntMapCmd
93+
// NewMapStringIntCmdResult returns a MapStringIntCmd initialised with val and err for testing.
94+
func NewMapStringIntCmdResult(val map[string]int64, err error) *MapStringIntCmd {
95+
var cmd MapStringIntCmd
9696
cmd.val = val
9797
cmd.SetErr(err)
9898
return &cmd

0 commit comments

Comments
 (0)
Please sign in to comment.