Merge pull request #407 from go-redis/fix/time-cmd

Fix/time cmd
This commit is contained in:
Vladimir Mihailenco 2016-10-14 14:50:30 +03:00 committed by GitHub
commit 084336d5eb
5 changed files with 82 additions and 10 deletions

View File

@ -355,6 +355,48 @@ func (cmd *DurationCmd) readReply(cn *pool.Conn) error {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type TimeCmd struct {
baseCmd
val time.Time
}
func NewTimeCmd(args ...interface{}) *TimeCmd {
cmd := newBaseCmd(args)
return &TimeCmd{
baseCmd: cmd,
}
}
func (cmd *TimeCmd) reset() {
cmd.val = time.Time{}
cmd.err = nil
}
func (cmd *TimeCmd) Val() time.Time {
return cmd.val
}
func (cmd *TimeCmd) Result() (time.Time, error) {
return cmd.val, cmd.err
}
func (cmd *TimeCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *TimeCmd) readReply(cn *pool.Conn) error {
v, err := cn.Rd.ReadArrayReply(timeParser)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(time.Time)
return nil
}
//------------------------------------------------------------------------------
type BoolCmd struct { type BoolCmd struct {
baseCmd baseCmd

View File

@ -199,7 +199,7 @@ type Cmdable interface {
ShutdownSave() *StatusCmd ShutdownSave() *StatusCmd
ShutdownNoSave() *StatusCmd ShutdownNoSave() *StatusCmd
SlaveOf(host, port string) *StatusCmd SlaveOf(host, port string) *StatusCmd
Time() *StringSliceCmd Time() *TimeCmd
Eval(script string, keys []string, args ...interface{}) *Cmd Eval(script string, keys []string, args ...interface{}) *Cmd
EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
ScriptExists(scripts ...string) *BoolSliceCmd ScriptExists(scripts ...string) *BoolSliceCmd
@ -1741,9 +1741,8 @@ func (c *cmdable) Sync() {
panic("not implemented") panic("not implemented")
} }
// TODO: add TimeCmd and use it here func (c *cmdable) Time() *TimeCmd {
func (c *cmdable) Time() *StringSliceCmd { cmd := NewTimeCmd("time")
cmd := NewStringSliceCmd("time")
c.process(cmd) c.process(cmd)
return cmd return cmd
} }

View File

@ -159,9 +159,9 @@ var _ = Describe("Commands", func() {
}) })
It("should Time", func() { It("should Time", func() {
time := client.Time() tm, err := client.Time().Result()
Expect(time.Err()).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(time.Val()).To(HaveLen(2)) Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second))
}) })
}) })

View File

@ -77,7 +77,7 @@ func (p *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {
case IntReply: case IntReply:
return parseIntValue(line) return parseIntValue(line)
case StringReply: case StringReply:
return p.parseBytesValue(line) return p.readBytesValue(line)
case ArrayReply: case ArrayReply:
n, err := parseArrayLen(line) n, err := parseArrayLen(line)
if err != nil { if err != nil {
@ -112,7 +112,7 @@ func (p *Reader) ReadBytesReply() ([]byte, error) {
case ErrorReply: case ErrorReply:
return nil, parseErrorValue(line) return nil, parseErrorValue(line)
case StringReply: case StringReply:
return p.parseBytesValue(line) return p.readBytesValue(line)
case StatusReply: case StatusReply:
return parseStatusValue(line) return parseStatusValue(line)
default: default:
@ -206,7 +206,7 @@ func (p *Reader) ReadScanReply() ([]string, uint64, error) {
return keys, cursor, err return keys, cursor, err
} }
func (p *Reader) parseBytesValue(line []byte) ([]byte, error) { func (p *Reader) readBytesValue(line []byte) ([]byte, error) {
if isNilReply(line) { if isNilReply(line) {
return nil, internal.Nil return nil, internal.Nil
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
"time"
"gopkg.in/redis.v5/internal/proto" "gopkg.in/redis.v5/internal/proto"
) )
@ -364,6 +365,7 @@ func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
return &cmd, nil return &cmd, nil
} }
// Implements proto.MultiBulkParse
func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) { func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
m := make(map[string]*CommandInfo, n) m := make(map[string]*CommandInfo, n)
for i := int64(0); i < n; i++ { for i := int64(0); i < n; i++ {
@ -377,3 +379,32 @@ func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
} }
return m, nil return m, nil
} }
// Implements proto.MultiBulkParse
func timeParser(rd *proto.Reader, n int64) (interface{}, error) {
if n != 2 {
fmt.Errorf("got %d elements, expected 2", n)
}
secStr, err := rd.ReadStringReply()
if err != nil {
return nil, err
}
microsecStr, err := rd.ReadStringReply()
if err != nil {
return nil, err
}
sec, err := strconv.ParseInt(secStr, 10, 64)
if err != nil {
return nil, err
}
microsec, err := strconv.ParseInt(microsecStr, 10, 64)
if err != nil {
return nil, err
}
return time.Unix(sec, microsec*1000), nil
}