go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

slice result conversion functions in Cmd are broken

Open ideasculptor opened this issue 3 years ago • 2 comments

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 Slice() helper methods in Cmd. In the meantime, the issue can be worked around by always calling cmd.Val() and then casting the interface{} returned by that method to the expected type.

ideasculptor avatar Jan 27 '22 07:01 ideasculptor

Your analysis sounds correct. Could you send a PR that improves StringSlice to handle []string?

vmihailenco avatar Jan 28 '22 13:01 vmihailenco

@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?

ykalayy avatar Apr 10 '22 00:04 ykalayy

This issue is marked stale. It will be closed in 30 days if it is not updated.

github-actions[bot] avatar Sep 22 '23 00:09 github-actions[bot]