debasishg / scala-redis

A scala library for connecting to a redis server, or a cluster of redis nodes using consistent hashing on the client side.
1.02k stars 219 forks source link

Type information is lost when using the Pipeline function #191

Closed dqdinh closed 7 years ago

dqdinh commented 7 years ago

Hi,

I’m using the pipeline function to do batch atomic Redis operations — https://github.com/debasishg/scala-redis/blob/master/src/main/scala/com/redis/RedisClient.scala#L113-L129

The pipeline function does not retain type information and instead lumps all transaction results as List[Any]. This is unfortunate because running any method off of RedisClient will return the correct type information.

Let me know if this a known issue or if I need to re-read the source and tests?

This is strange also because the tests show that the return types retains their type information (https://github.com/debasishg/scala-redis/blob/master/src/test/scala/com/redis/PipelineSpec.scala#L28-L36)

Other than converting the Any result to a string, parsing, and converting back to the target data structure, do you have any ideas on how to retain the type information for transactions using pipeline?

Here's my console output:

import com.redis._

val r = new RedisClient("localhost", 6379)

r.pipeline { p =>
         p.set("key", "debasish")
         p.get("key")
         p.get("key1")
       }.get

// res: List[Any] = List(true, Some(debasish), None)

 r.get("key")
// res: Option[String] = Some(debasish)
debasishg commented 7 years ago

Unless you are returning something like HList of Shapeless, List(true, Some(debasish), None) can only be typed as List[Any].
Also if you look at the function definition of pipeline, it returns an Option[List[Any]]. So, yeah, it's a known limitation and custom processing needs to be done at the client side to retrieve the types. Maybe using HList in redisclient can be an interesting idea to handle such situations.

dqdinh commented 7 years ago

Thanks for the feedback!