Skip to content

Commit 59a2102

Browse files
authored
Merge pull request #1097 from amomchilov/noop-variadic-APIs-on-empty-input
Make variadic APIs no-op if given empty values
2 parents 674541c + 13cbec2 commit 59a2102

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/redis/commands/sorted_sets.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ def zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil)
6060
command << "INCR" if incr
6161

6262
if args.size == 1 && args[0].is_a?(Array)
63+
members_to_add = args[0]
64+
return 0 if members_to_add.empty?
65+
6366
# Variadic: return float if INCR, integer if !INCR
64-
send_command(command + args[0], &(incr ? Floatify : nil))
67+
send_command(command + members_to_add, &(incr ? Floatify : nil))
6568
elsif args.size == 2
6669
# Single pair: return float if INCR, boolean if !INCR
6770
send_command(command + args, &(incr ? Floatify : Boolify))
@@ -102,6 +105,11 @@ def zincrby(key, increment, member)
102105
# - `Integer` when an array of pairs is specified, holding the number of
103106
# members that were removed to the sorted set
104107
def zrem(key, member)
108+
if member.is_a?(Array)
109+
members_to_remove = member
110+
return 0 if members_to_remove.empty?
111+
end
112+
105113
send_command([:zrem, key, member]) do |reply|
106114
if member.is_a? Array
107115
# Variadic: return integer

test/lint/sorted_sets.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,32 @@ def test_variadic_zadd
8080
target_version "2.3.9" do # 2.4-rc6
8181
# Non-nested array with pairs
8282
assert_equal 0, r.zcard("foo")
83+
8384
assert_equal 2, r.zadd("foo", [1, "s1", 2, "s2"])
85+
assert_equal 2, r.zcard("foo")
86+
8487
assert_equal 1, r.zadd("foo", [4, "s1", 5, "s2", 6, "s3"])
8588
assert_equal 3, r.zcard("foo")
89+
8690
r.del "foo"
8791

8892
# Nested array with pairs
8993
assert_equal 0, r.zcard("foo")
94+
9095
assert_equal 2, r.zadd("foo", [[1, "s1"], [2, "s2"]])
96+
assert_equal 2, r.zcard("foo")
97+
9198
assert_equal 1, r.zadd("foo", [[4, "s1"], [5, "s2"], [6, "s3"]])
9299
assert_equal 3, r.zcard("foo")
100+
101+
r.del "foo"
102+
103+
# Empty array
104+
assert_equal 0, r.zcard("foo")
105+
106+
assert_equal 0, r.zadd("foo", [])
107+
assert_equal 0, r.zcard("foo")
108+
93109
r.del "foo"
94110

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

181197
assert_equal 3, r.zcard("foo")
198+
199+
assert_equal 0, r.zrem("foo", [])
200+
assert_equal 3, r.zcard("foo")
201+
182202
assert_equal 1, r.zrem("foo", ["s1", "aaa"])
203+
assert_equal 2, r.zcard("foo")
204+
183205
assert_equal 0, r.zrem("foo", ["bbb", "ccc", "ddd"])
206+
assert_equal 2, r.zcard("foo")
207+
184208
assert_equal 1, r.zrem("foo", ["eee", "s3"])
185209
assert_equal 1, r.zcard("foo")
186210
end

0 commit comments

Comments
 (0)