Merge pull request #501 from go-redis/feature/scan-slice

Feature/scan slice
This commit is contained in:
Vladimir Mihailenco 2017-02-18 13:17:55 +03:00 committed by GitHub
commit 59a96d9d86
5 changed files with 110 additions and 9 deletions

View File

@ -542,6 +542,10 @@ func (cmd *StringSliceCmd) String() string {
return cmdString(cmd, cmd.val) return cmdString(cmd, cmd.val)
} }
func (cmd *StringSliceCmd) ScanSlice(container interface{}) error {
return proto.ScanSlice(cmd.Val(), container)
}
func (cmd *StringSliceCmd) readReply(cn *pool.Conn) error { func (cmd *StringSliceCmd) readReply(cn *pool.Conn) error {
var v interface{} var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(stringSliceParser) v, cmd.err = cn.Rd.ReadArrayReply(stringSliceParser)

View File

@ -250,11 +250,11 @@ var _ = Describe("Commands", func() {
Expect(exists.Err()).NotTo(HaveOccurred()) Expect(exists.Err()).NotTo(HaveOccurred())
Expect(exists.Val()).To(Equal(false)) Expect(exists.Val()).To(Equal(false))
existsMul := client.ExistsMulti("key1", "key2") existsMul := client.ExistsMulti("key1", "key2")
Expect(existsMul.Err()).NotTo(HaveOccurred()) Expect(existsMul.Err()).NotTo(HaveOccurred())
Expect(existsMul.Val()).To(Equal(int64(1))) Expect(existsMul.Val()).To(Equal(int64(1)))
existsMul = client.ExistsMulti("key1", "key1") existsMul = client.ExistsMulti("key1", "key1")
Expect(existsMul.Err()).NotTo(HaveOccurred()) Expect(existsMul.Err()).NotTo(HaveOccurred())
Expect(existsMul.Val()).To(Equal(int64(2))) Expect(existsMul.Val()).To(Equal(int64(2)))
}) })
@ -1264,14 +1264,19 @@ var _ = Describe("Commands", func() {
}) })
It("should HVals", func() { It("should HVals", func() {
hSet := client.HSet("hash", "key1", "hello1") err := client.HSet("hash", "key1", "hello1").Err()
Expect(hSet.Err()).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
hSet = client.HSet("hash", "key2", "hello2") err = client.HSet("hash", "key2", "hello2").Err()
Expect(hSet.Err()).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
hVals := client.HVals("hash") v, err := client.HVals("hash").Result()
Expect(hVals.Err()).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(hVals.Val()).To(Equal([]string{"hello1", "hello2"})) Expect(v).To(Equal([]string{"hello1", "hello2"}))
var slice []string
err = client.HVals("hash").ScanSlice(&slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Equal([]string{"hello1", "hello2"}))
}) })
}) })

View File

@ -3,6 +3,7 @@ package proto
import ( import (
"encoding" "encoding"
"fmt" "fmt"
"reflect"
"gopkg.in/redis.v5/internal" "gopkg.in/redis.v5/internal"
) )
@ -105,3 +106,26 @@ func Scan(b []byte, v interface{}) error {
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v) "redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)
} }
} }
func ScanSlice(data []string, slice interface{}) error {
v := reflect.ValueOf(slice)
if !v.IsValid() {
return fmt.Errorf("redis: ScanSlice(nil)")
}
if v.Kind() != reflect.Ptr {
return fmt.Errorf("redis: ScanSlice(non-pointer %T)", slice)
}
v = v.Elem()
if v.Kind() != reflect.Slice {
return fmt.Errorf("redis: ScanSlice(non-slice %T)", slice)
}
for i, s := range data {
elem := internal.SliceNextElem(v)
if err := Scan([]byte(s), elem.Addr().Interface()); err != nil {
return fmt.Errorf("redis: ScanSlice(index=%d value=%q) failed: %s", i, s, err)
}
}
return nil
}

View File

@ -0,0 +1,48 @@
package proto
import (
"encoding/json"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type testScanSliceStruct struct {
ID int
Name string
}
func (s *testScanSliceStruct) MarshalBinary() ([]byte, error) {
return json.Marshal(s)
}
func (s *testScanSliceStruct) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, s)
}
var _ = Describe("ScanSlice", func() {
data := []string{
`{"ID":-1,"Name":"Back Yu"}`,
`{"ID":1,"Name":"szyhf"}`,
}
It("[]testScanSliceStruct", func() {
var slice []testScanSliceStruct
err := ScanSlice(data, &slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Equal([]testScanSliceStruct{
{-1, "Back Yu"},
{1, "szyhf"},
}))
})
It("var testContainer []*testScanSliceStruct", func() {
var slice []*testScanSliceStruct
err := ScanSlice(data, &slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Equal([]*testScanSliceStruct{
{-1, "Back Yu"},
{1, "szyhf"},
}))
})
})

View File

@ -1,5 +1,7 @@
package internal package internal
import "reflect"
func ToLower(s string) string { func ToLower(s string) string {
if isLower(s) { if isLower(s) {
return s return s
@ -25,3 +27,21 @@ func isLower(s string) bool {
} }
return true return true
} }
func SliceNextElem(v reflect.Value) reflect.Value {
if v.Len() < v.Cap() {
v.Set(v.Slice(0, v.Len()+1))
return v.Index(v.Len() - 1)
}
elemType := v.Type().Elem()
if elemType.Kind() == reflect.Ptr {
elem := reflect.New(elemType.Elem())
v.Set(reflect.Append(v, elem))
return elem.Elem()
}
v.Set(reflect.Append(v, reflect.Zero(elemType)))
return v.Index(v.Len() - 1)
}