1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-31 05:04:23 +03:00

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

@ -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)
})
})