mirror of
https://github.com/nalgeon/redka.git
synced 2025-10-16 21:10:37 +08:00
37 lines
740 B
Go
37 lines
740 B
Go
package hash
|
|
|
|
import "github.com/nalgeon/redka/internal/redis"
|
|
|
|
// Determines whether a field exists in a hash.
|
|
// HEXISTS key field
|
|
// https://redis.io/commands/hexists
|
|
type HExists struct {
|
|
redis.BaseCmd
|
|
Key string
|
|
Field string
|
|
}
|
|
|
|
func ParseHExists(b redis.BaseCmd) (*HExists, error) {
|
|
cmd := &HExists{BaseCmd: b}
|
|
if len(cmd.Args()) != 2 {
|
|
return cmd, redis.ErrInvalidArgNum
|
|
}
|
|
cmd.Key = string(cmd.Args()[0])
|
|
cmd.Field = string(cmd.Args()[1])
|
|
return cmd, nil
|
|
}
|
|
|
|
func (cmd *HExists) Run(w redis.Writer, red redis.Redka) (any, error) {
|
|
ok, err := red.Hash().Exists(cmd.Key, cmd.Field)
|
|
if err != nil {
|
|
w.WriteError(cmd.Error(err))
|
|
return nil, err
|
|
}
|
|
if ok {
|
|
w.WriteInt(1)
|
|
} else {
|
|
w.WriteInt(0)
|
|
}
|
|
return ok, nil
|
|
}
|