gosexy / redis

Redis client for Go that maps the full redis command list into equivalent Go functions.
MIT License
167 stars 44 forks source link

Get() of Non-Existent Key Returns Zero-String #23

Closed ewhac closed 10 years ago

ewhac commented 10 years ago

The fix for issue #12 has itself caused a problem: It is now no longer possible to detect via Get() that a key does not exist.

If you GET a non-existent key in Redis, you get a nil value:

127.0.0.1:6379> get nokey
(nil)

To fix issue #12, nil responses from the server were converted to the Go zero-value for a string, which is "" (empty string). However, "" is a valid value for a Redis key:

127.0.0.1:6379> set nokey ""
OK
127.0.0.1:6379> get nokey
""

As such, keys with empty values are now indistinguishable from keys that do not exist. This is a problem.

It is unclear how to fix this without breaking existing client code. One possibility might be a settable option in the Client struct that says how to deal with nil values from the server, and passed as an argument to setReplyValue()...

xiam commented 10 years ago

Hi @ewhac,

As you know, (nil) is a valid redis value that does not exists in the Go string type. The (nil) redis reply returned the redis.ErrNilReply error value in an older version of gosexy/redis when a (nil) reply was received from the redis-server, I think we'd start experimenting with it again.

xiam commented 10 years ago

Please see https://github.com/gosexy/redis/commit/31f89609a38bb4aeb32c70b7f570d0a92f2402cb#diff-53618cae32e23ae47e1c35f10da92654R152