-1

I have a class ClassName(id:String, etc:String, long:Long)

And a function that returns that object from the DB as Future[Option[ClassName]]

I want to get transform that result (the Future[Option]) as, either a class with it's values, or a Map[String, AnyRef]

Does anyone knows how to handle this transform? I haven't been finding any answers on the internet

Javier Bullrich
  • 389
  • 1
  • 5
  • 22
  • Related? http://stackoverflow.com/questions/17713642/accessing-value-returned-by-scala-futures – Dan W Apr 06 '17 at 18:36
  • What is the bigger picture of what you are to do? – Tyler Apr 06 '17 at 18:41
  • @Tyler I want to extract a value from a database using Akka HTTP so I give it to the user. It's a project based on this one: https://github.com/ArchDev/akka-http-rest – Javier Bullrich Apr 06 '17 at 19:02
  • @JavierBullrich can you be more specific as to what "give it to the user" means? Are you responding to an HTTP request, displaying to a desktop giu or something else? Also, what exactly is the thing that you are "giving" to the user? – puhlen Apr 07 '17 at 14:41

1 Answers1

-2

I manage to create this function which returns me the class that is inside the future.

It's not generic, so it won't be very elegant, but it does the job

def convertToClass(futureEntity: Future[Option[ClassName]]): ClassName = { var newClass: ClassName = null futureEntity onComplete { case Success(value) => value match { case Some(s) => newClass = s case None => null } case Failure(e) => e.printStackTrace null } newClass }

EDIT: That method wasn't working. This one does:

def convertToMyClass(futureEntity: Future[Option[ClassName]]): ClassName = {
    var myClass: ClassName = null
    Await.ready(futureEntity andThen {
      case Success(value) =>
        value match {
          case Some(s) => myClass = s
          case None => null
        }
      case Failure(e) => e.printStackTrace
        null
    }, Duration.Inf)
    myClass
  }
Javier Bullrich
  • 389
  • 1
  • 5
  • 22