mirror of https://github.com/go-redis/redis.git
Add missing error checks and support for MGET in Scan()
This commit is contained in:
parent
e113512c18
commit
600f1665a0
22
command.go
22
command.go
|
@ -375,9 +375,21 @@ func (cmd *SliceCmd) String() string {
|
|||
// 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)
|
||||
if cmd.err != nil {
|
||||
return cmd.err
|
||||
}
|
||||
|
||||
// Pass the list of keys and values.
|
||||
// Skip the first two args for: HMGET key
|
||||
var args []interface{}
|
||||
if cmd.args[0] == "hmget" {
|
||||
args = cmd.args[2:]
|
||||
} else {
|
||||
// Otherwise, it's: MGET field field ...
|
||||
args = cmd.args[1:]
|
||||
}
|
||||
|
||||
return hscan.Scan(args, cmd.val, dest)
|
||||
}
|
||||
|
||||
func (cmd *SliceCmd) readReply(rd *proto.Reader) error {
|
||||
|
@ -929,6 +941,10 @@ func (cmd *StringStringMapCmd) String() string {
|
|||
// 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 {
|
||||
if cmd.err != nil {
|
||||
return cmd.err
|
||||
}
|
||||
|
||||
// Pass the list of keys and values. Skip the first to args (command, key),
|
||||
// eg: HGETALL map.
|
||||
var (
|
||||
|
|
|
@ -310,6 +310,39 @@ func ExampleStringStringMapCmd_Scan() {
|
|||
// Output: {hello 123 true}
|
||||
}
|
||||
|
||||
// ExampleSliceCmd_Scan shows how to scan the results of a multi key fetch
|
||||
// into a struct.
|
||||
func ExampleSliceCmd_Scan() {
|
||||
rdb.FlushDB(ctx)
|
||||
err := rdb.MSet(ctx,
|
||||
"name", "hello",
|
||||
"count", 123,
|
||||
"correct", true).Err()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
res := rdb.MGet(ctx, "name", "count", "empty", "correct")
|
||||
if res.Err() != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
type data struct {
|
||||
Name string `redis:"name"`
|
||||
Count int `redis:"count"`
|
||||
Correct bool `redis:"correct"`
|
||||
}
|
||||
|
||||
// Scan the results into the struct.
|
||||
var d data
|
||||
if err := res.Scan(&d); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(d)
|
||||
// Output: {hello 123 true}
|
||||
}
|
||||
|
||||
func ExampleClient_Pipelined() {
|
||||
var incr *redis.IntCmd
|
||||
_, err := rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
|
|
Loading…
Reference in New Issue