Cleanup appendArgs

This commit is contained in:
Vladimir Mihailenco 2020-06-29 17:48:57 +03:00
parent 52557f02b7
commit 8638895c66
1 changed files with 23 additions and 20 deletions

View File

@ -37,27 +37,33 @@ func formatSec(dur time.Duration) int64 {
func appendArgs(dst, src []interface{}) []interface{} { func appendArgs(dst, src []interface{}) []interface{} {
if len(src) == 1 { if len(src) == 1 {
switch v := src[0].(type) { return appendArg(dst, src[0])
case []string:
for _, s := range v {
dst = append(dst, s)
}
return dst
case []interface{}:
dst = append(dst, v...)
return dst
case map[string]interface{}:
for k, v := range v {
dst = append(dst, k, v)
}
return dst
}
} }
dst = append(dst, src...) dst = append(dst, src...)
return dst return dst
} }
func appendArg(dst []interface{}, arg interface{}) []interface{} {
switch arg := arg.(type) {
case []string:
for _, s := range arg {
dst = append(dst, s)
}
return dst
case []interface{}:
dst = append(dst, arg...)
return dst
case map[string]interface{}:
for k, v := range arg {
dst = append(dst, k, v)
}
return dst
default:
return append(dst, arg)
}
}
type Cmdable interface { type Cmdable interface {
Pipeline() Pipeliner Pipeline() Pipeliner
Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error)
@ -1405,10 +1411,7 @@ type XAddArgs struct {
} }
func (c cmdable) XAdd(ctx context.Context, a *XAddArgs) *StringCmd { func (c cmdable) XAdd(ctx context.Context, a *XAddArgs) *StringCmd {
var values []interface{} args := make([]interface{}, 0, 8)
values = appendArgs(values, []interface{}{a.Values})
args := make([]interface{}, 0, 6+len(values))
args = append(args, "xadd") args = append(args, "xadd")
args = append(args, a.Stream) args = append(args, a.Stream)
if a.MaxLen > 0 { if a.MaxLen > 0 {
@ -1421,7 +1424,7 @@ func (c cmdable) XAdd(ctx context.Context, a *XAddArgs) *StringCmd {
} else { } else {
args = append(args, "*") args = append(args, "*")
} }
args = append(args, values...) args = appendArg(args, a.Values)
cmd := NewStringCmd(ctx, args...) cmd := NewStringCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)