Class

zio.config.syntax

ZIOConfigNarrowOps

Related Doc: package syntax

Permalink

implicit final class ZIOConfigNarrowOps[R, E, A] extends AnyVal

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ZIOConfigNarrowOps
  2. AnyVal
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new ZIOConfigNarrowOps(self: ZLayer[R, E, A])

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def getClass(): Class[_ <: AnyVal]

    Permalink
    Definition Classes
    AnyVal → Any
  6. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  7. def narrow[B](f: (A) ⇒ B)(implicit arg0: Tag[B], ta: Tag[A]): ZLayer[R, E, B]

    Permalink

    In bigger apps you can have a lot of components and, consequently - a lot of configuration fields.

    In bigger apps you can have a lot of components and, consequently - a lot of configuration fields. It's not ideal to pass around the whole configuration object as a dependency for all of those components: this way you break the separation of concerns principle. Component should be aware only about dependencies it cares about and uses somehow.

    So to avoid that and do it in an ergonomic way, there's a narrow syntax extension for ZLayer, available under import zio.config.syntax._. In the below example, we use both zio-config-magnolia and zio-config-typesafe module inorder to have automatic derivation as well as read/write HOCON structure.

    import zio._
    import zio.config._, typesafe._, syntax._, magnolia.DeriveConfigDescriptor
    
    trait Endpoint
    trait Repository
    
    case class AppConfig(api: ApiConfig, db: DbConfig)
    case class DbConfig (url: String,    driver: String)
    case class ApiConfig(host: String,   port: Int)
    
    val configDescription = DeriveConfigDescriptor.descriptor[AppConfig]
    
    // components have only required dependencies
    val endpoint: ZLayer[Has[ApiConfig], Nothing, Has[Endpoint]]    = ZLayer.fromService(_ => new Endpoint {})
    val repository: ZLayer[Has[DbConfig], Nothing, Has[Repository]] = ZLayer.fromService(_ => new Repository {})
    
    val cfg = TypesafeConfig.fromResourcePath(configDescription)
    
    cfg.narrow(_.api) >>> endpoint // narrowing down to a proper config subtree
    cfg.narrow(_.db) >>> repository
  8. def toString(): String

    Permalink
    Definition Classes
    Any

Inherited from AnyVal

Inherited from Any

Ungrouped