forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
119 lines (108 loc) · 3.74 KB
/
response.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
package resp
import (
"io"
)
// CustomResponse values implement custom serialization and can be passed
// to ResponseWriter.Append.
type CustomResponse interface {
// AppendTo must be implemented by custom response types
AppendTo(w ResponseWriter)
}
// ResponseWriter is used by servers to wrap a client connection and send
// protocol-compatible responses in buffered pipelines.
type ResponseWriter interface {
io.WriteCloser
// AppendArrayLen appends an array header to the output buffer.
AppendArrayLen(n int)
// AppendBulk appends bulk bytes to the output buffer.
AppendBulk(p []byte)
// AppendBulkString appends a bulk string to the output buffer.
AppendBulkString(s string)
// AppendInline appends inline bytes to the output buffer.
AppendInline(p []byte)
// AppendInlineString appends an inline string to the output buffer.
AppendInlineString(s string)
// AppendError appends an error message to the output buffer.
AppendError(msg string)
// AppendErrorf appends an error message to the output buffer.
AppendErrorf(pattern string, args ...interface{})
// AppendInt appends a numeric response to the output buffer.
AppendInt(n int64)
// AppendNil appends a nil-value to the output buffer.
AppendNil()
// AppendOK appends "OK" to the output buffer.
AppendOK()
// Append automatically serialized given values and appends them to the output buffer.
// Supported values include:
// * nil
// * error
// * string
// * []byte
// * bool
// * float32, float64
// * int, int8, int16, int32, int64
// * uint, uint8, uint16, uint32, uint64
// * CustomResponse instances
// * slices and maps of any of the above
Append(v interface{}) error
// CopyBulk copies n bytes from a reader.
// This call may flush pending buffer to prevent overflows.
CopyBulk(src io.Reader, n int64) error
// Buffered returns the number of pending bytes.
Buffered() int
// Flush flushes pending buffer.
Flush() error
// Reset resets the writer to a new writer and recycles internal buffers.
Reset(w io.Writer)
}
// NewResponseWriter wraps any writer interface, but
// normally a net.Conn.
func NewResponseWriter(wr io.Writer) ResponseWriter {
w := new(bufioW)
w.reset(mkStdBuffer(), wr)
return w
}
// --------------------------------------------------------------------
// ResponseParser is a basic response parser
type ResponseParser interface {
// PeekType returns the type of the next response block
PeekType() (ResponseType, error)
// ReadNil reads a nil value
ReadNil() error
// ReadBulkString reads a bulk and returns a string
ReadBulkString() (string, error)
// ReadBulk reads a bulk and returns bytes (optionally appending to a passed p buffer)
ReadBulk(p []byte) ([]byte, error)
// StreamBulk parses a bulk responses and returns a streaming reader object
// Returned responses must be closed.
StreamBulk() (AllReadCloser, error)
// SkipBulk skip a bulk
SkipBulk() error
// ReadInt reads an int value
ReadInt() (int64, error)
// ReadArrayLen reads the array length
ReadArrayLen() (int, error)
// ReadError reads an error string
ReadError() (string, error)
// ReadInlineString reads a status string
ReadInlineString() (string, error)
// Scan scans results into the given values.
Scan(vv ...interface{}) error
}
// ResponseReader is used by clients to wrap a server connection and
// parse responses.
type ResponseReader interface {
io.Closer
ResponseParser
// Buffered returns the number of buffered (unread) bytes.
Buffered() int
// Reset resets the reader to a new reader and recycles internal buffers.
Reset(r io.Reader)
}
// NewResponseReader returns ResponseReader, which wraps any reader interface, but
// normally a net.Conn.
func NewResponseReader(rd io.Reader) ResponseReader {
r := new(bufioR)
r.reset(mkStdBuffer(), rd)
return r
}