Skip to content

Commit

Permalink
fixes #1180: Type-class instances for zio.Config (#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimY4 authored Nov 11, 2024
1 parent b4669b5 commit c4d7312
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion core/shared/src/main/scala/zio/prelude/Associative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]`.
Expand Down
9 changes: 9 additions & 0 deletions core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand Down
12 changes: 11 additions & 1 deletion core/shared/src/main/scala/zio/prelude/Invariant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
*/
Expand Down

0 comments on commit c4d7312

Please sign in to comment.