1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-28 06:42:00 +03:00

Refactor scan signature to work with Slice and StringMap cmds

This commit is contained in:
Kailash Nadh
2021-02-02 16:28:10 +05:30
parent a4144ea98e
commit f9dfc7a949
4 changed files with 74 additions and 47 deletions

View File

@ -372,11 +372,12 @@ func (cmd *SliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
// Scan scans the results from a key-value Redis map result set ([]interface{})
// like HMGET and HGETALL to a destination struct.
// The Redis keys are matched to the struct's field with the `redis` tag.
func (cmd *SliceCmd) Scan(val interface{}) error {
return hscan.Scan(cmd.val, val)
// Scan scans the results from the map into a destination struct. The map keys
// are matched in the Redis struct fields by the `redis:"field"` tag.
func (cmd *SliceCmd) Scan(dest interface{}) error {
// Pass the list of keys and values. Skip the first to args (command, key),
// eg: HMGET map.
return hscan.Scan(cmd.args[2:], cmd.val, dest)
}
func (cmd *SliceCmd) readReply(rd *proto.Reader) error {
@ -925,6 +926,23 @@ func (cmd *StringStringMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
// Scan scans the results from the map into a destination struct. The map keys
// are matched in the Redis struct fields by the `redis:"field"` tag.
func (cmd *StringStringMapCmd) Scan(dest interface{}) error {
// Pass the list of keys and values. Skip the first to args (command, key),
// eg: HGETALL map.
var (
keys = make([]interface{}, 0, len(cmd.val))
vals = make([]interface{}, 0, len(cmd.val))
)
for k, v := range cmd.val {
keys = append(keys, k)
vals = append(vals, v)
}
return hscan.Scan(keys, vals, dest)
}
func (cmd *StringStringMapCmd) readReply(rd *proto.Reader) error {
_, err := rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
cmd.val = make(map[string]string, n/2)