Skip to content

Commit

Permalink
Add ScanSlice.
Browse files Browse the repository at this point in the history
  • Loading branch information
szyhf authored and vmihailenco committed Feb 18, 2017
1 parent 6b8c6b3 commit 681a1fe
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
4 changes: 4 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions internal/proto/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proto
import (
"encoding"
"fmt"
"reflect"

"gopkg.in/redis.v5/internal"
)
Expand Down Expand Up @@ -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
}
70 changes: 70 additions & 0 deletions internal/proto/scan_test.go
Original file line number Diff line number Diff line change
@@ -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)
})

})
20 changes: 20 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package internal

import "reflect"

func ToLower(s string) string {
if isLower(s) {
return s
Expand All @@ -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)
}

0 comments on commit 681a1fe

Please sign in to comment.