Skip to content

Commit eb8d628

Browse files
committed
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+
)

0 commit comments

Comments
 (0)