forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/internal/proto/writer.go

322 lines
3.5 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package proto
import (
"encoding"
"fmt"
"io"
"net"
"strconv"
"time"
"github.com/redis/go-redis/v9/internal/util"
)
type writer interface {
io.Writer
2024-02-18 10:42:21 +00:00
io.ByteWriter
2024-02-18 10:42:21 +00:00
// WriteString implement io.StringWriter.
2024-02-18 10:42:21 +00:00
WriteString(s string) (n int, err error)
}
type Writer struct {
writer
lenBuf []byte
2024-02-18 10:42:21 +00:00
numBuf []byte
}
func NewWriter(wr writer) *Writer {
2024-02-18 10:42:21 +00:00
return &Writer{
2024-02-18 10:42:21 +00:00
writer: wr,
lenBuf: make([]byte, 64),
2024-02-18 10:42:21 +00:00
numBuf: make([]byte, 64),
}
2024-02-18 10:42:21 +00:00
}
func (w *Writer) WriteArgs(args []interface{}) error {
2024-02-18 10:42:21 +00:00
if err := w.WriteByte(RespArray); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if err := w.writeLen(len(args)); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
for _, arg := range args {
2024-02-18 10:42:21 +00:00
if err := w.WriteArg(arg); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
func (w *Writer) writeLen(n int) error {
2024-02-18 10:42:21 +00:00
w.lenBuf = strconv.AppendUint(w.lenBuf[:0], uint64(n), 10)
2024-02-18 10:42:21 +00:00
w.lenBuf = append(w.lenBuf, '\r', '\n')
2024-02-18 10:42:21 +00:00
_, err := w.Write(w.lenBuf)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func (w *Writer) WriteArg(v interface{}) error {
2024-02-18 10:42:21 +00:00
switch v := v.(type) {
2024-02-18 10:42:21 +00:00
case nil:
2024-02-18 10:42:21 +00:00
return w.string("")
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
return w.string(v)
2024-02-18 10:42:21 +00:00
case *string:
2024-02-18 10:42:21 +00:00
return w.string(*v)
2024-02-18 10:42:21 +00:00
case []byte:
2024-02-18 10:42:21 +00:00
return w.bytes(v)
2024-02-18 10:42:21 +00:00
case int:
2024-02-18 10:42:21 +00:00
return w.int(int64(v))
2024-02-18 10:42:21 +00:00
case *int:
2024-02-18 10:42:21 +00:00
return w.int(int64(*v))
2024-02-18 10:42:21 +00:00
case int8:
2024-02-18 10:42:21 +00:00
return w.int(int64(v))
2024-02-18 10:42:21 +00:00
case *int8:
2024-02-18 10:42:21 +00:00
return w.int(int64(*v))
2024-02-18 10:42:21 +00:00
case int16:
2024-02-18 10:42:21 +00:00
return w.int(int64(v))
2024-02-18 10:42:21 +00:00
case *int16:
2024-02-18 10:42:21 +00:00
return w.int(int64(*v))
2024-02-18 10:42:21 +00:00
case int32:
2024-02-18 10:42:21 +00:00
return w.int(int64(v))
2024-02-18 10:42:21 +00:00
case *int32:
2024-02-18 10:42:21 +00:00
return w.int(int64(*v))
2024-02-18 10:42:21 +00:00
case int64:
2024-02-18 10:42:21 +00:00
return w.int(v)
2024-02-18 10:42:21 +00:00
case *int64:
2024-02-18 10:42:21 +00:00
return w.int(*v)
2024-02-18 10:42:21 +00:00
case uint:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(v))
2024-02-18 10:42:21 +00:00
case *uint:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(*v))
2024-02-18 10:42:21 +00:00
case uint8:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(v))
2024-02-18 10:42:21 +00:00
case *uint8:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(*v))
2024-02-18 10:42:21 +00:00
case uint16:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(v))
2024-02-18 10:42:21 +00:00
case *uint16:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(*v))
2024-02-18 10:42:21 +00:00
case uint32:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(v))
2024-02-18 10:42:21 +00:00
case *uint32:
2024-02-18 10:42:21 +00:00
return w.uint(uint64(*v))
2024-02-18 10:42:21 +00:00
case uint64:
2024-02-18 10:42:21 +00:00
return w.uint(v)
2024-02-18 10:42:21 +00:00
case *uint64:
2024-02-18 10:42:21 +00:00
return w.uint(*v)
2024-02-18 10:42:21 +00:00
case float32:
2024-02-18 10:42:21 +00:00
return w.float(float64(v))
2024-02-18 10:42:21 +00:00
case *float32:
2024-02-18 10:42:21 +00:00
return w.float(float64(*v))
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
return w.float(v)
2024-02-18 10:42:21 +00:00
case *float64:
2024-02-18 10:42:21 +00:00
return w.float(*v)
2024-02-18 10:42:21 +00:00
case bool:
2024-02-18 10:42:21 +00:00
if v {
2024-02-18 10:42:21 +00:00
return w.int(1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return w.int(0)
2024-02-18 10:42:21 +00:00
case *bool:
2024-02-18 10:42:21 +00:00
if *v {
2024-02-18 10:42:21 +00:00
return w.int(1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return w.int(0)
2024-02-18 10:42:21 +00:00
case time.Time:
2024-02-18 10:42:21 +00:00
w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano)
2024-02-18 10:42:21 +00:00
return w.bytes(w.numBuf)
2024-02-18 10:42:21 +00:00
case time.Duration:
2024-02-18 10:42:21 +00:00
return w.int(v.Nanoseconds())
2024-02-18 10:42:21 +00:00
case encoding.BinaryMarshaler:
2024-02-18 10:42:21 +00:00
b, err := v.MarshalBinary()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return w.bytes(b)
2024-02-18 10:42:21 +00:00
case net.IP:
2024-02-18 10:42:21 +00:00
return w.bytes(v)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return fmt.Errorf(
2024-02-18 10:42:21 +00:00
"redis: can't marshal %T (implement encoding.BinaryMarshaler)", v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (w *Writer) bytes(b []byte) error {
2024-02-18 10:42:21 +00:00
if err := w.WriteByte(RespString); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if err := w.writeLen(len(b)); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if _, err := w.Write(b); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
return w.crlf()
2024-02-18 10:42:21 +00:00
}
func (w *Writer) string(s string) error {
2024-02-18 10:42:21 +00:00
return w.bytes(util.StringToBytes(s))
2024-02-18 10:42:21 +00:00
}
func (w *Writer) uint(n uint64) error {
2024-02-18 10:42:21 +00:00
w.numBuf = strconv.AppendUint(w.numBuf[:0], n, 10)
2024-02-18 10:42:21 +00:00
return w.bytes(w.numBuf)
2024-02-18 10:42:21 +00:00
}
func (w *Writer) int(n int64) error {
2024-02-18 10:42:21 +00:00
w.numBuf = strconv.AppendInt(w.numBuf[:0], n, 10)
2024-02-18 10:42:21 +00:00
return w.bytes(w.numBuf)
2024-02-18 10:42:21 +00:00
}
func (w *Writer) float(f float64) error {
2024-02-18 10:42:21 +00:00
w.numBuf = strconv.AppendFloat(w.numBuf[:0], f, 'f', -1, 64)
2024-02-18 10:42:21 +00:00
return w.bytes(w.numBuf)
2024-02-18 10:42:21 +00:00
}
func (w *Writer) crlf() error {
2024-02-18 10:42:21 +00:00
if err := w.WriteByte('\r'); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return w.WriteByte('\n')
2024-02-18 10:42:21 +00:00
}