diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e0ae9434..ec300a92 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - go-version: [1.17.x, 1.18.x] + go-version: [1.18.x, 1.19.x] services: redis: diff --git a/Makefile b/Makefile index 5dbd7ae8..6399210c 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,8 @@ test: testdeps go test ./... -run=NONE -bench=. -benchmem env GOOS=linux GOARCH=386 go test ./... go vet + cd internal/customvet && go build . + go vet -vettool ./internal/customvet/customvet testdeps: testdata/redis/src/redis-server diff --git a/command.go b/command.go index 319bfba2..d4fc91de 100644 --- a/command.go +++ b/command.go @@ -104,7 +104,7 @@ func cmdString(cmd Cmder, val interface{}) string { b = internal.AppendArg(b, val) } - return internal.String(b) + return util.BytesToString(b) } //------------------------------------------------------------------------------ @@ -1093,6 +1093,10 @@ func NewKeyValueSliceCmd(ctx context.Context, args ...interface{}) *KeyValueSlic } } +func (cmd *KeyValueSliceCmd) SetVal(val []KeyValue) { + cmd.val = val +} + func (cmd *KeyValueSliceCmd) Val() []KeyValue { return cmd.val } @@ -1288,16 +1292,16 @@ func (cmd *MapStringStringCmd) readReply(rd *proto.Reader) error { //------------------------------------------------------------------------------ -type StringIntMapCmd struct { +type MapStringIntCmd struct { baseCmd val map[string]int64 } -var _ Cmder = (*StringIntMapCmd)(nil) +var _ Cmder = (*MapStringIntCmd)(nil) -func NewStringIntMapCmd(ctx context.Context, args ...interface{}) *StringIntMapCmd { - return &StringIntMapCmd{ +func NewMapStringIntCmd(ctx context.Context, args ...interface{}) *MapStringIntCmd { + return &MapStringIntCmd{ baseCmd: baseCmd{ ctx: ctx, args: args, @@ -1305,23 +1309,23 @@ func NewStringIntMapCmd(ctx context.Context, args ...interface{}) *StringIntMapC } } -func (cmd *StringIntMapCmd) SetVal(val map[string]int64) { +func (cmd *MapStringIntCmd) SetVal(val map[string]int64) { cmd.val = val } -func (cmd *StringIntMapCmd) Val() map[string]int64 { +func (cmd *MapStringIntCmd) Val() map[string]int64 { return cmd.val } -func (cmd *StringIntMapCmd) Result() (map[string]int64, error) { +func (cmd *MapStringIntCmd) Result() (map[string]int64, error) { return cmd.val, cmd.err } -func (cmd *StringIntMapCmd) String() string { +func (cmd *MapStringIntCmd) String() string { return cmdString(cmd, cmd.val) } -func (cmd *StringIntMapCmd) readReply(rd *proto.Reader) error { +func (cmd *MapStringIntCmd) readReply(rd *proto.Reader) error { n, err := rd.ReadMapLen() if err != nil { return err @@ -3117,6 +3121,10 @@ func NewGeoSearchLocationCmd( } } +func (cmd *GeoSearchLocationCmd) SetVal(val []GeoLocation) { + cmd.val = val +} + func (cmd *GeoSearchLocationCmd) Val() []GeoLocation { return cmd.val } @@ -3573,6 +3581,10 @@ func NewMapStringInterfaceCmd(ctx context.Context, args ...interface{}) *MapStri } } +func (cmd *MapStringInterfaceCmd) SetVal(val map[string]interface{}) { + cmd.val = val +} + func (cmd *MapStringInterfaceCmd) Val() map[string]interface{} { return cmd.val } @@ -3633,6 +3645,10 @@ func NewMapStringStringSliceCmd(ctx context.Context, args ...interface{}) *MapSt } } +func (cmd *MapStringStringSliceCmd) SetVal(val []map[string]string) { + cmd.val = val +} + func (cmd *MapStringStringSliceCmd) Val() []map[string]string { return cmd.val } diff --git a/commands.go b/commands.go index beb3af2d..57de25b0 100644 --- a/commands.go +++ b/commands.go @@ -346,7 +346,7 @@ type Cmdable interface { Publish(ctx context.Context, channel string, message interface{}) *IntCmd PubSubChannels(ctx context.Context, pattern string) *StringSliceCmd - PubSubNumSub(ctx context.Context, channels ...string) *StringIntMapCmd + PubSubNumSub(ctx context.Context, channels ...string) *MapStringIntCmd PubSubNumPat(ctx context.Context) *IntCmd ClusterSlots(ctx context.Context) *ClusterSlotsCmd @@ -3088,14 +3088,14 @@ func (c cmdable) PubSubChannels(ctx context.Context, pattern string) *StringSlic return cmd } -func (c cmdable) PubSubNumSub(ctx context.Context, channels ...string) *StringIntMapCmd { +func (c cmdable) PubSubNumSub(ctx context.Context, channels ...string) *MapStringIntCmd { args := make([]interface{}, 2+len(channels)) args[0] = "pubsub" args[1] = "numsub" for i, channel := range channels { args[2+i] = channel } - cmd := NewStringIntMapCmd(ctx, args...) + cmd := NewMapStringIntCmd(ctx, args...) _ = c(ctx, cmd) return cmd } diff --git a/internal/arg.go b/internal/arg.go index b97fa0d6..af5f8c93 100644 --- a/internal/arg.go +++ b/internal/arg.go @@ -4,6 +4,8 @@ import ( "fmt" "strconv" "time" + + "github.com/go-redis/redis/v9/internal/util" ) func AppendArg(b []byte, v interface{}) []byte { @@ -11,7 +13,7 @@ func AppendArg(b []byte, v interface{}) []byte { case nil: return append(b, ""...) case string: - return appendUTF8String(b, Bytes(v)) + return appendUTF8String(b, util.StringToBytes(v)) case []byte: return appendUTF8String(b, v) case int: diff --git a/internal/customvet/.gitignore b/internal/customvet/.gitignore new file mode 100644 index 00000000..6341ecb8 --- /dev/null +++ b/internal/customvet/.gitignore @@ -0,0 +1 @@ +/customvet diff --git a/internal/customvet/checks/setval/setval.go b/internal/customvet/checks/setval/setval.go new file mode 100644 index 00000000..e629f5f7 --- /dev/null +++ b/internal/customvet/checks/setval/setval.go @@ -0,0 +1,61 @@ +package setval + +import ( + "go/ast" + "go/token" + "go/types" + "golang.org/x/tools/go/analysis" +) + +var Analyzer = &analysis.Analyzer{ + Name: "setval", + Doc: "find Cmder types that are missing a SetVal method", + + Run: func(pass *analysis.Pass) (interface{}, error) { + cmderTypes := make(map[string]token.Pos) + typesWithSetValMethod := make(map[string]bool) + + for _, file := range pass.Files { + for _, decl := range file.Decls { + funcName, receiverType := parseFuncDecl(decl, pass.TypesInfo) + + switch funcName { + case "Result": + cmderTypes[receiverType] = decl.Pos() + case "SetVal": + typesWithSetValMethod[receiverType] = true + } + } + } + + for cmder, pos := range cmderTypes { + if !typesWithSetValMethod[cmder] { + pass.Reportf(pos, "%s is missing a SetVal method", cmder) + } + } + + return nil, nil + }, +} + +func parseFuncDecl(decl ast.Decl, typesInfo *types.Info) (funcName, receiverType string) { + funcDecl, ok := decl.(*ast.FuncDecl) + if !ok { + return "", "" // Not a function declaration. + } + + if funcDecl.Recv == nil { + return "", "" // Not a method. + } + + if len(funcDecl.Recv.List) != 1 { + return "", "" // Unexpected number of receiver arguments. (Can this happen?) + } + + receiverTypeObj := typesInfo.TypeOf(funcDecl.Recv.List[0].Type) + if receiverTypeObj == nil { + return "", "" // Unable to determine the receiver type. + } + + return funcDecl.Name.Name, receiverTypeObj.String() +} diff --git a/internal/customvet/checks/setval/setval_test.go b/internal/customvet/checks/setval/setval_test.go new file mode 100644 index 00000000..44377439 --- /dev/null +++ b/internal/customvet/checks/setval/setval_test.go @@ -0,0 +1,12 @@ +package setval_test + +import ( + "github.com/go-redis/redis/internal/customvet/checks/setval" + "golang.org/x/tools/go/analysis/analysistest" + "testing" +) + +func Test(t *testing.T) { + testdata := analysistest.TestData() + analysistest.Run(t, testdata, setval.Analyzer, "a") +} diff --git a/internal/customvet/checks/setval/testdata/src/a/a.go b/internal/customvet/checks/setval/testdata/src/a/a.go new file mode 100644 index 00000000..035cb5d3 --- /dev/null +++ b/internal/customvet/checks/setval/testdata/src/a/a.go @@ -0,0 +1,29 @@ +package a + +type GoodCmd struct { + val int +} + +func (c *GoodCmd) SetVal(val int) { + c.val = val +} + +func (c *GoodCmd) Result() (int, error) { + return c.val, nil +} + +type BadCmd struct { + val int +} + +func (c *BadCmd) Result() (int, error) { // want "\\*a.BadCmd is missing a SetVal method" + return c.val, nil +} + +type NotACmd struct { + val int +} + +func (c *NotACmd) Val() int { + return c.val +} diff --git a/internal/customvet/go.mod b/internal/customvet/go.mod new file mode 100644 index 00000000..d3bce547 --- /dev/null +++ b/internal/customvet/go.mod @@ -0,0 +1,10 @@ +module github.com/go-redis/redis/internal/customvet + +go 1.17 + +require golang.org/x/tools v0.1.12 + +require ( + golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect +) diff --git a/internal/customvet/go.sum b/internal/customvet/go.sum new file mode 100644 index 00000000..3ddae732 --- /dev/null +++ b/internal/customvet/go.sum @@ -0,0 +1,26 @@ +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/customvet/main.go b/internal/customvet/main.go new file mode 100644 index 00000000..a6146dcf --- /dev/null +++ b/internal/customvet/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/go-redis/redis/internal/customvet/checks/setval" + "golang.org/x/tools/go/analysis/multichecker" +) + +func main() { + multichecker.Main( + setval.Analyzer, + ) +} diff --git a/internal/safe.go b/internal/safe.go deleted file mode 100644 index fd2f4340..00000000 --- a/internal/safe.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build appengine -// +build appengine - -package internal - -func String(b []byte) string { - return string(b) -} - -func Bytes(s string) []byte { - return []byte(s) -} diff --git a/internal/unsafe.go b/internal/unsafe.go deleted file mode 100644 index 9f2e418f..00000000 --- a/internal/unsafe.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build !appengine -// +build !appengine - -package internal - -import "unsafe" - -// String converts byte slice to string. -func String(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} - -// Bytes converts string to byte slice. -func Bytes(s string) []byte { - return *(*[]byte)(unsafe.Pointer( - &struct { - string - Cap int - }{s, len(s)}, - )) -} diff --git a/iterator.go b/iterator.go index 2f8bc2be..cd1a8285 100644 --- a/iterator.go +++ b/iterator.go @@ -2,30 +2,21 @@ package redis import ( "context" - "sync" ) // ScanIterator is used to incrementally iterate over a collection of elements. -// It's safe for concurrent use by multiple goroutines. type ScanIterator struct { - mu sync.Mutex // protects Scanner and pos cmd *ScanCmd pos int } // Err returns the last iterator error, if any. func (it *ScanIterator) Err() error { - it.mu.Lock() - err := it.cmd.Err() - it.mu.Unlock() - return err + return it.cmd.Err() } // Next advances the cursor and returns true if more values can be read. func (it *ScanIterator) Next(ctx context.Context) bool { - it.mu.Lock() - defer it.mu.Unlock() - // Instantly return on errors. if it.cmd.Err() != nil { return false @@ -68,10 +59,8 @@ func (it *ScanIterator) Next(ctx context.Context) bool { // Val returns the key/field at the current cursor position. func (it *ScanIterator) Val() string { var v string - it.mu.Lock() if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) { v = it.cmd.page[it.pos-1] } - it.mu.Unlock() return v } diff --git a/result.go b/result.go index 9401dfee..44741b01 100644 --- a/result.go +++ b/result.go @@ -83,16 +83,16 @@ func NewBoolSliceResult(val []bool, err error) *BoolSliceCmd { } // NewStringStringMapResult returns a StringStringMapCmd initialised with val and err for testing. -func NewStringStringMapResult(val map[string]string, err error) *MapStringStringCmd { +func NewMapStringStringResult(val map[string]string, err error) *MapStringStringCmd { var cmd MapStringStringCmd cmd.val = val cmd.SetErr(err) return &cmd } -// NewStringIntMapCmdResult returns a StringIntMapCmd initialised with val and err for testing. -func NewStringIntMapCmdResult(val map[string]int64, err error) *StringIntMapCmd { - var cmd StringIntMapCmd +// NewMapStringIntCmdResult returns a MapStringIntCmd initialised with val and err for testing. +func NewMapStringIntCmdResult(val map[string]int64, err error) *MapStringIntCmd { + var cmd MapStringIntCmd cmd.val = val cmd.SetErr(err) return &cmd