-
Notifications
You must be signed in to change notification settings - Fork 520
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
massimosiani
wants to merge
17
commits into
typelevel:series/3.x
Choose a base branch
from
massimosiani:feature/prop
base: series/3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Feature/prop #3924
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
72704b1
write the Prop interface
massimosiani 4b8144a
sbt prePR
massimosiani 0385959
add more tests
massimosiani 030915f
randomize the test key
massimosiani fe589e6
remove non-atomic functions
massimosiani cbad5e5
use an immutable map
massimosiani dd20429
Merge remote-tracking branch 'upstream/series/3.x' into feature/prop
armanbilge 49fa3b2
Update headers
armanbilge 1e2bf07
Fix compile on 2.12
armanbilge 967ca63
Use `blocking` when setting a property
armanbilge f570b7e
Make `entries` atomic
armanbilge 923c0d5
compare the same types
massimosiani 9436fd6
Fix deprecations in test
armanbilge 4a71b85
`Prop` -> `SystemProperties`
armanbilge 638a1fb
Formatting
armanbilge 51c219f
Merge remote-tracking branch 'upstream/series/3.x' into feature/prop
armanbilge 99de034
`SystemProperties` is a `MapRef`
armanbilge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
std/shared/src/main/scala/cats/effect/std/SystemProperties.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. | ||
*/ | ||
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) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/shared/src/test/scala/cats/effect/std/SystemPropertiesSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.