From c4d7312b51674d1d4d995f5174e31c25524db061 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 12 Nov 2024 01:30:49 +1000 Subject: [PATCH] fixes #1180: Type-class instances for zio.Config (#1406) --- .../src/main/scala/zio/prelude/Associative.scala | 8 +++++++- .../src/main/scala/zio/prelude/AssociativeBoth.scala | 9 +++++++++ .../main/scala/zio/prelude/AssociativeEither.scala | 9 +++++++++ .../src/main/scala/zio/prelude/Invariant.scala | 12 +++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/core/shared/src/main/scala/zio/prelude/Associative.scala b/core/shared/src/main/scala/zio/prelude/Associative.scala index ababf33f2..a7da1f463 100644 --- a/core/shared/src/main/scala/zio/prelude/Associative.scala +++ b/core/shared/src/main/scala/zio/prelude/Associative.scala @@ -17,7 +17,7 @@ package zio.prelude import zio.prelude.newtypes.{And, AndF, AndThen, Both, First, Last, Max, Min, Natural, Or, OrF, Prod, Sum} -import zio.{Cause, Chunk, Duration => ZIODuration, NonEmptyChunk} +import zio.{Cause, Chunk, ConfigProvider, Duration => ZIODuration, NonEmptyChunk} import scala.annotation.tailrec @@ -352,6 +352,12 @@ object Associative extends AssociativeLowPriority { implicit def ChunkIdentity[A]: Identity[Chunk[A]] = Identity.make(Chunk.empty, _ ++ _) + /** + * The `Associative` instance for `ConfigProvider`. + */ + implicit val ConfigProviderAssociative: Associative[ConfigProvider] = + Associative.make(_.orElse(_)) + /** * Derives an `Associative[F[A]]` given a `Derive[F, Associative]` and an * `Associative[A]`. diff --git a/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala b/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala index e4a03793d..d4d7e46a4 100644 --- a/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala +++ b/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala @@ -1074,6 +1074,15 @@ object AssociativeBoth extends AssociativeBothLowPriority { def both[A, B](fa: => Chunk[A], fb: => Chunk[B]): Chunk[(A, B)] = fa.flatMap(a => fb.map(b => (a, b))) } + /** + * The `IdentityBoth` instance for `Config`. + */ + implicit val ConfigIdentityBoth: IdentityBoth[Config] = + new IdentityBoth[Config] { + val any: Config[Any] = Config.succeed(()) + def both[A, B](fa: => Config[A], fb: => Config[B]): Config[(A, B)] = fa ++ fb + } + /** * The `AssociativeBoth` instance for `Const`. */ diff --git a/core/shared/src/main/scala/zio/prelude/AssociativeEither.scala b/core/shared/src/main/scala/zio/prelude/AssociativeEither.scala index 119a57c46..1bb956e4d 100644 --- a/core/shared/src/main/scala/zio/prelude/AssociativeEither.scala +++ b/core/shared/src/main/scala/zio/prelude/AssociativeEither.scala @@ -57,6 +57,15 @@ object AssociativeEither { Chunk.empty } + /** + * The `AssociativeEither` instance for `Config`. + */ + implicit val ConfigAssociativeEither: AssociativeEither[Config] = + new AssociativeEither[Config] { + def either[A, B](fa: => Config[A], fb: => Config[B]): Config[Either[A, B]] = + fa.map(Left(_)).orElse(fb.map(Right(_))) + } + /** * The `AssociativeEither` instance for `Either`. */ diff --git a/core/shared/src/main/scala/zio/prelude/Invariant.scala b/core/shared/src/main/scala/zio/prelude/Invariant.scala index 530087664..baa8623f7 100644 --- a/core/shared/src/main/scala/zio/prelude/Invariant.scala +++ b/core/shared/src/main/scala/zio/prelude/Invariant.scala @@ -16,11 +16,11 @@ package zio.prelude +import zio._ import zio.prelude.coherent.CovariantIdentityBoth import zio.prelude.newtypes.Failure import zio.stm.ZSTM import zio.stream.{ZSink, ZStream} -import zio.{Cause, Chunk, ChunkBuilder, Exit, Fiber, NonEmptyChunk, Schedule, ZIO} import scala.concurrent.{ExecutionContext, Future} import scala.util.Try @@ -98,6 +98,16 @@ object Invariant extends LowPriorityInvariantImplicits with InvariantVersionSpec ) } + /** + * The `Covariant` instance for `Config`. + */ + implicit def ConfigCovariant[A]: Covariant[Config] = + new Covariant[Config] { + override def map[A, B](f: A => B): Config[A] => Config[B] = { config => + config.map(f) + } + } + /** * The `ForEach` instance for `Const`. */