add: hscan data struct

This commit is contained in:
lxc 2024-02-23 14:51:20 +08:00
parent d43a9fa887
commit a77a6bb3d4
3 changed files with 37 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package redis_test
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"sync" "sync"
@ -344,6 +345,24 @@ func ExampleClient_ScanType_hashType() {
// Output: 33 keys ready for use // Output: 33 keys ready for use
} }
type Income struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
}
func (p *Income) MarshalBinary() (data []byte, err error) {
return json.Marshal(*p)
}
func (p *Income) UnmarshalBinary(data []byte) error {
val := Income{}
if err := json.Unmarshal(data, &val); err != nil {
return err
}
*p = val
return nil
}
// ExampleMapStringStringCmd_Scan shows how to scan the results of a map fetch // ExampleMapStringStringCmd_Scan shows how to scan the results of a map fetch
// into a struct. // into a struct.
func ExampleMapStringStringCmd_Scan() { func ExampleMapStringStringCmd_Scan() {
@ -351,7 +370,9 @@ func ExampleMapStringStringCmd_Scan() {
err := rdb.HMSet(ctx, "map", err := rdb.HMSet(ctx, "map",
"name", "hello", "name", "hello",
"count", 123, "count", 123,
"correct", true).Err() "correct", true,
"income", &Income{Max: 10, Min: 1},
).Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -366,6 +387,7 @@ func ExampleMapStringStringCmd_Scan() {
Name string `redis:"name"` Name string `redis:"name"`
Count int `redis:"count"` Count int `redis:"count"`
Correct bool `redis:"correct"` Correct bool `redis:"correct"`
Income *Income `redis:"income"`
} }
// Scan the results into the struct. // Scan the results into the struct.
@ -374,8 +396,9 @@ func ExampleMapStringStringCmd_Scan() {
panic(err) panic(err)
} }
fmt.Println(d) fmt.Printf("name: %s count: %d correct: %v income: {max: %.0f, mix: %.0f}",
// Output: {hello 123 true} d.Name, d.Count, d.Correct, d.Income.Max, d.Income.Min)
// Output: name: hello count: 123 correct: true income: {max: 10, mix: 1}
} }
// ExampleSliceCmd_Scan shows how to scan the results of a multi key fetch // ExampleSliceCmd_Scan shows how to scan the results of a multi key fetch

View File

@ -107,6 +107,8 @@ func (s StructValue) Scan(key string, value string) error {
switch scan := v.Interface().(type) { switch scan := v.Interface().(type) {
case Scanner: case Scanner:
return scan.ScanRedis(value) return scan.ScanRedis(value)
case encoding.BinaryUnmarshaler:
return scan.UnmarshalBinary(util.StringToBytes(value))
case encoding.TextUnmarshaler: case encoding.TextUnmarshaler:
return scan.UnmarshalText(util.StringToBytes(value)) return scan.UnmarshalText(util.StringToBytes(value))
} }

View File

@ -138,6 +138,12 @@ func (w *Writer) WriteArg(v interface{}) error {
return err return err
} }
return w.bytes(b) return w.bytes(b)
case encoding.TextMarshaler:
b, err := v.MarshalText()
if err != nil {
return err
}
return w.bytes(b)
case net.IP: case net.IP:
return w.bytes(v) return w.bytes(v)
default: default: