Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions redis/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ var errScanStructValue = errors.New("redigo.ScanStruct: value must be non-nil po
// ScanStruct uses exported field names to match values in the response. Use
// 'redis' field tag to override the name:
//
// Field int `redis:"myName"`
// Field int `redis:"myName"`
//
// Fields with the tag redis:"-" are ignored.
//
Expand Down Expand Up @@ -513,9 +513,9 @@ func ScanStruct(src []interface{}, dest interface{}) error {
continue
}

name, ok := src[i].([]byte)
name, ok := convertToBulk(src[i])
if !ok {
return fmt.Errorf("redigo.ScanStruct: key %d not a bulk string value", i)
return fmt.Errorf("redigo.ScanStruct: key %d not a bulk string value got type: %T", i, src[i])
}

fs := ss.fieldSpec(name)
Expand All @@ -530,6 +530,19 @@ func ScanStruct(src []interface{}, dest interface{}) error {
return nil
}

// convertToBulk converts src to a []byte if src is a string or bulk string
// and returns true. Otherwise nil and false is returned.
func convertToBulk(src interface{}) ([]byte, bool) {
switch v := src.(type) {
case []byte:
return v, true
case string:
return []byte(v), true
default:
return nil, false
}
}

var (
errScanSliceValue = errors.New("redigo.ScanSlice: dest must be non-nil pointer to a struct")
)
Expand Down
16 changes: 16 additions & 0 deletions redis/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ func TestScanStruct(t *testing.T) {
}
}

func TestScanStructStringKeys(t *testing.T) {
reply := []interface{}{"simple", []byte("value"), "number", []byte("123")}
expected := &struct {
Simple string `redis:"simple"`
Number int `redis:"number"`
}{
Simple: "value",
Number: 123,
}

value := reflect.New(reflect.ValueOf(expected).Type().Elem()).Interface()
err := redis.ScanStruct(reply, value)
require.NoError(t, err)
require.Equal(t, expected, value)
}

func TestBadScanStructArgs(t *testing.T) {
x := []interface{}{"A", "b"}
test := func(v interface{}) {
Expand Down
Loading