zio / zio-config

Easily use and document any config from anywhere in ZIO apps
https://zio.dev/zio-config
Apache License 2.0
233 stars 113 forks source link

Need to support ZLayer injection properly. #1040

Open zhiwilliam opened 1 year ago

zhiwilliam commented 1 year ago

Hi, I used zio-config to create my app. This was working fine: TypesafeConfigSource.fromHoconString(hocon).toLayer >>> configLayer(descriptor[MyConfig])

Then I am thinking that hocon string should come from some file. And I do have a read file layer. But it doesn't work. val fileConfigLayer: ZLayer[Any, ReadError[K], MyConfig] = ZLayer.fromZIO(readResourceFileLines("myconfig.conf").foldZIO(e => { val convert2ReadError: ZIO[Any, ReadError[K], ConfigSource] = ZIO.fail(ReadError.SourceError(e.getMessage)) convert2ReadError }, x => { val readConfig: ZIO[Any, ReadError[K], ConfigSource] = TypesafeConfigSource.fromHoconString(x).strictlyOnce readConfig }).provide(Scope.default)) >>> configLayer(descriptor[MyConfig])

I checked, they are exactly same type. But the one with file will throw: Could not find ConfigSourceModule::ConfigSource inside ZEnvironment(package::ConfigSource -> Reader(Set(ConfigSourceName(hocon)),Sync(zio.config.ConfigSourceModule.ConfigSource.strictlyOnce(ConfigSourceModule.scala:91),zio.config.ConfigSourceModule$ConfigSource$$Lambda$1051/0x0000000800fa5840@23fd4a8d)))

I think it might be a bug. Please help.

zhiwilliam commented 1 year ago

val readResourceFileLines = (filePath: String) => ZIO.fromAutoCloseable(ZIO.attemptBlockingIO(Source.fromResource(filePath))).map(.getLines()).map(.mkString("\n"))

afsalthaj commented 1 year ago

This will all be handled in the big PR #1035 (hopefully)