-
Notifications
You must be signed in to change notification settings - Fork 5
/
fmt.go
153 lines (145 loc) · 2.73 KB
/
fmt.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
package redisproto
import (
"bufio"
"log"
"strconv"
)
var (
newLine = []byte{'\r', '\n'}
nilBulk = []byte{'$', '-', '1', '\r', '\n'}
nilArray = []byte{'*', '-', '1', '\r', '\n'}
)
func intToString(val int64) string {
return strconv.FormatInt(val, 10)
}
func SendError(w *bufio.Writer, msg string) error {
resp := "-" + msg + "\r\n"
_, e := w.Write([]byte(resp))
if e != nil {
return e
}
return w.Flush()
}
func SendString(w *bufio.Writer, msg string) error {
resp := "+" + msg + "\r\n"
_, e := w.Write([]byte(resp))
if e != nil {
return e
}
return w.Flush()
}
func sendInt(w *bufio.Writer, val int64) error {
resp := ":" + intToString(val) + "\r\n"
_, e := w.Write([]byte(resp))
if e != nil {
return e
}
return nil
}
func SendInt(w *bufio.Writer, val int64) error {
e := sendInt(w, val)
if e != nil {
return e
}
return w.Flush()
}
func SendBulk(w *bufio.Writer, val []byte) error {
if e := sendBulk(w, val); e != nil {
return e
}
return w.Flush()
}
func sendBulk(w *bufio.Writer, val []byte) error {
if val == nil {
_, e := w.Write(nilBulk)
if e != nil {
return e
}
return nil
}
pre := "$" + intToString(int64(len(val))) + "\r\n"
_, e := w.Write([]byte(pre))
if e != nil {
return e
}
_, e = w.Write(val)
if e != nil {
return e
}
_, e = w.Write(newLine)
if e != nil {
return e
}
return nil
}
func SendBulks(w *bufio.Writer, vals [][]byte) error {
if e := sendBulks(w, vals); e != nil {
return e
}
return w.Flush()
}
func sendBulks(w *bufio.Writer, vals [][]byte) error {
var e error
if vals == nil {
_, e = w.Write(nilArray)
e = w.Flush()
return e
}
pre := "*" + intToString(int64(len(vals))) + "\r\n"
_, e = w.Write([]byte(pre))
if e != nil {
return e
}
numArg := len(vals)
for i := 0; i < numArg; i++ {
if e = SendBulk(w, vals[i]); e != nil {
return e
}
}
e = w.Flush()
return e
}
func SendObjects(w *bufio.Writer, vals []interface{}) error {
var e error
if vals == nil {
_, e = w.Write(nilArray)
e = w.Flush()
return e
}
pre := "*" + intToString(int64(len(vals))) + "\r\n"
_, e = w.Write([]byte(pre))
if e != nil {
return e
}
numArg := len(vals)
for i := 0; i < numArg; i++ {
v := vals[i]
switch v := v.(type) {
case int64:
if e = sendInt(w, v); e != nil {
return e
}
case []byte:
if e = sendBulk(w, v); e != nil {
return e
}
default:
log.Println("unsupport value ", v)
}
}
e = w.Flush()
return e
}
func SendBulkString(w *bufio.Writer, str string) error {
return SendBulk(w, []byte(str))
}
func SendBulkStrings(w *bufio.Writer, strs []string) error {
if strs == nil {
return SendBulks(w, nil)
}
t := make([][]byte, 0, len(strs))
for i := 0; i < len(strs); i++ {
t = append(t, []byte(strs[i]))
}
return SendBulks(w, t)
}