Can not pass in nil for HMSET
Closed this issue · 4 comments
arianf commented
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')
=> ""
byroot commented
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.
arianf commented
@byroot it seems interesting that we auto convert integers to strings
redis-client/lib/redis_client/command_builder.rb
Lines 34 to 35 in 7897ffe
but don't do something like
when nil then ''
byroot commented
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.
arianf commented
Ahh I see thanks for the explanation