Skip to content

Commit

Permalink
Make variadic APIs no-op if given empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Apr 26, 2022
1 parent 674541c commit 13cbec2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/redis/commands/sorted_sets.rb
Expand Up @@ -60,8 +60,11 @@ def zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil)
command << "INCR" if incr

if args.size == 1 && args[0].is_a?(Array)
members_to_add = args[0]
return 0 if members_to_add.empty?

# Variadic: return float if INCR, integer if !INCR
send_command(command + args[0], &(incr ? Floatify : nil))
send_command(command + members_to_add, &(incr ? Floatify : nil))
elsif args.size == 2
# Single pair: return float if INCR, boolean if !INCR
send_command(command + args, &(incr ? Floatify : Boolify))
Expand Down Expand Up @@ -102,6 +105,11 @@ def zincrby(key, increment, member)
# - `Integer` when an array of pairs is specified, holding the number of
# members that were removed to the sorted set
def zrem(key, member)
if member.is_a?(Array)
members_to_remove = member
return 0 if members_to_remove.empty?
end

send_command([:zrem, key, member]) do |reply|
if member.is_a? Array
# Variadic: return integer
Expand Down
24 changes: 24 additions & 0 deletions test/lint/sorted_sets.rb
Expand Up @@ -80,16 +80,32 @@ def test_variadic_zadd
target_version "2.3.9" do # 2.4-rc6
# Non-nested array with pairs
assert_equal 0, r.zcard("foo")

assert_equal 2, r.zadd("foo", [1, "s1", 2, "s2"])
assert_equal 2, r.zcard("foo")

assert_equal 1, r.zadd("foo", [4, "s1", 5, "s2", 6, "s3"])
assert_equal 3, r.zcard("foo")

r.del "foo"

# Nested array with pairs
assert_equal 0, r.zcard("foo")

assert_equal 2, r.zadd("foo", [[1, "s1"], [2, "s2"]])
assert_equal 2, r.zcard("foo")

assert_equal 1, r.zadd("foo", [[4, "s1"], [5, "s2"], [6, "s3"]])
assert_equal 3, r.zcard("foo")

r.del "foo"

# Empty array
assert_equal 0, r.zcard("foo")

assert_equal 0, r.zadd("foo", [])
assert_equal 0, r.zcard("foo")

r.del "foo"

# Wrong number of arguments
Expand Down Expand Up @@ -179,8 +195,16 @@ def test_variadic_zrem
r.zadd("foo", 3, "s3")

assert_equal 3, r.zcard("foo")

assert_equal 0, r.zrem("foo", [])
assert_equal 3, r.zcard("foo")

assert_equal 1, r.zrem("foo", ["s1", "aaa"])
assert_equal 2, r.zcard("foo")

assert_equal 0, r.zrem("foo", ["bbb", "ccc", "ddd"])
assert_equal 2, r.zcard("foo")

assert_equal 1, r.zrem("foo", ["eee", "s3"])
assert_equal 1, r.zcard("foo")
end
Expand Down

0 comments on commit 13cbec2

Please sign in to comment.