mirror of
https://github.com/nalgeon/redka.git
synced 2025-10-26 17:40:28 +08:00
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package hash
|
|
|
|
import (
|
|
"github.com/nalgeon/redka/internal/parser"
|
|
"github.com/nalgeon/redka/internal/redis"
|
|
)
|
|
|
|
// Iterates over fields and values of a hash.
|
|
// HSCAN key cursor [MATCH pattern] [COUNT count]
|
|
// https://redis.io/commands/hscan
|
|
type HScan struct {
|
|
redis.BaseCmd
|
|
key string
|
|
cursor int
|
|
match string
|
|
count int
|
|
}
|
|
|
|
func ParseHScan(b redis.BaseCmd) (*HScan, error) {
|
|
cmd := &HScan{BaseCmd: b}
|
|
|
|
err := parser.New(
|
|
parser.String(&cmd.key),
|
|
parser.Int(&cmd.cursor),
|
|
parser.Named("match", parser.String(&cmd.match)),
|
|
parser.Named("count", parser.Int(&cmd.count)),
|
|
).Required(2).Run(cmd.Args())
|
|
if err != nil {
|
|
return cmd, err
|
|
}
|
|
|
|
// all keys by default
|
|
if cmd.match == "" {
|
|
cmd.match = "*"
|
|
}
|
|
|
|
return cmd, nil
|
|
}
|
|
|
|
func (cmd *HScan) Run(w redis.Writer, red redis.Redka) (any, error) {
|
|
res, err := red.Hash().Scan(cmd.key, cmd.cursor, cmd.match, cmd.count)
|
|
if err != nil {
|
|
w.WriteError(cmd.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
w.WriteArray(2)
|
|
w.WriteInt(res.Cursor)
|
|
w.WriteArray(len(res.Items) * 2)
|
|
for _, it := range res.Items {
|
|
w.WriteBulkString(it.Field)
|
|
w.WriteBulk(it.Value)
|
|
}
|
|
return res, nil
|
|
}
|