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

Feature/prop #3924

Draft
wants to merge 17 commits into
base: series/3.x
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion 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, 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 @@ -2159,6 +2159,8 @@ object IO extends IOCompanionPlatform with IOLowPriorityImplicits with TuplePara

implicit val envForIO: Env[IO] = Env.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
// later in the file
Expand Down
14 changes: 11 additions & 3 deletions std/shared/src/main/scala/cats/effect/std/MapRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cats.data._
import cats.effect.kernel._
import cats.syntax.all._

import java.util.Hashtable
import java.util.concurrent.ConcurrentHashMap

/**
Expand All @@ -34,12 +35,15 @@ import java.util.concurrent.ConcurrentHashMap
* concurrent updates may be executed independently to each other, as long as their keys belong
* to different shards.
*/
trait MapRef[F[_], K, V] extends Function1[K, Ref[F, V]] {
trait MapRef[F[_], K, V] extends Function1[K, Ref[F, V]] { outer =>

/**
* Access the reference for this Key
*/
def apply(k: K): Ref[F, V]

def mapK[G[_]](f: F ~> G)(implicit G: Functor[G]): MapRef[G, K, V] =
outer(_).mapK(f)
}

object MapRef extends MapRefCompanionPlatform {
Expand Down Expand Up @@ -123,7 +127,7 @@ object MapRef extends MapRefCompanionPlatform {
ref: Ref[F, Map[K, V]]): MapRef[F, K, Option[V]] =
k => Ref.lens(ref)(_.get(k), m => _.fold(m - k)(v => m + (k -> v)))

private class ConcurrentHashMapImpl[F[_], K, V](chm: ConcurrentHashMap[K, V], sync: Sync[F])
private class JavaMapImpl[F[_], K, V](chm: java.util.Map[K, V], sync: Sync[F])
extends MapRef[F, K, Option[V]] {
private implicit def syncF: Sync[F] = sync

Expand Down Expand Up @@ -241,7 +245,11 @@ object MapRef extends MapRefCompanionPlatform {
*/
def fromConcurrentHashMap[F[_]: Sync, K, V](
map: ConcurrentHashMap[K, V]): MapRef[F, K, Option[V]] =
new ConcurrentHashMapImpl[F, K, V](map, Sync[F])
new JavaMapImpl[F, K, V](map, Sync[F])

private[std] def fromHashtable[F[_]: Sync, K, V](
map: Hashtable[K, V]): MapRef[F, K, Option[V]] =
new JavaMapImpl[F, K, V](map, Sync[F])

/**
* This allocates mutable memory, so it has to be inside F. The way to use things like this is
Expand Down
117 changes: 117 additions & 0 deletions std/shared/src/main/scala/cats/effect/std/SystemProperties.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2020-2024 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.std

import cats.{~>, Applicative, Functor}
import cats.data.{EitherT, IorT, Kleisli, OptionT, ReaderWriterStateT, StateT, WriterT}
import cats.effect.kernel.{Ref, Sync}
import cats.kernel.Monoid

import java.util.Hashtable

trait SystemProperties[F[_]] extends MapRef[F, String, Option[String]] { outer =>

def apply(key: String): Ref[F, Option[String]]

override def mapK[G[_]](f: F ~> G)(implicit G: Functor[G]): SystemProperties[G] =
outer(_).mapK(f)
}

object SystemProperties {

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

/**
* Constructs a `SystemProperties` instance for `F` data types that are
* [[cats.effect.kernel.Sync]].
*/
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`.
Comment on lines +48 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* [[Prop]] instance built for `cats.data.EitherT` values initialized with any `F` data type
* that also implements `Prop`.
* [[SystemProperties]] instance built for `cats.data.EitherT` values initialized with any `F` data type
* that also implements `SystemProperties`.

*/
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 catsKleisliSystemProperties[F[_]: Functor: 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 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 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 catsWriterTSystemProperties[
F[_]: SystemProperties: Applicative,
L: Monoid
]: 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 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 catsReaderWriterStateTSystemProperties[
F[_]: SystemProperties: Applicative,
E,
L: Monoid,
S
]: SystemProperties[ReaderWriterStateT[F, E, L, S, *]] =
SystemProperties[F].mapK(ReaderWriterStateT.liftK)

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

private[this] val underlying =
MapRef.fromHashtable(System.getProperties().asInstanceOf[Hashtable[String, String]])

def apply(key: String) = underlying(key)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020-2024 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect
package std

import org.typelevel.scalaccompat.annotation._

class SystemPropertiesSpec extends BaseSpec {

"SystemProperties" should {
"retrieve a property just set" in real {
Random.javaUtilConcurrentThreadLocalRandom[IO].nextString(12).flatMap { key =>
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 {
SystemProperties[IO].get("MADE_THIS_UP").flatMap(x => IO(x must beNone))
}
"unset" in real {
Random.javaUtilConcurrentThreadLocalRandom[IO].nextString(12).flatMap { key =>
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 {
_ <- SystemProperties[IO].set("some property", "the value")
props <- SystemProperties[IO].entries
expected <- IO {
import scala.collection.JavaConverters._
Map.empty ++ System.getProperties.asScala
}: @nowarn213("cat=deprecation") @nowarn3("cat=deprecation")
assertion <- IO(props mustEqual expected)
} yield assertion
}
}
}
Loading