-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
resp.go
99 lines (81 loc) · 1.59 KB
/
resp.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package redis
import "fmt"
type RespType byte
const (
TypeString RespType = '+'
TypeError RespType = '-'
TypeInt RespType = ':'
TypeBulkBytes RespType = '$'
TypeArray RespType = '*'
)
func (t RespType) String() string {
switch t {
case TypeString:
return "<string>"
case TypeError:
return "<error>"
case TypeInt:
return "<int>"
case TypeBulkBytes:
return "<bulkbytes>"
case TypeArray:
return "<array>"
default:
return fmt.Sprintf("<unknown-0x%02x>", byte(t))
}
}
type Resp struct {
Type RespType
Value []byte
Array []*Resp
}
func (r *Resp) IsString() bool {
return r.Type == TypeString
}
func (r *Resp) IsError() bool {
return r.Type == TypeError
}
func (r *Resp) IsInt() bool {
return r.Type == TypeInt
}
func (r *Resp) IsBulkBytes() bool {
return r.Type == TypeBulkBytes
}
func (r *Resp) IsArray() bool {
return r.Type == TypeArray
}
func NewString(value []byte) *Resp {
r := &Resp{}
r.Type = TypeString
r.Value = value
return r
}
func NewError(value []byte) *Resp {
r := &Resp{}
r.Type = TypeError
r.Value = value
return r
}
func NewErrorf(format string, args ...interface{}) *Resp {
return NewError([]byte(fmt.Sprintf(format, args...)))
}
func NewInt(value []byte) *Resp {
r := &Resp{}
r.Type = TypeInt
r.Value = value
return r
}
func NewBulkBytes(value []byte) *Resp {
r := &Resp{}
r.Type = TypeBulkBytes
r.Value = value
return r
}
func NewArray(array []*Resp) *Resp {
r := &Resp{}
r.Type = TypeArray
r.Array = array
return r
}