-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathformat.go
More file actions
137 lines (119 loc) · 2.77 KB
/
format.go
File metadata and controls
137 lines (119 loc) · 2.77 KB
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
package redis
import (
"bytes"
"fmt"
"reflect"
"strconv"
)
var delim []byte = []byte{'\r', '\n'}
type request struct {
cmd string
args []interface{}
}
// formatArg formats the given argument to a Redis-styled argument byte slice.
func formatArg(v interface{}) []byte {
var b, bs []byte
switch vt := v.(type) {
case []byte:
bs = vt
case string:
bs = []byte(vt)
case bool:
if vt {
bs = []byte{'1'}
} else {
bs = []byte{'0'}
}
case nil:
// empty byte slice
case int:
bs = []byte(strconv.Itoa(vt))
case int8:
bs = []byte(strconv.FormatInt(int64(vt), 10))
case int16:
bs = []byte(strconv.FormatInt(int64(vt), 10))
case int32:
bs = []byte(strconv.FormatInt(int64(vt), 10))
case int64:
bs = []byte(strconv.FormatInt(vt, 10))
case uint:
bs = []byte(strconv.FormatUint(uint64(vt), 10))
case uint8:
bs = []byte(strconv.FormatUint(uint64(vt), 10))
case uint16:
bs = []byte(strconv.FormatUint(uint64(vt), 10))
case uint32:
bs = []byte(strconv.FormatUint(uint64(vt), 10))
case uint64:
bs = []byte(strconv.FormatUint(vt, 10))
default:
// Fallback to reflect-based.
switch reflect.TypeOf(vt).Kind() {
case reflect.Slice:
rv := reflect.ValueOf(vt)
for i := 0; i < rv.Len(); i++ {
bs = append(bs, formatArg(rv.Index(i).Interface())...)
}
return bs
case reflect.Map:
rv := reflect.ValueOf(vt)
keys := rv.MapKeys()
for _, k := range keys {
bs = append(bs, formatArg(k.Interface())...)
bs = append(bs, formatArg(rv.MapIndex(k).Interface())...)
}
return bs
default:
var buf bytes.Buffer
fmt.Fprint(&buf, v)
bs = buf.Bytes()
}
}
b = append(b, '$')
b = append(b, []byte(strconv.Itoa(len(bs)))...)
b = append(b, delim...)
b = append(b, bs...)
b = append(b, delim...)
return b
}
var byteSliceType = reflect.TypeOf([]byte(nil))
// createRequest creates a request string from the given requests.
func createRequest(requests ...*request) []byte {
var total []byte
for _, req := range requests {
var s []byte
// Calculate number of arguments.
argsLen := 1
for _, arg := range req.args {
typ := reflect.TypeOf(arg)
switch typ.Kind() {
case reflect.Slice:
if typ == byteSliceType {
argsLen++
} else {
argsLen += reflect.ValueOf(arg).Len()
}
case reflect.Map:
argsLen += reflect.ValueOf(arg).Len() * 2
default:
argsLen++
}
}
// number of arguments
s = append(s, '*')
s = append(s, []byte(strconv.Itoa(argsLen))...)
s = append(s, delim...)
// command
s = append(s, '$')
s = append(s, []byte(strconv.Itoa(len(req.cmd)))...)
s = append(s, delim...)
s = append(s, []byte(req.cmd)...)
s = append(s, delim...)
// arguments
for _, arg := range req.args {
s = append(s, formatArg(arg)...)
}
total = append(total, s...)
}
return total
}