scalascala-catshttp4srefined

How to convert into refined type?


I am using the library https://github.com/fthomas/refined and would like to convert java.util.UUID to refined's Uuid.
How to convert java.util.UUID to refined's Uuid?

Update

I have the following http routes:

  private val httpRoutes: HttpRoutes[F] = HttpRoutes.of[F] {
    case GET -> Root / UUIDVar(id) =>
      program.read(id)

and the read function is defined as follows:

  def read(id: Uuid): F[User] =
    query
      .read(id)
      .flatMap {
        case Some(user) =>
          Applicative[F].pure(user)
        case None =>
          ApplicativeError[F, UserError].raiseError[User](UserNotRegistered)
      }

The compiler complains:

type mismatch;
[error]  found   : java.util.UUID
[error]  required: eu.timepit.refined.string.Uuid
[error]       program.read(id)
[error]         

       ^

Solution

  • Here is transformation java.util.UUID into eu.timepit.refined.api.Refined[String, eu.timepit.refined.string.Uuid]

    import java.util.UUID    
    import eu.timepit.refined.string.Uuid
    import eu.timepit.refined.api.Refined
    
    val uuid: UUID = UUID.fromString("deea44c7-a180-4898-9527-58db0ed34683")
    
    val uuid1: String Refined Uuid = Refined.unsafeApply[String, Uuid](uuid.toString)