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
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
44 changes: 44 additions & 0 deletions core/js/src/main/scala/zio/SystemPlatformSpecific.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017-2024 John A. De Goes and the ZIO Contributors
*
* 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 zio

import zio.internal.stacktracer.Tracer
import zio.stacktracer.TracingImplicits.disableAutoTrace

import scala.collection.mutable
import scala.scalajs.js
import scala.scalajs.js.Dynamic.global

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

private[zio] val environmentProvider = new EnvironmentProvider {
private val envMap: mutable.Map[String, String] = {
if (js.typeOf(global.process) != "undefined" && js.typeOf(global.process.env) != "undefined") {
global.process.env.asInstanceOf[js.Dictionary[String]]
} else {
mutable.Map.empty
}
}

override def env(variable: String): Option[String] =
envMap.get(variable)

override def envs: Map[String, String] =
envMap.toMap
}

}
37 changes: 37 additions & 0 deletions core/jvm-native/src/main/scala/zio/SystemPlatformSpecific.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2017-2024 John A. De Goes and the ZIO Contributors
*
* 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 zio

import zio.internal.stacktracer.Tracer
import zio.stacktracer.TracingImplicits.disableAutoTrace

import java.lang.{System => JSystem}
import scala.annotation.nowarn
import scala.collection.JavaConverters._

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

private[zio] val environmentProvider = new EnvironmentProvider {
override def env(variable: String): Option[String] =
Option(JSystem.getenv(variable))

@nowarn("msg=JavaConverters")
override def envs: Map[String, String] =
JSystem.getenv().asScala.toMap
}

}
12 changes: 8 additions & 4 deletions core/shared/src/main/scala/zio/System.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ trait System extends Serializable { self =>
}
}

object System extends Serializable {
object System extends SystemPlatformSpecific {

val tag: Tag[System] = Tag[System]

Expand Down Expand Up @@ -140,17 +140,16 @@ object System extends Serializable {
@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)

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
Expand All @@ -172,6 +171,11 @@ object System extends Serializable {
}
}

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)

Expand Down
Loading
0