3

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]         

       ^
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

5

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)
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • How can I use it related my example with `UUIDVar(id)`? – softshipper Jun 29 '20 at 20:06
  • @zero_coding Let's start with your signature. `def read(id: Uuid)...` is strange. `eu.timepit.refined.string.Uuid` is a predicate, not data type. Data types are `java.util.UUID`, `String` or `Refined[String, Uuid]` (aka `String Refined Uuid`). Where did you get signature `def read(id: Uuid)...`? Should it be `def read(id: String Refined Uuid)...`? – Dmytro Mitin Jun 29 '20 at 20:10
  • Where can I find the tutorial about refined? I thought that `Uuid` is a type. – softshipper Jun 29 '20 at 20:13
  • 1
    @zero_coding it's a type, but it's predicate, not string refined with predicate. – Dmytro Mitin Jun 29 '20 at 20:21
  • 1
    @zero_coding For example at the very beginning of the tutorial it's written `val i1: Int Refined Positive = 5`. You do not write `val i1: Positive = 5`. – Dmytro Mitin Jun 29 '20 at 20:24
  • 1
    @zero_coding So if correct signature is `def read(id: String Refined Uuid): F[User]` then you can do `program.read(Refined.unsafeApply[String, Uuid](id.toString))`. – Dmytro Mitin Jun 29 '20 at 20:26