-
Notifications
You must be signed in to change notification settings - Fork 5
/
writer.go
175 lines (153 loc) · 3.34 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package redisproto
import (
"bufio"
"fmt"
"io"
"strconv"
)
var (
star = []byte{'*'}
colon = []byte{':'}
dollar = []byte{'$'}
plus = []byte{'+'}
subs = []byte{'-'}
// newLine = []byte{'\r', '\n'}
// nilBulk = []byte{'$', '-', '1', '\r', '\n'}
// nilArray = []byte{'*', '-', '1', '\r', '\n'}
)
type Writer struct {
w io.Writer
}
func NewWriter(sink io.Writer) *Writer {
return &Writer{
w: sink,
}
}
func (w *Writer) Write(data []byte) (int, error) {
return w.w.Write(data)
}
func (w *Writer) Flush() error {
if f, ok := w.w.(*bufio.Writer); ok {
return f.Flush()
}
return nil
}
func (w *Writer) WriteInt(val int64) error {
w.Write(colon)
w.Write(strconv.AppendInt(nil,val,10))
_, err := w.Write(newLine)
return err
}
func (w *Writer) WriteBulk(val []byte) error {
if val == nil {
_, err := w.Write(nilBulk)
return err
}
w.Write(dollar)
w.Write(strconv.AppendUint(nil,uint64(len(val)),10))
w.Write(newLine)
w.Write(val)
_, err := w.Write(newLine)
return err
}
func (w *Writer) WriteBulkString(s string) error {
return w.WriteBulk([]byte(s))
}
func (w *Writer) WriteSimpleString(s string) error {
w.Write(plus)
w.Write([]byte(s))
_, err := w.Write(newLine)
return err
}
func (w *Writer) WriteError(s string) error {
w.Write(subs)
w.Write([]byte(s))
_, err := w.Write(newLine)
return err
}
func (w *Writer) WriteObjects(objs ...interface{}) error {
if objs == nil {
_, err := w.Write(nilArray)
return err
}
w.Write(star)
w.Write(strconv.AppendUint(nil,uint64(len(objs)),10))
w.Write(newLine)
numArg := len(objs)
for i := 0; i < numArg; i++ {
v := objs[i]
if v == nil {
if err := w.WriteBulk(nil); err != nil {
return err
}
continue
}
switch v := v.(type) {
case []byte:
if err := w.WriteBulk(v); err != nil {
return err
}
case string:
if err := w.WriteBulkString(v); err != nil {
return err
}
case int:
if err := w.WriteInt(int64(v)); err != nil {
return err
}
case int32:
if err := w.WriteInt(int64(v)); err != nil {
return err
}
case int64:
if err := w.WriteInt(v); err != nil {
return err
}
default:
return fmt.Errorf("value not suppport %v", v)
}
}
return nil
}
func (w *Writer) WriteBulks(bulks ...[]byte) error {
if bulks == nil {
_, err := w.Write(nilArray)
return err
}
w.Write(star)
numElement := len(bulks)
w.Write(strconv.AppendUint(nil,uint64(numElement),10))
w.Write(newLine)
for i := 0; i < numElement; i++ {
if err := w.WriteBulk(bulks[i]); err != nil {
return err
}
}
return nil
}
// WriteObjectsSlice works like WriteObjects, it useful when args is a slice that can be nil,
// in that case WriteObjects(nil) will understand as response 1 element array (nil element)
// see https://github.com/secmask/go-redisproto/issues/4 for details.
func (w *Writer) WriteObjectsSlice(args []interface{}) error {
return w.WriteObjects(args...)
}
// WriteBulksSlice ...
func (w *Writer) WriteBulksSlice(args [][]byte) error {
return w.WriteBulks(args...)
}
func (w *Writer) WriteBulkStrings(bulks []string) error {
if bulks == nil {
_, err := w.Write(nilArray)
return err
}
w.Write(star)
numElement := len(bulks)
w.Write(strconv.AppendUint(nil,uint64(numElement),10))
w.Write(newLine)
for i := 0; i < numElement; i++ {
if err := w.WriteBulkString(bulks[i]); err != nil {
return err
}
}
return nil
}