ansible: update docker userguide for collectd_ves
[barometer.git] / src / dma / vendor / github.com / go-redis / redis / internal / proto / write_buffer.go
1 package proto
2
3 import (
4         "encoding"
5         "fmt"
6         "strconv"
7 )
8
9 type WriteBuffer struct {
10         b []byte
11 }
12
13 func NewWriteBuffer() *WriteBuffer {
14         return &WriteBuffer{
15                 b: make([]byte, 0, 4096),
16         }
17 }
18
19 func (w *WriteBuffer) Len() int      { return len(w.b) }
20 func (w *WriteBuffer) Bytes() []byte { return w.b }
21 func (w *WriteBuffer) Reset()        { w.b = w.b[:0] }
22
23 func (w *WriteBuffer) Append(args []interface{}) error {
24         w.b = append(w.b, ArrayReply)
25         w.b = strconv.AppendUint(w.b, uint64(len(args)), 10)
26         w.b = append(w.b, '\r', '\n')
27
28         for _, arg := range args {
29                 if err := w.append(arg); err != nil {
30                         return err
31                 }
32         }
33         return nil
34 }
35
36 func (w *WriteBuffer) append(val interface{}) error {
37         switch v := val.(type) {
38         case nil:
39                 w.AppendString("")
40         case string:
41                 w.AppendString(v)
42         case []byte:
43                 w.AppendBytes(v)
44         case int:
45                 w.AppendString(formatInt(int64(v)))
46         case int8:
47                 w.AppendString(formatInt(int64(v)))
48         case int16:
49                 w.AppendString(formatInt(int64(v)))
50         case int32:
51                 w.AppendString(formatInt(int64(v)))
52         case int64:
53                 w.AppendString(formatInt(v))
54         case uint:
55                 w.AppendString(formatUint(uint64(v)))
56         case uint8:
57                 w.AppendString(formatUint(uint64(v)))
58         case uint16:
59                 w.AppendString(formatUint(uint64(v)))
60         case uint32:
61                 w.AppendString(formatUint(uint64(v)))
62         case uint64:
63                 w.AppendString(formatUint(v))
64         case float32:
65                 w.AppendString(formatFloat(float64(v)))
66         case float64:
67                 w.AppendString(formatFloat(v))
68         case bool:
69                 if v {
70                         w.AppendString("1")
71                 } else {
72                         w.AppendString("0")
73                 }
74         case encoding.BinaryMarshaler:
75                 b, err := v.MarshalBinary()
76                 if err != nil {
77                         return err
78                 }
79                 w.AppendBytes(b)
80         default:
81                 return fmt.Errorf(
82                         "redis: can't marshal %T (consider implementing encoding.BinaryMarshaler)", val)
83         }
84         return nil
85 }
86
87 func (w *WriteBuffer) AppendString(s string) {
88         w.b = append(w.b, StringReply)
89         w.b = strconv.AppendUint(w.b, uint64(len(s)), 10)
90         w.b = append(w.b, '\r', '\n')
91         w.b = append(w.b, s...)
92         w.b = append(w.b, '\r', '\n')
93 }
94
95 func (w *WriteBuffer) AppendBytes(p []byte) {
96         w.b = append(w.b, StringReply)
97         w.b = strconv.AppendUint(w.b, uint64(len(p)), 10)
98         w.b = append(w.b, '\r', '\n')
99         w.b = append(w.b, p...)
100         w.b = append(w.b, '\r', '\n')
101 }
102
103 func formatInt(n int64) string {
104         return strconv.FormatInt(n, 10)
105 }
106
107 func formatUint(u uint64) string {
108         return strconv.FormatUint(u, 10)
109 }
110
111 func formatFloat(f float64) string {
112         return strconv.FormatFloat(f, 'f', -1, 64)
113 }