Debian Codegraph @

redistest/resp/encode.go

raw
1 package resp
2
3 import (
4 "errors"
5 "io"
6 "strconv"
7 )
8
9 var (
10 // Common encoding values optimized to avoid allocations.
11 pong = []byte("+PONG\r\n")
12 ok = []byte("+OK\r\n")
13 t = []byte(":1\r\n")
14 f = []byte(":0\r\n")
15 one = t
16 zero = f
17 )
18
19 // ErrInvalidValue is returned if the value to encode is invalid.
20 var ErrInvalidValue = errors.New("resp: invalid value")
21
22 // Error represents an error string as defined by the RESP. It cannot
23 // contain \r or \n characters. It must be used as a type conversion
24 // so that Encode serializes the string as an Error.
25 type Error string
26
27 // Pong is a sentinel type used to indicate that the PONG simple string
28 // value should be encoded.
29 type Pong struct{}
30
31 // OK is a sentinel type used to indicate that the OK simple string
32 // value should be encoded.
33 type OK struct{}
34
35 // SimpleString represents a simple string as defined by the RESP. It
36 // cannot contain \r or \n characters. It must be used as a type conversion
37 // so that Encode serializes the string as a SimpleString.
38 type SimpleString string
39
40 // BulkString represents a binary-safe string as defined by the RESP.
41 // It can be used as a type conversion so that Encode serializes the string
42 // as a BulkString, but this is the default encoding for a normal Go string.
43 type BulkString string
44
45 // Encode encode the value v and writes the serialized data to w.
46 func Encode(w io.Writer, v interface{}) error {
47 return encodeValue(w, v)
48 }
49
50 // encodeValue encodes the value v and writes the serialized data to w.
51 func encodeValue(w io.Writer, v interface{}) error {
52 switch v := v.(type) {
53 case OK:
54 _, err := w.Write(ok)
55 return err
56 case Pong:
57 _, err := w.Write(pong)
58 return err
59 case bool:
60 if v {
61 _, err := w.Write(t)
62 return err
63 }
64 _, err := w.Write(f)
65 return err
66 case SimpleString:
67 return encodeSimpleString(w, v)
68 case Error:
69 return encodeError(w, v)
70 case int64:
71 switch v {
72 case 0:
73 _, err := w.Write(zero)
74 return err
75 case 1:
76 _, err := w.Write(one)
77 return err
78 default:
79 return encodeInteger(w, v)
80 }
81 case string:
82 return encodeBulkString(w, BulkString(v))
83 case BulkString:
84 return encodeBulkString(w, v)
85 case []string:
86 return encodeStringArray(w, v)
87 case []interface{}:
88 return encodeArray(w, Array(v))
89 case Array:
90 return encodeArray(w, v)
91 case nil:
92 return encodeNil(w)
93 default:
94 return ErrInvalidValue
95 }
96 }
97
98 // encodeStringArray is a specialized array encoding func to avoid having to
99 // allocate an empty slice interface and copy values to it to use encodeArray.
100 func encodeStringArray(w io.Writer, v []string) error {
101 // Special case for a nil array
102 if v == nil {
103 err := encodePrefixed(w, '*', "-1")
104 return err
105 }
106
107 // First encode the number of elements
108 n := len(v)
109 err := encodePrefixed(w, '*', strconv.Itoa(n))
110 if err != nil {
111 return err
112 }
113
114 // Then encode each value
115 for _, el := range v {
116 err = encodeBulkString(w, BulkString(el))
117 if err != nil {
118 return err
119 }
120 }
121 return nil
122 }
123
124 // encodeArray encodes an array value to w.
125 func encodeArray(w io.Writer, v Array) error {
126 // Special case for a nil array
127 if v == nil {
128 err := encodePrefixed(w, '*', "-1")
129 return err
130 }
131
132 // First encode the number of elements
133 n := len(v)
134 err := encodePrefixed(w, '*', strconv.Itoa(n))
135 if err != nil {
136 return err
137 }
138
139 // Then encode each value
140 for _, el := range v {
141 err = encodeValue(w, el)
142 if err != nil {
143 return err
144 }
145 }
146 return nil
147 }
148
149 // encodeBulkString encodes a bulk string to w.
150 func encodeBulkString(w io.Writer, v BulkString) error {
151 n := len(v)
152 data := strconv.Itoa(n) + "\r\n" + string(v)
153 return encodePrefixed(w, '$', data)
154 }
155
156 // encodeInteger encodes an integer value to w.
157 func encodeInteger(w io.Writer, v int64) error {
158 return encodePrefixed(w, ':', strconv.FormatInt(v, 10))
159 }
160
161 // encodeSimpleString encodes a simple string value to w.
162 func encodeSimpleString(w io.Writer, v SimpleString) error {
163 return encodePrefixed(w, '+', string(v))
164 }
165
166 // encodeError encodes an error value to w.
167 func encodeError(w io.Writer, v Error) error {
168 return encodePrefixed(w, '-', string(v))
169 }
170
171 // encodeNil encodes a nil value as a nil bulk string.
172 func encodeNil(w io.Writer) error {
173 return encodePrefixed(w, '$', "-1")
174 }
175
176 // encodePrefixed encodes the data v to w, with the specified prefix.
177 func encodePrefixed(w io.Writer, prefix byte, v string) error {
178 buf := make([]byte, len(v)+3)
179 buf[0] = prefix
180 copy(buf[1:], v)
181 copy(buf[len(buf)-2:], "\r\n")
182 _, err := w.Write(buf)
183 return err
184 }