|
@@ -346,43 +346,55 @@ func (c *conn) writeFloat64(n float64) error {
|
|
|
return c.writeBytes(strconv.AppendFloat(c.numScratch[:0], n, 'g', -1, 64))
|
|
return c.writeBytes(strconv.AppendFloat(c.numScratch[:0], n, 'g', -1, 64))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *conn) writeCommand(cmd string, args []interface{}) (err error) {
|
|
|
|
|
|
|
+func (c *conn) writeCommand(cmd string, args []interface{}) error {
|
|
|
c.writeLen('*', 1+len(args))
|
|
c.writeLen('*', 1+len(args))
|
|
|
- err = c.writeString(cmd)
|
|
|
|
|
|
|
+ if err := c.writeString(cmd); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
for _, arg := range args {
|
|
for _, arg := range args {
|
|
|
- if err != nil {
|
|
|
|
|
- break
|
|
|
|
|
|
|
+ if err := c.writeArg(arg, true); err != nil {
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
- switch arg := arg.(type) {
|
|
|
|
|
- case string:
|
|
|
|
|
- err = c.writeString(arg)
|
|
|
|
|
- case []byte:
|
|
|
|
|
- err = c.writeBytes(arg)
|
|
|
|
|
- case int:
|
|
|
|
|
- err = c.writeInt64(int64(arg))
|
|
|
|
|
- case int64:
|
|
|
|
|
- err = c.writeInt64(arg)
|
|
|
|
|
- case float64:
|
|
|
|
|
- err = c.writeFloat64(arg)
|
|
|
|
|
- case bool:
|
|
|
|
|
- if arg {
|
|
|
|
|
- err = c.writeString("1")
|
|
|
|
|
- } else {
|
|
|
|
|
- err = c.writeString("0")
|
|
|
|
|
- }
|
|
|
|
|
- case nil:
|
|
|
|
|
- err = c.writeString("")
|
|
|
|
|
- case Argument:
|
|
|
|
|
- var buf bytes.Buffer
|
|
|
|
|
- fmt.Fprint(&buf, arg.RedisArg())
|
|
|
|
|
- err = c.writeBytes(buf.Bytes())
|
|
|
|
|
- default:
|
|
|
|
|
- var buf bytes.Buffer
|
|
|
|
|
- fmt.Fprint(&buf, arg)
|
|
|
|
|
- err = c.writeBytes(buf.Bytes())
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (c *conn) writeArg(arg interface{}, argumentTypeOK bool) (err error) {
|
|
|
|
|
+ switch arg := arg.(type) {
|
|
|
|
|
+ case string:
|
|
|
|
|
+ return c.writeString(arg)
|
|
|
|
|
+ case []byte:
|
|
|
|
|
+ return c.writeBytes(arg)
|
|
|
|
|
+ case int:
|
|
|
|
|
+ return c.writeInt64(int64(arg))
|
|
|
|
|
+ case int64:
|
|
|
|
|
+ return c.writeInt64(arg)
|
|
|
|
|
+ case float64:
|
|
|
|
|
+ return c.writeFloat64(arg)
|
|
|
|
|
+ case bool:
|
|
|
|
|
+ if arg {
|
|
|
|
|
+ return c.writeString("1")
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return c.writeString("0")
|
|
|
}
|
|
}
|
|
|
|
|
+ case nil:
|
|
|
|
|
+ return c.writeString("")
|
|
|
|
|
+ case Argument:
|
|
|
|
|
+ if argumentTypeOK {
|
|
|
|
|
+ return c.writeArg(arg.RedisArg(), false)
|
|
|
|
|
+ }
|
|
|
|
|
+ // See comment in default clause below.
|
|
|
|
|
+ var buf bytes.Buffer
|
|
|
|
|
+ fmt.Fprint(&buf, arg)
|
|
|
|
|
+ return c.writeBytes(buf.Bytes())
|
|
|
|
|
+ default:
|
|
|
|
|
+ // This default clause is intended to handle builtin numeric types.
|
|
|
|
|
+ // The function should return an error for other types, but this is not
|
|
|
|
|
+ // done for compatibility with previous versions of the package.
|
|
|
|
|
+ var buf bytes.Buffer
|
|
|
|
|
+ fmt.Fprint(&buf, arg)
|
|
|
|
|
+ return c.writeBytes(buf.Bytes())
|
|
|
}
|
|
}
|
|
|
- return err
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type protocolError string
|
|
type protocolError string
|