redis-rb/redis-client

Can not pass in nil for HMSET

Closed this issue · 4 comments

It raises an error:

[4] pry(main)> RedisCache.current.hmset('foo123', 'time', Time.current.to_s, 'foo', nil)
TypeError: Unsupported command argument type: NilClass
from /usr/local/bundle/ruby/3.2.0/gems/redis-client-0.12.0/lib/redis_client/command_builder.rb:37:in `block in generate'

Previously:

Redis.current.hmset('foo123', 'time', Time.current.to_s, 'foo', nil)
=> "OK"


Redis.current.hget('foo123', 'foo')
=> ""

This is by design. Redis hash values can only be strings, you either have not to set that key, or to cast that nil to an empty string.

@byroot it seems interesting that we auto convert integers to strings

when Integer, Float
element.to_s

but don't do something like

when nil then ''

Yes, because integers have an unambiguous string representation, so it's easy to know that's what the caller expects.

Casting nil to "" however is super opiniated, other languages would cast it to "null" or something.

Also nil is much more likely to be a mistake.

Ahh I see thanks for the explanation