Skip to content

Instantly share code, notes, and snippets.

@clsung
Created October 1, 2016 17:42
Show Gist options
  • Save clsung/af47170bfaa0db1ca4c292019f82d143 to your computer and use it in GitHub Desktop.
Save clsung/af47170bfaa0db1ca4c292019f82d143 to your computer and use it in GitHub Desktop.
// Strings is a helper that converts an array command reply to a []string. If
// err is not equal to nil, then Strings returns nil, err. Nil array items are
// converted to "" in the output slice. Strings returns an error if an array
// item is not a bulk string or nil.
func Strings(reply interface{}, err error) ([]string, error) {
if err != nil {
return nil, err
}
switch reply := reply.(type) {
case []interface{}:
result := make([]string, len(reply))
for i := range reply {
if reply[i] == nil {
continue
}
p, ok := reply[i].([]byte)
if !ok {
return nil, fmt.Errorf("redigo: unexpected element type for Strings, got type %T", reply[i])
}
result[i] = string(p)
}
return result, nil
case nil:
return nil, ErrNil
case Error:
return nil, reply
}
return nil, fmt.Errorf("redigo: unexpected type for Strings, got type %T", reply)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment