-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
90 lines (75 loc) · 1.84 KB
/
message.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
package main
import "strconv"
type message struct {
Type byte
Str string
}
// 定义常量,分别表示字符串、错误、整数、块、数组和空值
var (
STRING = message{Type: '+', Str: "string"} // 字符串
ERROR = message{Type: '-', Str: "error"} // 错误
INTEGER = message{Type: ':', Str: "integer"} // 整数
BULK = message{Type: '$', Str: "bulk"} // 块
ARRAY = message{Type: '*', Str: "array"} // 数组
NULL = message{Type: '.', Str: "null"} // 空值
)
type Value struct {
Typ string
Str string
Num int
Bulk string
Array []Value
}
func (v Value) Marshal() []byte {
switch v.Typ {
case ARRAY.Str:
return v.marshalArray()
case BULK.Str:
return v.marshalBulk()
case STRING.Str:
return v.marshalString()
case NULL.Str:
return v.marshallNull()
case ERROR.Str:
return v.marshallError()
default:
return []byte{}
}
}
func (v Value) marshalString() []byte {
var bytes []byte
bytes = append(bytes, STRING.Type)
bytes = append(bytes, v.Str...)
bytes = append(bytes, '\r', '\n')
return bytes
}
func (v Value) marshalBulk() []byte {
var bytes []byte
bytes = append(bytes, BULK.Type)
bytes = append(bytes, strconv.Itoa(len(v.Bulk))...)
bytes = append(bytes, '\r', '\n')
bytes = append(bytes, v.Bulk...)
bytes = append(bytes, '\r', '\n')
return bytes
}
func (v Value) marshalArray() []byte {
len := len(v.Array)
var bytes []byte
bytes = append(bytes, ARRAY.Type)
bytes = append(bytes, strconv.Itoa(len)...)
bytes = append(bytes, '\r', '\n')
for i := 0; i < len; i++ {
bytes = append(bytes, v.Array[i].Marshal()...)
}
return bytes
}
func (v Value) marshallError() []byte {
var bytes []byte
bytes = append(bytes, ERROR.Type)
bytes = append(bytes, v.Str...)
bytes = append(bytes, '\r', '\n')
return bytes
}
func (v Value) marshallNull() []byte {
return []byte("$-1\r\n")
}