From b957602110fb0c3a7eba56dcfec5074f4a5ec590 Mon Sep 17 00:00:00 2001 From: Grzegorz Bielski Date: Mon, 12 Aug 2024 11:34:22 +0200 Subject: [PATCH] Add RandomId and RandomIdOf (#288) * Add RandomId helper * Remove unused imports * fix missing .flatten method in TestFrameworkApi --- .../evolutiongaming/catshelper/RandomId.scala | 18 ++++++++++++++++++ .../catshelper/RandomIdOf.scala | 19 +++++++++++++++++++ .../catshelper/CatsHelperSpec.scala | 1 - .../catshelper/RandomIdSpec.scala | 16 ++++++++++++++++ .../ResourceBreakFlatMapChainTest.scala | 1 - 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 core/src/main/scala/com/evolutiongaming/catshelper/RandomId.scala create mode 100644 core/src/main/scala/com/evolutiongaming/catshelper/RandomIdOf.scala create mode 100644 core/src/test/scala/com/evolutiongaming/catshelper/RandomIdSpec.scala diff --git a/core/src/main/scala/com/evolutiongaming/catshelper/RandomId.scala b/core/src/main/scala/com/evolutiongaming/catshelper/RandomId.scala new file mode 100644 index 00000000..b7b621ea --- /dev/null +++ b/core/src/main/scala/com/evolutiongaming/catshelper/RandomId.scala @@ -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 +} diff --git a/core/src/main/scala/com/evolutiongaming/catshelper/RandomIdOf.scala b/core/src/main/scala/com/evolutiongaming/catshelper/RandomIdOf.scala new file mode 100644 index 00000000..01e8eea1 --- /dev/null +++ b/core/src/main/scala/com/evolutiongaming/catshelper/RandomIdOf.scala @@ -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) } + } +} diff --git a/core/src/test/scala/com/evolutiongaming/catshelper/CatsHelperSpec.scala b/core/src/test/scala/com/evolutiongaming/catshelper/CatsHelperSpec.scala index 4dc3c6fb..7b7ea901 100644 --- a/core/src/test/scala/com/evolutiongaming/catshelper/CatsHelperSpec.scala +++ b/core/src/test/scala/com/evolutiongaming/catshelper/CatsHelperSpec.scala @@ -1,6 +1,5 @@ package com.evolutiongaming.catshelper -import cats.effect.implicits._ import cats.effect.kernel.Ref import cats.effect.unsafe.IORuntime import cats.effect.{IO, Resource} diff --git a/core/src/test/scala/com/evolutiongaming/catshelper/RandomIdSpec.scala b/core/src/test/scala/com/evolutiongaming/catshelper/RandomIdSpec.scala new file mode 100644 index 00000000..13d98dd9 --- /dev/null +++ b/core/src/test/scala/com/evolutiongaming/catshelper/RandomIdSpec.scala @@ -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() + } +} diff --git a/core/src/test/scala/com/evolutiongaming/catshelper/ResourceBreakFlatMapChainTest.scala b/core/src/test/scala/com/evolutiongaming/catshelper/ResourceBreakFlatMapChainTest.scala index b59b5163..1fec5714 100644 --- a/core/src/test/scala/com/evolutiongaming/catshelper/ResourceBreakFlatMapChainTest.scala +++ b/core/src/test/scala/com/evolutiongaming/catshelper/ResourceBreakFlatMapChainTest.scala @@ -3,7 +3,6 @@ package com.evolutiongaming.catshelper import cats.effect.kernel.Deferred import cats.effect.{IO, Resource} import cats.implicits._ -import cats.effect.implicits._ import com.evolutiongaming.catshelper.CatsHelper._ import com.evolutiongaming.catshelper.IOSuite._ import org.scalatest.funsuite.AsyncFunSuite