writer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package proto
  2. import (
  3. "encoding"
  4. "fmt"
  5. "io"
  6. "strconv"
  7. "time"
  8. "github.com/go-redis/redis/v8/internal/util"
  9. )
  10. type writer interface {
  11. io.Writer
  12. io.ByteWriter
  13. // io.StringWriter
  14. WriteString(s string) (n int, err error)
  15. }
  16. type Writer struct {
  17. writer
  18. lenBuf []byte
  19. numBuf []byte
  20. }
  21. func NewWriter(wr writer) *Writer {
  22. return &Writer{
  23. writer: wr,
  24. lenBuf: make([]byte, 64),
  25. numBuf: make([]byte, 64),
  26. }
  27. }
  28. func (w *Writer) WriteArgs(args []interface{}) error {
  29. if err := w.WriteByte(ArrayReply); err != nil {
  30. return err
  31. }
  32. if err := w.writeLen(len(args)); err != nil {
  33. return err
  34. }
  35. for _, arg := range args {
  36. if err := w.WriteArg(arg); err != nil {
  37. return err
  38. }
  39. }
  40. return nil
  41. }
  42. func (w *Writer) writeLen(n int) error {
  43. w.lenBuf = strconv.AppendUint(w.lenBuf[:0], uint64(n), 10)
  44. w.lenBuf = append(w.lenBuf, '\r', '\n')
  45. _, err := w.Write(w.lenBuf)
  46. return err
  47. }
  48. func (w *Writer) WriteArg(v interface{}) error {
  49. switch v := v.(type) {
  50. case nil:
  51. return w.string("")
  52. case string:
  53. return w.string(v)
  54. case []byte:
  55. return w.bytes(v)
  56. case int:
  57. return w.int(int64(v))
  58. case int8:
  59. return w.int(int64(v))
  60. case int16:
  61. return w.int(int64(v))
  62. case int32:
  63. return w.int(int64(v))
  64. case int64:
  65. return w.int(v)
  66. case uint:
  67. return w.uint(uint64(v))
  68. case uint8:
  69. return w.uint(uint64(v))
  70. case uint16:
  71. return w.uint(uint64(v))
  72. case uint32:
  73. return w.uint(uint64(v))
  74. case uint64:
  75. return w.uint(v)
  76. case float32:
  77. return w.float(float64(v))
  78. case float64:
  79. return w.float(v)
  80. case bool:
  81. if v {
  82. return w.int(1)
  83. }
  84. return w.int(0)
  85. case time.Time:
  86. w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano)
  87. return w.bytes(w.numBuf)
  88. case time.Duration:
  89. return w.int(v.Nanoseconds())
  90. case encoding.BinaryMarshaler:
  91. b, err := v.MarshalBinary()
  92. if err != nil {
  93. return err
  94. }
  95. return w.bytes(b)
  96. default:
  97. return fmt.Errorf(
  98. "redis: can't marshal %T (implement encoding.BinaryMarshaler)", v)
  99. }
  100. }
  101. func (w *Writer) bytes(b []byte) error {
  102. if err := w.WriteByte(StringReply); err != nil {
  103. return err
  104. }
  105. if err := w.writeLen(len(b)); err != nil {
  106. return err
  107. }
  108. if _, err := w.Write(b); err != nil {
  109. return err
  110. }
  111. return w.crlf()
  112. }
  113. func (w *Writer) string(s string) error {
  114. return w.bytes(util.StringToBytes(s))
  115. }
  116. func (w *Writer) uint(n uint64) error {
  117. w.numBuf = strconv.AppendUint(w.numBuf[:0], n, 10)
  118. return w.bytes(w.numBuf)
  119. }
  120. func (w *Writer) int(n int64) error {
  121. w.numBuf = strconv.AppendInt(w.numBuf[:0], n, 10)
  122. return w.bytes(w.numBuf)
  123. }
  124. func (w *Writer) float(f float64) error {
  125. w.numBuf = strconv.AppendFloat(w.numBuf[:0], f, 'f', -1, 64)
  126. return w.bytes(w.numBuf)
  127. }
  128. func (w *Writer) crlf() error {
  129. if err := w.WriteByte('\r'); err != nil {
  130. return err
  131. }
  132. return w.WriteByte('\n')
  133. }