Skip to content

Commit 9dd00b2

Browse files
committed
Eagerly and strictly cast Integer and Float parameters
This helps cathing errors early, and allow `to_i` and similar interface to work out of the box.
1 parent 4bc949e commit 9dd00b2

File tree

10 files changed

+46
-39
lines changed

10 files changed

+46
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Eagerly and strictly cast Integer and Float parameters.
4+
35
# 5.0.0.beta4
46

57
- Allow to call `subscribe`, `unsubscribe`, `psubscribe` and `punsubscribe` from a subscribed client. See #1131.

lib/redis/commands/geo.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def geodist(key, member1, member2, unit = 'm')
7474
private
7575

7676
def _geoarguments(*args, options: nil, sort: nil, count: nil)
77-
args.push sort if sort
78-
args.push 'count', count if count
79-
args.push options if options
77+
args << sort if sort
78+
args << 'COUNT' << Integer(count) if count
79+
args << options if options
8080
args
8181
end
8282
end

lib/redis/commands/hashes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def hexists(key, field)
174174
# @param [Integer] increment
175175
# @return [Integer] value of the field after incrementing it
176176
def hincrby(key, field, increment)
177-
send_command([:hincrby, key, field, increment])
177+
send_command([:hincrby, key, field, Integer(increment)])
178178
end
179179

180180
# Increment the numeric value of a hash field by the given float number.
@@ -184,7 +184,7 @@ def hincrby(key, field, increment)
184184
# @param [Float] increment
185185
# @return [Float] value of the field after incrementing it
186186
def hincrbyfloat(key, field, increment)
187-
send_command([:hincrbyfloat, key, field, increment], &Floatify)
187+
send_command([:hincrbyfloat, key, field, Float(increment)], &Floatify)
188188
end
189189

190190
# Get all the fields in a hash.

lib/redis/commands/keys.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def _scan(command, cursor, args, match: nil, count: nil, type: nil, &block)
427427

428428
args << cursor
429429
args << "MATCH" << match if match
430-
args << "COUNT" << count if count
430+
args << "COUNT" << Integer(count) if count
431431
args << "TYPE" << type if type
432432

433433
send_command([command] + args, &block)

lib/redis/commands/lists.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def rpushx(key, value)
102102
# @return [String, Array<String>] the values of the first elements
103103
def lpop(key, count = nil)
104104
command = [:lpop, key]
105-
command << count if count
105+
command << Integer(count) if count
106106
send_command(command)
107107
end
108108

@@ -113,7 +113,7 @@ def lpop(key, count = nil)
113113
# @return [String, Array<String>] the values of the last elements
114114
def rpop(key, count = nil)
115115
command = [:rpop, key]
116-
command << count if count
116+
command << Integer(count) if count
117117
send_command(command)
118118
end
119119

@@ -189,7 +189,7 @@ def brpoplpush(source, destination, timeout: 0)
189189
# @param [Integer] index
190190
# @return [String]
191191
def lindex(key, index)
192-
send_command([:lindex, key, index])
192+
send_command([:lindex, key, Integer(index)])
193193
end
194194

195195
# Insert an element before or after another element in a list.
@@ -211,7 +211,7 @@ def linsert(key, where, pivot, value)
211211
# @param [Integer] stop stop index
212212
# @return [Array<String>]
213213
def lrange(key, start, stop)
214-
send_command([:lrange, key, start, stop])
214+
send_command([:lrange, key, Integer(start), Integer(stop)])
215215
end
216216

217217
# Remove elements from a list.
@@ -224,7 +224,7 @@ def lrange(key, start, stop)
224224
# @param [String] value
225225
# @return [Integer] the number of removed elements
226226
def lrem(key, count, value)
227-
send_command([:lrem, key, count, value])
227+
send_command([:lrem, key, Integer(count), value])
228228
end
229229

230230
# Set the value of an element in a list by its index.
@@ -234,7 +234,7 @@ def lrem(key, count, value)
234234
# @param [String] value
235235
# @return [String] `OK`
236236
def lset(key, index, value)
237-
send_command([:lset, key, index, value])
237+
send_command([:lset, key, Integer(index), value])
238238
end
239239

240240
# Trim a list to the specified range.
@@ -244,7 +244,7 @@ def lset(key, index, value)
244244
# @param [Integer] stop stop index
245245
# @return [String] `OK`
246246
def ltrim(key, start, stop)
247-
send_command([:ltrim, key, start, stop])
247+
send_command([:ltrim, key, Integer(start), Integer(stop)])
248248
end
249249

250250
private

lib/redis/commands/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def slaveof(host, port)
158158
# @return [Array<String>, Integer, String] depends on subcommand
159159
def slowlog(subcommand, length = nil)
160160
args = [:slowlog, subcommand]
161-
args << length if length
161+
args << Integer(length) if length
162162
send_command(args)
163163
end
164164

lib/redis/commands/sets.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def spop(key, count = nil)
6060
if count.nil?
6161
send_command([:spop, key])
6262
else
63-
send_command([:spop, key, count])
63+
send_command([:spop, key, Integer(count)])
6464
end
6565
end
6666

lib/redis/commands/sorted_sets.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ def zrem(key, member)
136136
# @return [Array<String, Float>] element and score pair if count is not specified
137137
# @return [Array<Array<String, Float>>] list of popped elements and scores
138138
def zpopmax(key, count = nil)
139-
send_command([:zpopmax, key, count].compact) do |members|
139+
command = [:zpopmax, key]
140+
command << Integer(count) if count
141+
send_command(command) do |members|
140142
members = FloatifyPairs.call(members)
141143
count.to_i > 1 ? members : members.first
142144
end
@@ -157,7 +159,9 @@ def zpopmax(key, count = nil)
157159
# @return [Array<String, Float>] element and score pair if count is not specified
158160
# @return [Array<Array<String, Float>>] list of popped elements and scores
159161
def zpopmin(key, count = nil)
160-
send_command([:zpopmin, key, count].compact) do |members|
162+
command = [:zpopmin, key]
163+
command << Integer(count) if count
164+
send_command(command) do |members|
161165
members = FloatifyPairs.call(members)
162166
count.to_i > 1 ? members : members.first
163167
end
@@ -261,7 +265,7 @@ def zrandmember(key, count = nil, withscores: false, with_scores: withscores)
261265
end
262266

263267
args = [:zrandmember, key]
264-
args << count if count
268+
args << Integer(count) if count
265269

266270
if with_scores
267271
args << "WITHSCORES"
@@ -313,7 +317,7 @@ def zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by
313317

314318
if limit
315319
args << "LIMIT"
316-
args.concat(limit)
320+
args.concat(limit.map { |l| Integer(l) })
317321
end
318322

319323
if with_scores
@@ -354,7 +358,7 @@ def zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscor
354358

355359
if limit
356360
args << "LIMIT"
357-
args.concat(limit)
361+
args.concat(limit.map { |l| Integer(l) })
358362
end
359363

360364
send_command(args)
@@ -372,7 +376,7 @@ def zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscor
372376
#
373377
# @see #zrange
374378
def zrevrange(key, start, stop, withscores: false, with_scores: withscores)
375-
args = [:zrevrange, key, start, stop]
379+
args = [:zrevrange, key, Integer(start), Integer(stop)]
376380

377381
if with_scores
378382
args << "WITHSCORES"
@@ -466,7 +470,7 @@ def zrangebylex(key, min, max, limit: nil)
466470

467471
if limit
468472
args << "LIMIT"
469-
args.concat(limit)
473+
args.concat(limit.map { |l| Integer(l) })
470474
end
471475

472476
send_command(args)
@@ -488,7 +492,7 @@ def zrevrangebylex(key, max, min, limit: nil)
488492

489493
if limit
490494
args << "LIMIT"
491-
args.concat(limit)
495+
args.concat(limit.map { |l| Integer(l) })
492496
end
493497

494498
send_command(args)
@@ -531,7 +535,7 @@ def zrangebyscore(key, min, max, withscores: false, with_scores: withscores, lim
531535

532536
if limit
533537
args << "LIMIT"
534-
args.concat(limit)
538+
args.concat(limit.map { |l| Integer(l) })
535539
end
536540

537541
send_command(args, &block)
@@ -561,7 +565,7 @@ def zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores,
561565

562566
if limit
563567
args << "LIMIT"
564-
args.concat(limit)
568+
args.concat(limit.map { |l| Integer(l) })
565569
end
566570

567571
send_command(args, &block)

lib/redis/commands/strings.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def decr(key)
2525
# @param [Integer] decrement
2626
# @return [Integer] value after decrementing it
2727
def decrby(key, decrement)
28-
send_command([:decrby, key, decrement])
28+
send_command([:decrby, key, Integer(decrement)])
2929
end
3030

3131
# Increment the integer value of a key by one.
@@ -50,7 +50,7 @@ def incr(key)
5050
# @param [Integer] increment
5151
# @return [Integer] value after incrementing it
5252
def incrby(key, increment)
53-
send_command([:incrby, key, increment])
53+
send_command([:incrby, key, Integer(increment)])
5454
end
5555

5656
# Increment the numeric value of a key by the given float number.
@@ -63,7 +63,7 @@ def incrby(key, increment)
6363
# @param [Float] increment
6464
# @return [Float] value after incrementing it
6565
def incrbyfloat(key, increment)
66-
send_command([:incrbyfloat, key, increment], &Floatify)
66+
send_command([:incrbyfloat, key, Float(increment)], &Floatify)
6767
end
6868

6969
# Set the string value of a key.
@@ -82,10 +82,10 @@ def incrbyfloat(key, increment)
8282
# @return [String, Boolean] `"OK"` or true, false if `:nx => true` or `:xx => true`
8383
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
8484
args = [:set, key, value.to_s]
85-
args << "EX" << ex if ex
86-
args << "PX" << px if px
87-
args << "EXAT" << exat if exat
88-
args << "PXAT" << pxat if pxat
85+
args << "EX" << Integer(ex) if ex
86+
args << "PX" << Integer(px) if px
87+
args << "EXAT" << Integer(exat) if exat
88+
args << "PXAT" << Integer(pxat) if pxat
8989
args << "NX" if nx
9090
args << "XX" if xx
9191
args << "KEEPTTL" if keepttl
@@ -233,7 +233,7 @@ def mapped_mget(*keys)
233233
# @param [String] value
234234
# @return [Integer] length of the string after it was modified
235235
def setrange(key, offset, value)
236-
send_command([:setrange, key, offset, value.to_s])
236+
send_command([:setrange, key, Integer(offset), value.to_s])
237237
end
238238

239239
# Get a substring of the string stored at a key.
@@ -244,7 +244,7 @@ def setrange(key, offset, value)
244244
# the end of the string
245245
# @return [Integer] `0` or `1`
246246
def getrange(key, start, stop)
247-
send_command([:getrange, key, start, stop])
247+
send_command([:getrange, key, Integer(start), Integer(stop)])
248248
end
249249

250250
# Append a value to a key.
@@ -292,10 +292,10 @@ def getdel(key)
292292
# @return [String] The value of key, or nil when key does not exist.
293293
def getex(key, ex: nil, px: nil, exat: nil, pxat: nil, persist: false)
294294
args = [:getex, key]
295-
args << "EX" << ex if ex
296-
args << "PX" << px if px
297-
args << "EXAT" << exat if exat
298-
args << "PXAT" << pxat if pxat
295+
args << "EX" << Integer(ex) if ex
296+
args << "PX" << Integer(px) if px
297+
args << "EXAT" << Integer(exat) if exat
298+
args << "PXAT" << Integer(pxat) if pxat
299299
args << "PERSIST" if persist
300300

301301
send_command(args)

test/redis/pipelining_commands_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ def test_futures_raise_when_trying_to_access_their_values_too_early
130130
def test_futures_raise_when_command_errors_and_needs_transformation
131131
assert_raises(Redis::CommandError) do
132132
r.pipelined do |p|
133-
@result = p.zrange("a", "b", 5, with_scores: true)
133+
p.zadd("set", "1", "one")
134+
@result = p.zincryby("set", "fail", "one")
134135
end
135136
end
136137
end

0 commit comments

Comments
 (0)