Refactoring my for comprehension to remove the unsafe get calls on the option values

69 Views Asked by At

So my for comprehension technically compiles fine currently, but if you look carefully I have a few unsafe .get calls on the options.

for {
   maybeProduct: Option[Product] <- EitherT.liftF {
        for {
           a <- ADao.get(123).transact(xa)  // ConnectionIO[Option[A]]
           b <- BDao.get(a.get).transact(xa)      // ConnectionIO[Option[B]]
           product <- CDao.get(b.get.productId.get).transact(xa) // ConnectionIO[Option[Product]]
        } yield product
  }

  transaction <- EitherT(
        TransactionDao.insert(..., maybeProduct.map(_.id),...).transact(xa)                    
   )
} 

I need help refactoring it to remove the unsafe calls in the following lines in the for comprehension

           a <- ADao.get(123).transact(xa)  // ConnectionIO[Option[A]]
           b <- BDao.get(a.get).transact(xa)      // ConnectionIO[Option[B]]
           product <- CDao.get(b.get.productId.get).transact(xa) // ConnectionIO[Option[Product]]

Specifically the call to a.get and b.get.productId.get

0

There are 0 best solutions below