1
0
mirror of https://github.com/redis/go-redis.git synced 2025-06-14 01:21:30 +03:00

Add support for scripting commands.

This commit is contained in:
Vladimir Mihailenco
2012-08-20 13:42:33 +03:00
parent 303687e438
commit 9ad848d04b
6 changed files with 231 additions and 33 deletions

View File

@ -57,15 +57,25 @@ func readLine(rd reader) ([]byte, error) {
//------------------------------------------------------------------------------
const (
ifaceSlice = iota
stringSlice
boolSlice
)
func parseReply(rd reader) (interface{}, error) {
return _parseReply(rd, false)
return _parseReply(rd, ifaceSlice)
}
func parseIfaceSliceReply(rd reader) (interface{}, error) {
return _parseReply(rd, true)
func parseStringSliceReply(rd reader) (interface{}, error) {
return _parseReply(rd, stringSlice)
}
func _parseReply(rd reader, useIfaceSlice bool) (interface{}, error) {
func parseBoolSliceReply(rd reader) (interface{}, error) {
return _parseReply(rd, boolSlice)
}
func _parseReply(rd reader, multiBulkType int) (interface{}, error) {
line, err := readLine(rd)
if err != nil {
return 0, err
@ -113,7 +123,42 @@ func _parseReply(rd reader, useIfaceSlice bool) (interface{}, error) {
return nil, Nil
}
if useIfaceSlice {
switch multiBulkType {
case stringSlice:
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
}
vals := make([]string, 0, numReplies)
for i := int64(0); i < numReplies; i++ {
v, err := parseReply(rd)
if err != nil {
return nil, err
} else {
vals = append(vals, v.(string))
}
}
return vals, nil
case boolSlice:
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
}
vals := make([]bool, 0, numReplies)
for i := int64(0); i < numReplies; i++ {
v, err := parseReply(rd)
if err != nil {
return nil, err
} else {
vals = append(vals, v.(int64) == 1)
}
}
return vals, nil
default:
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
@ -131,23 +176,6 @@ func _parseReply(rd reader, useIfaceSlice bool) (interface{}, error) {
}
}
return vals, nil
} else {
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
}
vals := make([]string, 0, numReplies)
for i := int64(0); i < numReplies; i++ {
v, err := parseReply(rd)
if err != nil {
return nil, err
} else {
vals = append(vals, v.(string))
}
}
return vals, nil
}
default: