How to refactor so I can return different types in my case statement and then execute the doobie queries

71 Views Asked by At

Based on the match condition I will need to execute a different doobie query, and these queries may return different Right types in their Eithers.

The problem currently is that sometimes the Right of my either returns SomeType1 and in another match branch in returns SomeType2.

Note: I actually don't care about the return type of this function, currently it is List[User] but I can make it Unit if it helps.

def blah(locationId: Int): ConnectionIO[Either[String, List[User]]] = 
(for {
    usersToUpdate <- EitherT.fromEither[ConnectionIO] (
       UserDao.getUsersByLocation(locationId) match {
           case Nil => 
        Left("...")
           case first :: _ => 
        SomeDao.blah2(..) // ConnectionIO[Either[String, SomeType1]]
           case ... => 
        SomeDao.blah(..) // ConnectionIO[Either[String, SomeType2]]

          }
       }
    )
   updatedUsers <- usersToUpdate.traverse { EitherT(_) }
} yield updatedUsers).value

Is it possible to get this to work or I have to figure out a different way to do this logic?

0

There are 0 best solutions below