forked from mirror/redis
Merge pull request #297 from go-redis/fix/eval-interface
Accept interface{} in Eval. Fixes #243.
This commit is contained in:
commit
b1aa808811
|
@ -1543,7 +1543,7 @@ func (c *commandable) Time() *StringSliceCmd {
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
func (c *commandable) Eval(script string, keys []string, args []string) *Cmd {
|
func (c *commandable) Eval(script string, keys []string, args ...interface{}) *Cmd {
|
||||||
cmdArgs := make([]interface{}, 3+len(keys)+len(args))
|
cmdArgs := make([]interface{}, 3+len(keys)+len(args))
|
||||||
cmdArgs[0] = "EVAL"
|
cmdArgs[0] = "EVAL"
|
||||||
cmdArgs[1] = script
|
cmdArgs[1] = script
|
||||||
|
@ -1563,7 +1563,7 @@ func (c *commandable) Eval(script string, keys []string, args []string) *Cmd {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandable) EvalSha(sha1 string, keys []string, args []string) *Cmd {
|
func (c *commandable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
|
||||||
cmdArgs := make([]interface{}, 3+len(keys)+len(args))
|
cmdArgs := make([]interface{}, 3+len(keys)+len(args))
|
||||||
cmdArgs[0] = "EVALSHA"
|
cmdArgs[0] = "EVALSHA"
|
||||||
cmdArgs[1] = sha1
|
cmdArgs[1] = sha1
|
||||||
|
|
|
@ -288,7 +288,7 @@ func ExampleScript() {
|
||||||
return false
|
return false
|
||||||
`)
|
`)
|
||||||
|
|
||||||
n, err := IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
|
n, err := IncrByXX.Run(client, []string{"xx_counter"}, 2).Result()
|
||||||
fmt.Println(n, err)
|
fmt.Println(n, err)
|
||||||
|
|
||||||
err = client.Set("xx_counter", "40", 0).Err()
|
err = client.Set("xx_counter", "40", 0).Err()
|
||||||
|
@ -296,7 +296,7 @@ func ExampleScript() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err = IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
|
n, err = IncrByXX.Run(client, []string{"xx_counter"}, 2).Result()
|
||||||
fmt.Println(n, err)
|
fmt.Println(n, err)
|
||||||
|
|
||||||
// Output: <nil> redis: nil
|
// Output: <nil> redis: nil
|
||||||
|
|
18
script.go
18
script.go
|
@ -8,8 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type scripter interface {
|
type scripter interface {
|
||||||
Eval(script string, keys []string, args []string) *Cmd
|
Eval(script string, keys []string, args ...interface{}) *Cmd
|
||||||
EvalSha(sha1 string, keys []string, args []string) *Cmd
|
EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
|
||||||
ScriptExists(scripts ...string) *BoolSliceCmd
|
ScriptExists(scripts ...string) *BoolSliceCmd
|
||||||
ScriptLoad(script string) *StringCmd
|
ScriptLoad(script string) *StringCmd
|
||||||
}
|
}
|
||||||
|
@ -35,18 +35,18 @@ func (s *Script) Exists(c scripter) *BoolSliceCmd {
|
||||||
return c.ScriptExists(s.src)
|
return c.ScriptExists(s.src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Script) Eval(c scripter, keys []string, args []string) *Cmd {
|
func (s *Script) Eval(c scripter, keys []string, args ...interface{}) *Cmd {
|
||||||
return c.Eval(s.src, keys, args)
|
return c.Eval(s.src, keys, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Script) EvalSha(c scripter, keys []string, args []string) *Cmd {
|
func (s *Script) EvalSha(c scripter, keys []string, args ...interface{}) *Cmd {
|
||||||
return c.EvalSha(s.hash, keys, args)
|
return c.EvalSha(s.hash, keys, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Script) Run(c scripter, keys []string, args []string) *Cmd {
|
func (s *Script) Run(c scripter, keys []string, args ...interface{}) *Cmd {
|
||||||
r := s.EvalSha(c, keys, args)
|
r := s.EvalSha(c, keys, args...)
|
||||||
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
|
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
|
||||||
return s.Eval(c, keys, args)
|
return s.Eval(c, keys, args...)
|
||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue