From 655336673b5549556800106e0dba86edc8a108c7 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 21 Aug 2018 14:11:19 +0300 Subject: [PATCH] Add Int helper --- command.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/command.go b/command.go index 44a49877..ca44d7c8 100644 --- a/command.go +++ b/command.go @@ -173,6 +173,21 @@ func (cmd *Cmd) String() (string, error) { } } +func (cmd *Cmd) Int() (int, error) { + if cmd.err != nil { + return 0, cmd.err + } + switch val := cmd.val.(type) { + case int64: + return int(val), nil + case string: + return strconv.Atoi(val) + default: + err := fmt.Errorf("redis: unexpected type=%T for Int64", val) + return 0, err + } +} + func (cmd *Cmd) Int64() (int64, error) { if cmd.err != nil { return 0, cmd.err @@ -549,6 +564,13 @@ func (cmd *StringCmd) Bytes() ([]byte, error) { return []byte(cmd.val), cmd.err } +func (cmd *StringCmd) Int() (int, error) { + if cmd.err != nil { + return 0, cmd.err + } + return strconv.Atoi(cmd.Val()) +} + func (cmd *StringCmd) Int64() (int64, error) { if cmd.err != nil { return 0, cmd.err