Skip to content

Commit

Permalink
Use be more flexible about the return type
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wille committed Dec 28, 2019
1 parent 8195e8a commit af5ea30
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
15 changes: 15 additions & 0 deletions spec/redis_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,13 @@ describe Redis do
result = redis.eval("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", keys, args)
result.should eq(["key1", "key2", "first", "second"])
end

it "handles different return types" do
keys = ["key1", "key2"] of Redis::RedisValue
args = ["first", "second"] of Redis::RedisValue
result = redis.eval("return KEYS[1]", keys, args)
result.should eq("key1")
end
end

describe "#script_load / #eval_sha" do
Expand All @@ -1197,6 +1204,14 @@ describe Redis do
result = redis.evalsha(sha1, keys, args)
result.should eq(["key1", "first"])
end

it "handles different return types" do
sha1 = redis.script_load("return KEYS[1]")
keys = ["key1", "key2"] of Redis::RedisValue
args = ["first", "second"] of Redis::RedisValue
result = redis.evalsha(sha1, keys, args)
result.should eq("key1")
end
end

describe "#script_kill" do
Expand Down
6 changes: 6 additions & 0 deletions src/redis/command_execution/future_oriented.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class Redis
command(request)
end

# Executes a Redis command and casts the response to the correct type.
# This is an internal method.
def string_array_or_string_or_integer_command(request : Request) : Redis::Future
command(request)
end

# Executes a Redis command and returns a Future.
def string_array_or_string_or_nil_command(request : Request) : Redis::Future
command(request)
Expand Down
6 changes: 6 additions & 0 deletions src/redis/command_execution/value_oriented.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Redis
command(request).as(Array(RedisValue) | Int64)
end

# Executes a Redis command and casts the response to the correct type.
# This is an internal method.
def string_array_or_string_or_integer_command(request : Request) : Array(RedisValue) | String | Int64
command(request).as(Array(RedisValue) | String | Int64)
end

# Executes a Redis command and casts the response to the correct type.
# This is an internal method.
def string_array_or_string_command(request : Request) : Array(RedisValue) | String
Expand Down
8 changes: 4 additions & 4 deletions src/redis/commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1470,23 +1470,23 @@ class Redis
# EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter
# built into Redis starting from version 2.6.0.
#
# **Return value**: Array(String), depends on the executed script
# **Return value**: Depends on the executed script
#
# Example:
#
# ```
# redis.eval("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", ["key1", "key2"], ["first art", "second arg"])
# ```
def eval(script : String, keys = [] of RedisValue, args = [] of RedisValue)
string_array_or_integer_command(concat(["EVAL", script, keys.size.to_s], keys, args))
string_array_or_string_or_integer_command(concat(["EVAL", script, keys.size.to_s], keys, args))
end

# EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter
# built into Redis starting from version 2.6.0.
#
# **Return value**: Array(String), depends on the executed script
# **Return value**: Depends on the executed script
def evalsha(sha1, keys = [] of RedisValue, args = [] of RedisValue)
string_array_or_integer_command(concat(["EVALSHA", sha1.to_s, keys.size.to_s], keys, args))
string_array_or_string_or_integer_command(concat(["EVALSHA", sha1.to_s, keys.size.to_s], keys, args))
end

# Load a script into the scripts cache, without executing it.
Expand Down

0 comments on commit af5ea30

Please sign in to comment.