Use int-bulk-strings for *Any calls

This commit is contained in:
tidwall 2020-03-06 09:32:11 -07:00
parent a389b6ca04
commit 1f5e70c2d8
2 changed files with 37 additions and 37 deletions

View File

@ -317,42 +317,28 @@ func AppendNull(b []byte) []byte {
} }
// AppendBulkFloat appends a float64, as bulk bytes. // AppendBulkFloat appends a float64, as bulk bytes.
func AppendBulkFloat(b []byte, f float64) []byte { func AppendBulkFloat(dst []byte, f float64) []byte {
mark1 := len(b) return AppendBulk(dst, strconv.AppendFloat(nil, f, 'f', -1, 64))
b = strconv.AppendFloat(b, f, 'f', -1, 64)
mark2 := len(b)
b = AppendBulk(b, b[mark1:mark2])
mark3 := len(b)
copy(b[mark1:], b[mark2:])
b = b[:mark1+(mark3-mark2)]
return b
} }
// AppendBulkInt appends an int64, as bulk bytes. // AppendBulkInt appends an int64, as bulk bytes.
func AppendBulkInt(b []byte, x int64) []byte { func AppendBulkInt(dst []byte, x int64) []byte {
mark1 := len(b) return AppendBulk(dst, strconv.AppendInt(nil, x, 10))
b = strconv.AppendInt(b, x, 10)
mark2 := len(b)
b = AppendBulk(b, b[mark1:mark2])
mark3 := len(b)
copy(b[mark1:], b[mark2:])
b = b[:mark1+(mark3-mark2)]
return b
} }
// AppendBulkUint appends an uint64, as bulk bytes. // AppendBulkUint appends an uint64, as bulk bytes.
func AppendBulkUint(b []byte, x uint64) []byte { func AppendBulkUint(dst []byte, x uint64) []byte {
mark1 := len(b) return AppendBulk(dst, strconv.AppendUint(nil, x, 10))
b = strconv.AppendUint(b, x, 10)
mark2 := len(b)
b = AppendBulk(b, b[mark1:mark2])
mark3 := len(b)
copy(b[mark1:], b[mark2:])
b = b[:mark1+(mark3-mark2)]
return b
} }
// AppendAny appends any type to valid Redis type // AppendAny appends any type to valid Redis type
// string -> bulk-string
// numbers -> bulk-string
// []byte -> bulk-string
// bool -> number (0 or 1)
// slice -> array
// map -> array with key/value pairs
// everything-else -> bulk-string representation using fmt.Sprint()
func AppendAny(b []byte, v interface{}) []byte { func AppendAny(b []byte, v interface{}) []byte {
switch v := v.(type) { switch v := v.(type) {
case nil: case nil:
@ -370,25 +356,25 @@ func AppendAny(b []byte, v interface{}) []byte {
b = AppendInt(b, 0) b = AppendInt(b, 0)
} }
case int: case int:
b = AppendInt(b, int64(v)) b = AppendBulkInt(b, int64(v))
case int8: case int8:
b = AppendInt(b, int64(v)) b = AppendBulkInt(b, int64(v))
case int16: case int16:
b = AppendInt(b, int64(v)) b = AppendBulkInt(b, int64(v))
case int32: case int32:
b = AppendInt(b, int64(v)) b = AppendBulkInt(b, int64(v))
case int64: case int64:
b = AppendInt(b, int64(v)) b = AppendBulkInt(b, int64(v))
case uint: case uint:
b = AppendUint(b, uint64(v)) b = AppendBulkUint(b, uint64(v))
case uint8: case uint8:
b = AppendUint(b, uint64(v)) b = AppendBulkUint(b, uint64(v))
case uint16: case uint16:
b = AppendUint(b, uint64(v)) b = AppendBulkUint(b, uint64(v))
case uint32: case uint32:
b = AppendUint(b, uint64(v)) b = AppendBulkUint(b, uint64(v))
case uint64: case uint64:
b = AppendUint(b, uint64(v)) b = AppendBulkUint(b, uint64(v))
case float32: case float32:
b = AppendBulkFloat(b, float64(v)) b = AppendBulkFloat(b, float64(v))
case float64: case float64:

View File

@ -61,6 +61,13 @@ type Conn interface {
// WriteRaw writes raw data to the client. // WriteRaw writes raw data to the client.
WriteRaw(data []byte) WriteRaw(data []byte)
// WriteAny writes any type to the client. // WriteAny writes any type to the client.
// string -> bulk-string
// numbers -> bulk-string
// []byte -> bulk-string
// bool -> number (0 or 1)
// slice -> array
// map -> array with key/value pairs
// everything-else -> bulk-string representation using fmt.Sprint()
WriteAny(any interface{}) WriteAny(any interface{})
// Context returns a user-defined context // Context returns a user-defined context
Context() interface{} Context() interface{}
@ -637,6 +644,13 @@ func (w *Writer) WriteRaw(data []byte) {
} }
// WriteAny writes any type to client. // WriteAny writes any type to client.
// string -> bulk-string
// numbers -> bulk-string
// []byte -> bulk-string
// bool -> number (0 or 1)
// slice -> array
// map -> array with key/value pairs
// everything-else -> bulk-string representation using fmt.Sprint()
func (w *Writer) WriteAny(v interface{}) { func (w *Writer) WriteAny(v interface{}) {
w.b = AppendAny(w.b, v) w.b = AppendAny(w.b, v)
} }