forked from mirror/redis
Cleanup code a bit.
This commit is contained in:
parent
681a1fe646
commit
335956cc9a
|
@ -1264,14 +1264,19 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should HVals", func() {
|
||||
hSet := client.HSet("hash", "key1", "hello1")
|
||||
Expect(hSet.Err()).NotTo(HaveOccurred())
|
||||
hSet = client.HSet("hash", "key2", "hello2")
|
||||
Expect(hSet.Err()).NotTo(HaveOccurred())
|
||||
err := client.HSet("hash", "key1", "hello1").Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = client.HSet("hash", "key2", "hello2").Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
hVals := client.HVals("hash")
|
||||
Expect(hVals.Err()).NotTo(HaveOccurred())
|
||||
Expect(hVals.Val()).To(Equal([]string{"hello1", "hello2"}))
|
||||
v, err := client.HVals("hash").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
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"}))
|
||||
})
|
||||
|
||||
})
|
||||
|
|
|
@ -107,34 +107,25 @@ func Scan(b []byte, v interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
// 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() {
|
||||
func ScanSlice(data []string, slice interface{}) error {
|
||||
v := reflect.ValueOf(slice)
|
||||
if !v.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)
|
||||
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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
for i, s := range data {
|
||||
elem := internal.SliceNextElem(v)
|
||||
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 fmt.Errorf("redis: ScanSlice(index=%d value=%q) failed: %s", i, s, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -12,59 +12,37 @@ type testScanSliceStruct struct {
|
|||
Name string
|
||||
}
|
||||
|
||||
func (this *testScanSliceStruct) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(data)
|
||||
func (s *testScanSliceStruct) MarshalBinary() ([]byte, error) {
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
func (this *testScanSliceStruct) UnmarshalBinary(data []byte) error {
|
||||
return json.Unmarshal(data, this)
|
||||
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"}`,
|
||||
}
|
||||
|
||||
// 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)
|
||||
It("[]testScanSliceStruct", func() {
|
||||
var slice []testScanSliceStruct
|
||||
err := ScanSlice(data, &slice)
|
||||
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)
|
||||
Expect(slice).To(Equal([]testScanSliceStruct{
|
||||
{-1, "Back Yu"},
|
||||
{1, "szyhf"},
|
||||
}))
|
||||
})
|
||||
|
||||
It("var testContainer []*testScanSliceStruct", func() {
|
||||
var testContainer []*testScanSliceStruct
|
||||
err := ScanSlice(strAry, &testContainer)
|
||||
var slice []*testScanSliceStruct
|
||||
err := ScanSlice(data, &slice)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
jsonBytes, err := json.Marshal(testContainer)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(jsonBytes).Should(equalJson)
|
||||
Expect(slice).To(Equal([]*testScanSliceStruct{
|
||||
{-1, "Back Yu"},
|
||||
{1, "szyhf"},
|
||||
}))
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue