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

Explain how to use this in a ZIO-Application #1411

Closed
domdorn opened this issue May 16, 2024 · 1 comment
Closed

Explain how to use this in a ZIO-Application #1411

domdorn opened this issue May 16, 2024 · 1 comment

Comments

@domdorn
Copy link

domdorn commented May 16, 2024

I'm having a hard time getting zio-config to run in my application...
all the examples focus around mapping to custom data types, while I just want to use the ZIO.config(...) way to access a registered config..

Please see this simplified test to see what I want to achieve as a minimum.

import com.typesafe.config
import zio.Config
import zio.ConfigProvider
import zio.Scope
import zio.ZIO
import zio.config.typesafe.FromConfigSourceTypesafe
import zio.test.Spec
import zio.test.TestAspect
import zio.test.TestEnvironment
import zio.test.ZIOSpecDefault

object ConfigSpec extends ZIOSpecDefault {

  override def spec: Spec[TestEnvironment with Scope, Any] = suite("ConfigSpec")(
    test("test1") {
      for {
        _ <- zio.ZIO.unit
        value <- ZIO.config(Config.string("myService.group.variable"))
      } yield zio.test.assertTrue(value == "value")
    }
  ) @@ TestAspect.withConfigProvider({
    // expect here a externally provided typesafe config object
    val c: config.Config = com.typesafe.config.ConfigFactory.parseString(
      s"""
         |myService {
         |  group {
         |    variable = "value"
         |  }
         |
         |}
         |
         |""".stripMargin)
    println(c)
    val cp = ConfigProvider.fromTypesafeConfig(c, false)

    cp
  })
}

Thanks,
Dominik

@domdorn
Copy link
Author

domdorn commented May 17, 2024

nevermind.. got it working.. key was using the Nested class, like this:

userAgent <- ZLayer.fromZIO(ZIO.config(Config.Nested("akka", Config.Nested("http", Config.Nested("client", Config.string("user-agent-header"))))))

later I figured out, I can use it like this as well:

userAgent <- ZIO.config(Config.string("user-agent-header").nested("akka", "http", "client"))

@domdorn domdorn closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant