Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #1180: Type-class instances for zio.Config #1406

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading