Skip to content

Commit

Permalink
Add RandomId and RandomIdOf (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz-bielski authored Aug 12, 2024
1 parent 8bc2c79 commit 4c5ceb9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/src/main/scala/com/evolutiongaming/catshelper/RandomId.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.evolutiongaming.catshelper

import cats.kernel.Eq
import cats.{Order, Show}

final case class RandomId(value: String) extends AnyVal {
override def toString = value
}

object RandomId {
implicit val eqRandomId: Eq[RandomId] = Eq.fromUniversalEquals

implicit val showRandomId: Show[RandomId] = Show.fromToString

implicit val orderingRandomId: Ordering[RandomId] = Ordering.by(_.value )

implicit val orderRandomId: Order[RandomId] = Order.fromOrdering
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.evolutiongaming.catshelper

import java.util.UUID

import cats.effect.Sync

trait RandomIdOf[F[_]] {
def apply: F[RandomId]
}

object RandomIdOf {

def apply[F[_]](implicit fa: RandomIdOf[F]): RandomIdOf[F] = fa

def uuid[F[_] : Sync]: RandomIdOf[F] = new RandomIdOf[F] {

def apply = Sync[F].delay { RandomId(UUID.randomUUID().toString) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.evolutiongaming.catshelper

import cats.effect.IO
import com.evolutiongaming.catshelper.IOSuite._
import org.scalatest.funsuite.AsyncFunSuite
import org.scalatest.matchers.should.Matchers

class RandomIdSpec extends AsyncFunSuite with Matchers {
test("uuid apply") {
RandomIdOf
.uuid[IO]
.apply
.map { _.value should not be empty }
.run()
}
}

0 comments on commit 4c5ceb9

Please sign in to comment.