go-redis
go-redis copied to clipboard
slice result conversion functions in Cmd are broken
Issue tracker is used for reporting bugs and discussing new features. Please use stackoverflow for supporting issues.
if one executes a command via client.Do() which will return a Slice, the built-in slice helper methods (StringSlince, Int64Slice, BoolSlice, etc) methods of Cmd do not work as expected. Because each of those methods first calls Slice() and Slice() selects on val.(type) matching []interface{} or else throwing an error, any command more strongly typed than SliceCmd (which expects a slice of interface{}) will throw an exception when using the conversion helper methods, since []int64, []string, etc do not satisfy []interface{}. You have to explicitly create a slice of interface{} to match that type. The net result is that it is possible to call cmd.Val() or cmd.Result() and then cast the returned val to []int64 or []string, but you cannot call cmd.Int64Slice() or cmd.StringSlice() to have the conversion done for you - which is the whole point of the helper methods, I think.
Expected Behavior
cmd := rdb.Do(ctx, "Keys", pattern)
values, err := cmd.StringSlice()
should result in values being populated with the returned values of the command and err being nil
Current Behavior
Instead, err receives a value expressing that unexpected type=[]string is not []interface{}
Possible Solution
remove the call to Slice() within the various
Your analysis sounds correct. Could you send a PR that improves StringSlice to handle []string?
@ideasculptor Which version do you use? I tried as you said and StringSlice worked as expected on me(6.2.6). Do you have a sample code?
This issue is marked stale. It will be closed in 30 days if it is not updated.