mirror of https://github.com/go-redis/redis.git
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() {
|
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"}))
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
@ -107,34 +107,25 @@ func Scan(b []byte, v interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan a string slice into a custom container
|
func ScanSlice(data []string, slice interface{}) error {
|
||||||
// Example:
|
v := reflect.ValueOf(slice)
|
||||||
// var container []YourStruct; ScanSlice([]string{""},&container)
|
if !v.IsValid() {
|
||||||
// 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)")
|
return fmt.Errorf("redis: ScanSlice(nil)")
|
||||||
}
|
}
|
||||||
|
if v.Kind() != reflect.Ptr {
|
||||||
// Check the if the container is pointer
|
return fmt.Errorf("redis: ScanSlice(non-pointer %T)", slice)
|
||||||
if val.Kind() != reflect.Ptr {
|
}
|
||||||
return fmt.Errorf("redis: ScanSlice(non-pointer %T)", container)
|
v = v.Elem()
|
||||||
|
if v.Kind() != reflect.Slice {
|
||||||
|
return fmt.Errorf("redis: ScanSlice(non-slice %T)", slice)
|
||||||
}
|
}
|
||||||
|
|
||||||
// slice of the Ptr
|
for i, s := range data {
|
||||||
val = val.Elem()
|
elem := internal.SliceNextElem(v)
|
||||||
// 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 {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,59 +12,37 @@ type testScanSliceStruct struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *testScanSliceStruct) MarshalBinary() (data []byte, err error) {
|
func (s *testScanSliceStruct) MarshalBinary() ([]byte, error) {
|
||||||
return json.Marshal(data)
|
return json.Marshal(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *testScanSliceStruct) UnmarshalBinary(data []byte) error {
|
func (s *testScanSliceStruct) UnmarshalBinary(b []byte) error {
|
||||||
return json.Unmarshal(data, this)
|
return json.Unmarshal(b, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = Describe("ScanSlice", func() {
|
var _ = Describe("ScanSlice", func() {
|
||||||
|
data := []string{
|
||||||
|
`{"ID":-1,"Name":"Back Yu"}`,
|
||||||
|
`{"ID":1,"Name":"szyhf"}`,
|
||||||
|
}
|
||||||
|
|
||||||
// Base string array for test.
|
It("[]testScanSliceStruct", func() {
|
||||||
strAry := []string{`{"ID":-1,"Name":"Back Yu"}`, `{"ID":1,"Name":"szyhf"}`}
|
var slice []testScanSliceStruct
|
||||||
// Validate json bytes of container if ScanSlice success
|
err := ScanSlice(data, &slice)
|
||||||
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())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(slice).To(Equal([]testScanSliceStruct{
|
||||||
jsonBytes, err := json.Marshal(testContainer)
|
{-1, "Back Yu"},
|
||||||
Expect(err).NotTo(HaveOccurred())
|
{1, "szyhf"},
|
||||||
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() {
|
It("var testContainer []*testScanSliceStruct", func() {
|
||||||
var testContainer []*testScanSliceStruct
|
var slice []*testScanSliceStruct
|
||||||
err := ScanSlice(strAry, &testContainer)
|
err := ScanSlice(data, &slice)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(slice).To(Equal([]*testScanSliceStruct{
|
||||||
jsonBytes, err := json.Marshal(testContainer)
|
{-1, "Back Yu"},
|
||||||
Expect(err).NotTo(HaveOccurred())
|
{1, "szyhf"},
|
||||||
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)
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue