stefanwille / crystal-redis

Full featured Redis client for Crystal
MIT License
380 stars 61 forks source link

Cast from Array(Redis::RedisValue) to (String | Nil) failed when using brpoplpush #68

Closed parruda closed 5 years ago

parruda commented 5 years ago

Forgive me if that's just because I am just learning Crystal, but I am stuck on this. I am trying to pop something from a queue with:

message = redis.brpoplpush("queue_name", "process_queue_name", 15)

The docs say:

Return value: String, the element being popped from source and pushed to destination. If timeout is reached, nil is returned

Once timeout is reached, I am getting this error:

Exception: cast from Array(Redis::RedisValue) to (String | Nil) failed, at /dev/lib/redis/src/redis/command_execution/value_oriented.cr:37:9:37 (TypeCastError)

What am I doing wrong?

maiha commented 5 years ago

Hi @parruda , welcome to Crystal!

It seems a problem of the library. Although I created a PR, please put this monkey patch into your app if you are in hurry. 😃

class Redis
  module CommandExecution
    module ValueOriented
      def string_or_nil_command(request : Request) : String?
        res = command(request)
        res = nil if res.is_a?(Array) && res.empty?
        res.as(String?)
      end
    end
  end
end
parruda commented 5 years ago

It works. Thanks a lot @maiha !

stefanwille commented 5 years ago

I have merged @maiha's PR and published release 2.1.1. Please try!

parruda commented 5 years ago

It works. Thanks!