Add Int helper

This commit is contained in:
Vladimir Mihailenco 2018-08-21 14:11:19 +03:00
parent 21826c1c21
commit 655336673b
1 changed files with 22 additions and 0 deletions

View File

@ -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) { func (cmd *Cmd) Int64() (int64, error) {
if cmd.err != nil { if cmd.err != nil {
return 0, cmd.err return 0, cmd.err
@ -549,6 +564,13 @@ func (cmd *StringCmd) Bytes() ([]byte, error) {
return []byte(cmd.val), cmd.err 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) { func (cmd *StringCmd) Int64() (int64, error) {
if cmd.err != nil { if cmd.err != nil {
return 0, cmd.err return 0, cmd.err