Add ScanSlice.

This commit is contained in:
Back Yu 2017-02-01 16:36:33 +08:00 committed by Vladimir Mihailenco
parent 6b8c6b3fe9
commit 681a1fe646
4 changed files with 127 additions and 0 deletions

View File

@ -542,6 +542,10 @@ func (cmd *StringSliceCmd) String() string {
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 {
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(stringSliceParser)

View File

@ -3,6 +3,7 @@ package proto
import (
"encoding"
"fmt"
"reflect"
"gopkg.in/redis.v5/internal"
)
@ -105,3 +106,35 @@ func Scan(b []byte, v interface{}) error {
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)
}
}
// Scan a string slice into a custom container
// Example:
// var container []YourStruct; ScanSlice([]string{""},&container)
// var container []*YourStruct; ScanSlice([]string{""},&container)
func ScanSlice(sSlice []string, container interface{}) error {
val := reflect.ValueOf(container)
if !val.IsValid() {
return fmt.Errorf("redis: ScanSlice(nil)")
}
// Check the if the container is pointer
if val.Kind() != reflect.Ptr {
return fmt.Errorf("redis: ScanSlice(non-pointer %T)", container)
}
// slice of the Ptr
val = val.Elem()
// if the container is slice
if val.Kind() != reflect.Slice {
return fmt.Errorf("redis: Wrong object type `%T` for ScanSlice(), need *[]*Type or *[]Type", container)
}
for index, s := range sSlice {
elem := internal.SliceNextElem(val)
if err := Scan([]byte(s), elem.Addr().Interface()); err != nil {
return fmt.Errorf("redis: ScanSlice failed at index of %d => %s, %s", index, s, err.Error())
}
}
return nil
}

View File

@ -0,0 +1,70 @@
package proto
import (
"encoding/json"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type testScanSliceStruct struct {
ID int
Name string
}
func (this *testScanSliceStruct) MarshalBinary() (data []byte, err error) {
return json.Marshal(data)
}
func (this *testScanSliceStruct) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, this)
}
var _ = Describe("ScanSlice", func() {
// Base string array for test.
strAry := []string{`{"ID":-1,"Name":"Back Yu"}`, `{"ID":1,"Name":"szyhf"}`}
// Validate json bytes of container if ScanSlice success
equalJson := Equal([]byte(`[{"ID":-1,"Name":"Back Yu"},{"ID":1,"Name":"szyhf"}]`))
It("var testContainer []testScanSliceStruct", func() {
var testContainer []testScanSliceStruct
err := ScanSlice(strAry, &testContainer)
Expect(err).NotTo(HaveOccurred())
jsonBytes, err := json.Marshal(testContainer)
Expect(err).NotTo(HaveOccurred())
Expect(jsonBytes).Should(equalJson)
})
It("testContainer := new([]testScanSliceStruct)", func() {
testContainer := new([]testScanSliceStruct)
err := ScanSlice(strAry, testContainer)
Expect(err).NotTo(HaveOccurred())
jsonBytes, err := json.Marshal(testContainer)
Expect(err).NotTo(HaveOccurred())
Expect(jsonBytes).Should(equalJson)
})
It("var testContainer []*testScanSliceStruct", func() {
var testContainer []*testScanSliceStruct
err := ScanSlice(strAry, &testContainer)
Expect(err).NotTo(HaveOccurred())
jsonBytes, err := json.Marshal(testContainer)
Expect(err).NotTo(HaveOccurred())
Expect(jsonBytes).Should(equalJson)
})
It("testContainer := new([]*testScanSliceStruct)", func() {
testContainer := new([]*testScanSliceStruct)
err := ScanSlice(strAry, testContainer)
Expect(err).NotTo(HaveOccurred())
jsonBytes, err := json.Marshal(testContainer)
Expect(err).NotTo(HaveOccurred())
Expect(jsonBytes).Should(equalJson)
})
})

View File

@ -1,5 +1,7 @@
package internal
import "reflect"
func ToLower(s string) string {
if isLower(s) {
return s
@ -25,3 +27,21 @@ func isLower(s string) bool {
}
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)
}