Skip to content

Eagerly and strictly cast Integer and Float parameters #1133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Eagerly and strictly cast Integer and Float parameters.

# 5.0.0.beta4

- Allow to call `subscribe`, `unsubscribe`, `psubscribe` and `punsubscribe` from a subscribed client. See #1131.
Expand Down
6 changes: 3 additions & 3 deletions lib/redis/commands/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def geodist(key, member1, member2, unit = 'm')
private

def _geoarguments(*args, options: nil, sort: nil, count: nil)
args.push sort if sort
args.push 'count', count if count
args.push options if options
args << sort if sort
args << 'COUNT' << Integer(count) if count
args << options if options
args
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/redis/commands/hashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def hexists(key, field)
# @param [Integer] increment
# @return [Integer] value of the field after incrementing it
def hincrby(key, field, increment)
send_command([:hincrby, key, field, increment])
send_command([:hincrby, key, field, Integer(increment)])
end

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

# Get all the fields in a hash.
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/commands/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def _scan(command, cursor, args, match: nil, count: nil, type: nil, &block)

args << cursor
args << "MATCH" << match if match
args << "COUNT" << count if count
args << "COUNT" << Integer(count) if count
args << "TYPE" << type if type

send_command([command] + args, &block)
Expand Down
14 changes: 7 additions & 7 deletions lib/redis/commands/lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def rpushx(key, value)
# @return [String, Array<String>] the values of the first elements
def lpop(key, count = nil)
command = [:lpop, key]
command << count if count
command << Integer(count) if count
send_command(command)
end

Expand All @@ -113,7 +113,7 @@ def lpop(key, count = nil)
# @return [String, Array<String>] the values of the last elements
def rpop(key, count = nil)
command = [:rpop, key]
command << count if count
command << Integer(count) if count
send_command(command)
end

Expand Down Expand Up @@ -189,7 +189,7 @@ def brpoplpush(source, destination, timeout: 0)
# @param [Integer] index
# @return [String]
def lindex(key, index)
send_command([:lindex, key, index])
send_command([:lindex, key, Integer(index)])
end

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

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

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

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

private
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/commands/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def slaveof(host, port)
# @return [Array<String>, Integer, String] depends on subcommand
def slowlog(subcommand, length = nil)
args = [:slowlog, subcommand]
args << length if length
args << Integer(length) if length
send_command(args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/redis/commands/sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def spop(key, count = nil)
if count.nil?
send_command([:spop, key])
else
send_command([:spop, key, count])
send_command([:spop, key, Integer(count)])
end
end

Expand Down
24 changes: 14 additions & 10 deletions lib/redis/commands/sorted_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def zrem(key, member)
# @return [Array<String, Float>] element and score pair if count is not specified
# @return [Array<Array<String, Float>>] list of popped elements and scores
def zpopmax(key, count = nil)
send_command([:zpopmax, key, count].compact) do |members|
command = [:zpopmax, key]
command << Integer(count) if count
send_command(command) do |members|
members = FloatifyPairs.call(members)
count.to_i > 1 ? members : members.first
end
Expand All @@ -157,7 +159,9 @@ def zpopmax(key, count = nil)
# @return [Array<String, Float>] element and score pair if count is not specified
# @return [Array<Array<String, Float>>] list of popped elements and scores
def zpopmin(key, count = nil)
send_command([:zpopmin, key, count].compact) do |members|
command = [:zpopmin, key]
command << Integer(count) if count
send_command(command) do |members|
members = FloatifyPairs.call(members)
count.to_i > 1 ? members : members.first
end
Expand Down Expand Up @@ -261,7 +265,7 @@ def zrandmember(key, count = nil, withscores: false, with_scores: withscores)
end

args = [:zrandmember, key]
args << count if count
args << Integer(count) if count

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

if limit
args << "LIMIT"
args.concat(limit)
args.concat(limit.map { |l| Integer(l) })
end

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

if limit
args << "LIMIT"
args.concat(limit)
args.concat(limit.map { |l| Integer(l) })
end

send_command(args)
Expand All @@ -372,7 +376,7 @@ def zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscor
#
# @see #zrange
def zrevrange(key, start, stop, withscores: false, with_scores: withscores)
args = [:zrevrange, key, start, stop]
args = [:zrevrange, key, Integer(start), Integer(stop)]

if with_scores
args << "WITHSCORES"
Expand Down Expand Up @@ -466,7 +470,7 @@ def zrangebylex(key, min, max, limit: nil)

if limit
args << "LIMIT"
args.concat(limit)
args.concat(limit.map { |l| Integer(l) })
end

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

if limit
args << "LIMIT"
args.concat(limit)
args.concat(limit.map { |l| Integer(l) })
end

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

if limit
args << "LIMIT"
args.concat(limit)
args.concat(limit.map { |l| Integer(l) })
end

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

if limit
args << "LIMIT"
args.concat(limit)
args.concat(limit.map { |l| Integer(l) })
end

send_command(args, &block)
Expand Down
26 changes: 13 additions & 13 deletions lib/redis/commands/strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def decr(key)
# @param [Integer] decrement
# @return [Integer] value after decrementing it
def decrby(key, decrement)
send_command([:decrby, key, decrement])
send_command([:decrby, key, Integer(decrement)])
end

# Increment the integer value of a key by one.
Expand All @@ -50,7 +50,7 @@ def incr(key)
# @param [Integer] increment
# @return [Integer] value after incrementing it
def incrby(key, increment)
send_command([:incrby, key, increment])
send_command([:incrby, key, Integer(increment)])
end

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

# Set the string value of a key.
Expand All @@ -82,10 +82,10 @@ def incrbyfloat(key, increment)
# @return [String, Boolean] `"OK"` or true, false if `:nx => true` or `:xx => true`
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
args = [:set, key, value.to_s]
args << "EX" << ex if ex
args << "PX" << px if px
args << "EXAT" << exat if exat
args << "PXAT" << pxat if pxat
args << "EX" << Integer(ex) if ex
args << "PX" << Integer(px) if px
args << "EXAT" << Integer(exat) if exat
args << "PXAT" << Integer(pxat) if pxat
args << "NX" if nx
args << "XX" if xx
args << "KEEPTTL" if keepttl
Expand Down Expand Up @@ -233,7 +233,7 @@ def mapped_mget(*keys)
# @param [String] value
# @return [Integer] length of the string after it was modified
def setrange(key, offset, value)
send_command([:setrange, key, offset, value.to_s])
send_command([:setrange, key, Integer(offset), value.to_s])
end

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

# Append a value to a key.
Expand Down Expand Up @@ -292,10 +292,10 @@ def getdel(key)
# @return [String] The value of key, or nil when key does not exist.
def getex(key, ex: nil, px: nil, exat: nil, pxat: nil, persist: false)
args = [:getex, key]
args << "EX" << ex if ex
args << "PX" << px if px
args << "EXAT" << exat if exat
args << "PXAT" << pxat if pxat
args << "EX" << Integer(ex) if ex
args << "PX" << Integer(px) if px
args << "EXAT" << Integer(exat) if exat
args << "PXAT" << Integer(pxat) if pxat
args << "PERSIST" if persist

send_command(args)
Expand Down
3 changes: 2 additions & 1 deletion test/redis/pipelining_commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def test_futures_raise_when_trying_to_access_their_values_too_early
def test_futures_raise_when_command_errors_and_needs_transformation
assert_raises(Redis::CommandError) do
r.pipelined do |p|
@result = p.zrange("a", "b", 5, with_scores: true)
p.zadd("set", "1", "one")
@result = p.zincryby("set", "fail", "one")
end
end
end
Expand Down