Skip to content

Commit

Permalink
Prop -> SystemProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Aug 8, 2024
1 parent 9436fd6 commit 4a71b85
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import cats.data.Ior
import cats.effect.instances.spawn
import cats.effect.kernel.CancelScope
import cats.effect.kernel.GenTemporal.handleDuration
import cats.effect.std.{Backpressure, Console, Env, Prop, Supervisor, UUIDGen}
import cats.effect.std.{Backpressure, Console, Env, Supervisor, SystemProperties, UUIDGen}
import cats.effect.tracing.{Tracing, TracingEvent}
import cats.effect.unsafe.IORuntime
import cats.syntax._
Expand Down Expand Up @@ -2108,7 +2108,7 @@ object IO extends IOCompanionPlatform with IOLowPriorityImplicits with TuplePara

implicit val envForIO: Env[IO] = Env.make

implicit val propForIO: Prop[IO] = Prop.make
implicit val systemPropertiesForIO: SystemProperties[IO] = SystemProperties.make

// This is cached as a val to save allocations, but it uses ops from the Async
// instance which is also cached as a val, and therefore needs to appear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.typelevel.scalaccompat.annotation._

import scala.collection.immutable.Map

trait Prop[F[_]] { self =>
trait SystemProperties[F[_]] { self =>

/**
* Retrieves the value for the specified key.
Expand All @@ -45,84 +45,84 @@ trait Prop[F[_]] { self =>

def entries: F[Map[String, String]]

def mapK[G[_]](f: F ~> G): Prop[G] = new Prop[G] {
def mapK[G[_]](f: F ~> G): SystemProperties[G] = new SystemProperties[G] {
def get(key: String): G[Option[String]] = f(self.get(key))
def set(key: String, value: String): G[Unit] = f(self.set(key, value))
def unset(key: String) = f(self.unset(key))
def entries: G[Map[String, String]] = f(self.entries)
}
}

object Prop {
object SystemProperties {

/**
* Summoner method for `Prop` instances.
* Summoner method for `SystemProperties` instances.
*/
def apply[F[_]](implicit ev: Prop[F]): ev.type = ev
def apply[F[_]](implicit ev: SystemProperties[F]): ev.type = ev

/**
* Constructs a `Prop` instance for `F` data types that are [[cats.effect.kernel.Sync]].
* Constructs a `SystemProperties` instance for `F` data types that are [[cats.effect.kernel.Sync]].
*/
def make[F[_]](implicit F: Sync[F]): Prop[F] = new SyncProp[F]
def make[F[_]](implicit F: Sync[F]): SystemProperties[F] = new SyncSystemProperties[F]

/**
* [[Prop]] instance built for `cats.data.EitherT` values initialized with any `F` data type
* that also implements `Prop`.
*/
implicit def catsEitherTProp[F[_]: Prop: Functor, L]: Prop[EitherT[F, L, *]] =
Prop[F].mapK(EitherT.liftK)
implicit def catsEitherTSystemProperties[F[_]: SystemProperties: Functor, L]: SystemProperties[EitherT[F, L, *]] =
SystemProperties[F].mapK(EitherT.liftK)

/**
* [[Prop]] instance built for `cats.data.Kleisli` values initialized with any `F` data type
* that also implements `Prop`.
*/
implicit def catsKleisliProp[F[_]: Prop, R]: Prop[Kleisli[F, R, *]] =
Prop[F].mapK(Kleisli.liftK)
implicit def catsKleisliSystemProperties[F[_]: SystemProperties, R]: SystemProperties[Kleisli[F, R, *]] =
SystemProperties[F].mapK(Kleisli.liftK)

/**
* [[Prop]] instance built for `cats.data.OptionT` values initialized with any `F` data type
* that also implements `Prop`.
*/
implicit def catsOptionTProp[F[_]: Prop: Functor]: Prop[OptionT[F, *]] =
Prop[F].mapK(OptionT.liftK)
implicit def catsOptionTSystemProperties[F[_]: SystemProperties: Functor]: SystemProperties[OptionT[F, *]] =
SystemProperties[F].mapK(OptionT.liftK)

/**
* [[Prop]] instance built for `cats.data.StateT` values initialized with any `F` data type
* that also implements `Prop`.
*/
implicit def catsStateTProp[F[_]: Prop: Applicative, S]: Prop[StateT[F, S, *]] =
Prop[F].mapK(StateT.liftK)
implicit def catsStateTSystemProperties[F[_]: SystemProperties: Applicative, S]: SystemProperties[StateT[F, S, *]] =
SystemProperties[F].mapK(StateT.liftK)

/**
* [[Prop]] instance built for `cats.data.WriterT` values initialized with any `F` data type
* that also implements `Prop`.
*/
implicit def catsWriterTProp[
F[_]: Prop: Applicative,
implicit def catsWriterTSystemProperties[
F[_]: SystemProperties: Applicative,
L: Monoid
]: Prop[WriterT[F, L, *]] =
Prop[F].mapK(WriterT.liftK)
]: SystemProperties[WriterT[F, L, *]] =
SystemProperties[F].mapK(WriterT.liftK)

/**
* [[Prop]] instance built for `cats.data.IorT` values initialized with any `F` data type that
* also implements `Prop`.
*/
implicit def catsIorTProp[F[_]: Prop: Functor, L]: Prop[IorT[F, L, *]] =
Prop[F].mapK(IorT.liftK)
implicit def catsIorTSystemProperties[F[_]: SystemProperties: Functor, L]: SystemProperties[IorT[F, L, *]] =
SystemProperties[F].mapK(IorT.liftK)

/**
* [[Prop]] instance built for `cats.data.ReaderWriterStateT` values initialized with any `F`
* data type that also implements `Prop`.
*/
implicit def catsReaderWriterStateTProp[
F[_]: Prop: Applicative,
implicit def catsReaderWriterStateTSystemProperties[
F[_]: SystemProperties: Applicative,
E,
L: Monoid,
S
]: Prop[ReaderWriterStateT[F, E, L, S, *]] =
Prop[F].mapK(ReaderWriterStateT.liftK)
]: SystemProperties[ReaderWriterStateT[F, E, L, S, *]] =
SystemProperties[F].mapK(ReaderWriterStateT.liftK)

private[std] final class SyncProp[F[_]](implicit F: Sync[F]) extends Prop[F] {
private[std] final class SyncSystemProperties[F[_]](implicit F: Sync[F]) extends SystemProperties[F] {

def get(key: String): F[Option[String]] =
F.delay(Option(System.getProperty(key))) // thread-safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@
package cats.effect
package std

class PropSpec extends BaseSpec {
import org.typelevel.scalaccompat.annotation._

"Prop" should {
class SystemPropertiesSpec extends BaseSpec {

"SystemProperties" should {
"retrieve a property just set" in real {
Random.javaUtilConcurrentThreadLocalRandom[IO].nextString(12).flatMap { key =>
Prop[IO].set(key, "bar") *> Prop[IO].get(key).flatMap(x => IO(x mustEqual Some("bar")))
SystemProperties[IO].set(key, "bar") *>
SystemProperties[IO].get(key).flatMap(x => IO(x mustEqual Some("bar")))
}
}
"return none for a non-existent property" in real {
Prop[IO].get("MADE_THIS_UP").flatMap(x => IO(x must beNone))
SystemProperties[IO].get("MADE_THIS_UP").flatMap(x => IO(x must beNone))
}
"unset" in real {
Random.javaUtilConcurrentThreadLocalRandom[IO].nextString(12).flatMap { key =>
Prop[IO].set(key, "bar") *> Prop[IO]
.unset(key) *> Prop[IO].get(key).flatMap(x => IO(x must beNone))
SystemProperties[IO].set(key, "bar") *> SystemProperties[IO].unset(key) *>
SystemProperties[IO].get(key).flatMap(x => IO(x must beNone))
}
}
"retrieve the system properties" in real {
for {
_ <- Prop[IO].set("some property", "the value")
props <- Prop[IO].entries
_ <- SystemProperties[IO].set("some property", "the value")
props <- SystemProperties[IO].entries
expected <- IO {
import scala.collection.JavaConverters._
Map.empty ++ System.getProperties.asScala
Expand Down

0 comments on commit 4a71b85

Please sign in to comment.