From 2b2a6805dd26257c4ca6ce4ac6e488a3218218df Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sat, 6 Feb 2016 11:45:34 +0200 Subject: [PATCH] Fix cluster slots parsing. --- command.go | 2 ++ parser.go | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/command.go b/command.go index 25f9dc5..1986c66 100644 --- a/command.go +++ b/command.go @@ -725,12 +725,14 @@ func (cmd *ScanCmd) readReply(cn *conn) error { //------------------------------------------------------------------------------ +// TODO: rename to ClusterSlot type ClusterSlotInfo struct { Start int End int Addrs []string } +// TODO: rename to ClusterSlotsCmd type ClusterSlotCmd struct { baseCmd diff --git a/parser.go b/parser.go index 741c02a..b99ee89 100644 --- a/parser.go +++ b/parser.go @@ -107,7 +107,7 @@ func appendArg(b []byte, val interface{}) ([]byte, error) { } func appendArgs(b []byte, args []interface{}) ([]byte, error) { - b = append(b, '*') + b = append(b, arrayReply) b = strconv.AppendUint(b, uint64(len(args)), 10) b = append(b, '\r', '\n') for _, arg := range args { @@ -238,7 +238,9 @@ func readLine(cn *conn) ([]byte, error) { } func isNilReply(b []byte) bool { - return len(b) == 3 && (b[0] == '$' || b[0] == '*') && b[1] == '-' && b[2] == '1' + return len(b) == 3 && + (b[0] == stringReply || b[0] == arrayReply) && + b[1] == '-' && b[2] == '1' } func readN(cn *conn, n int) ([]byte, error) { @@ -337,7 +339,7 @@ func readFloatReply(cn *conn) (float64, error) { } func parseArrayHeader(cn *conn, line []byte) (int64, error) { - if len(line) == 3 && line[1] == '-' && line[2] == '1' { + if isNilReply(line) { return 0, Nil } @@ -604,8 +606,9 @@ func clusterSlotInfoSliceParser(cn *conn, n int64) (interface{}, error) { if err != nil { return nil, err } - if n != 2 { - return nil, fmt.Errorf("got %d elements in cluster info address, expected 2", n) + if n != 2 && n != 3 { + err := fmt.Errorf("got %d elements in cluster info address, expected 2 or 3", n) + return nil, err } ip, err := readStringReply(cn) @@ -618,6 +621,14 @@ func clusterSlotInfoSliceParser(cn *conn, n int64) (interface{}, error) { return nil, err } + if n == 3 { + // TODO: expose id in ClusterSlotInfo + _, err := readStringReply(cn) + if err != nil { + return nil, err + } + } + info.Addrs[i] = net.JoinHostPort(ip, strconv.FormatInt(port, 10)) }