I've seen examples of how to do the conversion of a Map[String,String]
such as ("id"->"1", "name"->"andre")
to a case class User(id:String,name:String)
, but they involve calling a method mapToUser
- for example:
val r = User.mapToUser(u.get)
println("information for " + r.name + " with id " + r.id)
case class User(id: String, name: String)
object User {
implicit def userToMap(user: User): Map[String, String] = {
Map("id" -> user.id, "name" -> user.name)
}
implicit def mapToUser(m: Map[String, String]) : User = {
User(m.get("id").get, m.get("name").get)
}
def unapply(arg: Map[String,String]): User = User(arg.get("id").get, arg.get("name").get)
}
Scala understands well on how to use the first implicit conversion of User
to Map[String,String]
has I don't have to call userToMap
but don't understand why it's failing doing the conversion like for example
var r = u.get.asInstanceOf[User]
. It says it can't cast the value to User - I tried .to[User]
also and that says
Error:(73, 24) User takes no type parameters, expected: one val r = u.get.to[User]
Full code:
import scredis._
import scala.concurrent.Future
import scala.util.{Failure, Success}
case class User(id: String, name: String)
object User {
implicit def userToMap(user: User): Map[String, String] = {
Map("id" -> user.id, "name" -> user.name)
}
implicit def mapToUser(m: Map[String, String]) : User = {
User(m.get("id").get, m.get("name").get)
}
def unapply(arg: Map[String,String]): User = User(arg.get("id").get, arg.get("name").get)
}
object RedisClient {
val redis = new Redis(host="192.168.122.2", passwordOpt = Some("privatemachine"))
import redis.dispatcher
def save(key:String, x : User) : Unit = {
x.foreach {
f => redis.hSet(key, f._1, f._2).onComplete{
case Success(content) => None
case Failure(e) => e.printStackTrace()
}
}
}
def get(key:String) : Future[Option[Map[String,String]]] = {
val result = redis.hGetAll(key)
result.onComplete {
case Success(content) => {
println(" --> " + content)
}
case Failure(e) => e.printStackTrace()
}
result
}
}
object Run extends App {
val redis = new Redis(host="192.168.122.2", passwordOpt = Some("privatemachine"))
import redis.dispatcher
redis.hSet("my-hash", "maker", "BMW")
redis.hGetAll("my-hash") onComplete {
case Success(content) => {
println(content)
}
case Failure(e) => e.printStackTrace()
}
val u1 = User("1", "andre")
RedisClient.save("user_1", u1)
val userResult = RedisClient.get("user_1")
userResult.map {
u =>
//val r = User.mapToUser(u.get)
val r = u.get.to[User]
println("information for " + r.name + " with id " + r.id)
}.onFailure{ case x => println("Look here : " + x )}
}