8000 Make environment variables access platform-specific by mhriemers · Pull Request #9282 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make environment variables access platform-specific #9282

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

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

mhriemers
Copy link
Contributor
@mhriemers mhriemers commented Nov 6, 2024

Fixes #9279

/claim #9279

@CLAassistant
Copy link
CLAassistant commented Nov 6, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor
@kyri-petrou kyri-petrou left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Some really minor comments and I'll merge it in once we resolve an issue with sonatype releases


private[zio] trait SystemPlatformSpecific { self: System.type =>

trait SystemLivePlatformSpecific extends System {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make this private[zio] as well


private[zio] trait SystemPlatformSpecific { self: System.type =>

trait SystemLivePlatformSpecific extends System {
Copy link
Contributor

Choose a reason for hiding this comment

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

As above

@mhriemers
Copy link
Contributor Author

@kyri-petrou

I'm considering whether implementing the entire UnsafeAPI in SystemPlatformSpecific is too broad and whether we should limit the platform-specific implementation to just the env and envs methods, keeping the rest of the UnsafeAPI in the shared code. This might make the implementation more focused and reduce duplication.

Additionally, regarding the system properties and lineSeparator implementations in Scala.js, I noticed that these are already handled by the Scala.js library itself:

Scala.js System.scala implementation

// Excerpt from Scala.js System.scala
def getProperties(): ju.Properties = SystemProperties.getProperties()
def lineSeparator(): String = "\n"
// Other methods...

These methods are maintained within the Scala.js standard library, providing compatibility with the Java API. Since they are already implemented and maintained by Scala.js, perhaps we can defer to these existing implementations instead of re-implementing them in our platform-specific code.

What do you think about tailoring the platform-specific code specifically towards environment variables and relying on the existing Scala.js implementations for system properties and line separators? This approach might help us avoid unnecessary duplication and leverage the maintenance work already done by the Scala.js team.

I'm open to your thoughts on this. Do you think it's better to focus the platform-specific code solely on environment variables, or should we keep the complete UnsafeAPI implementation within SystemPlatformSpecific?

Thanks!

@mhriemers
Copy link
Contributor Author

Something like this:

diff --git a/core/shared/src/main/scala/zio/System.scala b/core/shared/src/main/scala/zio/System.scala
--- a/core/shared/src/main/scala/zio/System.scala	(revision f092e25d1bfa01eb8dae4dc656ff8e623894ee3b)
+++ b/core/shared/src/main/scala/zio/System.scala	(date 1730986534534)
@@ -101,7 +101,7 @@
     }
 }
 
-object System extends Serializable {
+object System extends SystemPlatformSpecific {
 
   val tag: Tag[System] = Tag[System]
 
@@ -140,7 +140,7 @@
     @transient override val unsafe: UnsafeAPI =
       new UnsafeAPI {
         override def env(variable: String)(implicit unsafe: Unsafe): Option[String] =
-          Option(JSystem.getenv(variable))
+          environmentProvider.env(variable)
 
         override def envOrElse(variable: String, alt: => String)(implicit unsafe: Unsafe): String =
           envOrElseWith(variable, alt)(env)
@@ -148,9 +148,8 @@
         override def envOrOption(variable: String, alt: => Option[String])(implicit unsafe: Unsafe): Option[String] =
           envOrOptionWith(variable, alt)(env)
 
-        @nowarn("msg=JavaConverters")
         override def envs()(implicit unsafe: Unsafe): Map[String, String] =
-          JSystem.getenv.asScala.toMap
+          environmentProvider.envs
 
         override def lineSeparator()(implicit unsafe: Unsafe): String =
           JSystem.lineSeparator
@@ -172,6 +171,11 @@
       }
   }
 
+  private[zio] trait EnvironmentProvider {
+    def env(variable: String): Option[String]
+    def envs: Map[String, String]
+  }
+
   private[zio] def envOrElseWith(variable: String, alt: => String)(env: String => Option[String]): String =
     env(variable).getOrElse(alt)
 

@kyri-petrou
Copy link
Contributor

@mhriemers yeah I think the approach you proposed makes sense. It should also be bin-compatible so let's go with it 👍

@mhriemers
Copy link
Contributor Author

@kyri-petrou I prepared the commit yesterday, including your review comments.

Copy link
algora-pbc bot commented Nov 9, 2024

💵 To receive payouts, sign up on Algora, link your Github account and connect with Stripe.

Copy link
Contributor
@kyri-petrou kyri-petrou left a comment

Choose a reason for hiding this comment

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

LGTM, thanks 🙏

@kyri-petrou kyri-petrou merged commit 7c5046f into zio:series/2.x Nov 12, 2024
18 checks passed
joroKr21 pushed a commit to joroKr21/zio that referenced this pull request Nov 12, 2024
* Make environment variables access platform-specific

* More environment variable specific API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling System.env and related methods do not work on Scala.js
3 participants
0