Pārlūkot izejas kodu

Handle struct with no fields in ScanSlice.

Gary Burd 12 gadi atpakaļ
vecāks
revīzija
77406400ad
2 mainītis faili ar 12 papildinājumiem un 2 dzēšanām
  1. 6 2
      redis/scan.go
  2. 6 0
      redis/scan_test.go

+ 6 - 2
redis/scan.go

@@ -431,14 +431,18 @@ func ScanSlice(src []interface{}, dest interface{}, fieldNames ...string) error
 		for i, name := range fieldNames {
 			fss[i] = ss.m[name]
 			if fss[i] == nil {
-				return errors.New("redigo: bad field name " + name)
+				return errors.New("redigo: ScanSlice bad field name " + name)
 			}
 		}
 	}
 
+	if len(fss) == 0 {
+		return errors.New("redigo: ScanSlice no struct fields.")
+	}
+
 	n := len(src) / len(fss)
 	if n*len(fss) != len(src) {
-		return errors.New("redigo: length of ScanSlice not a multiple of struct field count.")
+		return errors.New("redigo: ScanSlice length not a multiple of struct field count.")
 	}
 
 	ensureLen(d, n)

+ 6 - 0
redis/scan_test.go

@@ -264,6 +264,12 @@ var scanSliceTests = []struct {
 		true,
 		[]struct{ A, C, B string }{{"a1", "", "b1"}, {"a2", "", "b2"}},
 	},
+	{
+		[]interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
+		nil,
+		false,
+		[]struct{}{},
+	},
 }
 
 func TestScanSlice(t *testing.T) {