|
| 1 | +package app.impl.zio |
| 2 | + |
| 3 | +import java.util.Properties |
| 4 | + |
| 5 | +import org.junit.Test |
| 6 | +import zio.config.ConfigDescriptor._ |
| 7 | +import zio.config._ |
| 8 | +import zio.{Layer, ZIO} |
| 9 | + |
| 10 | +class ZIOConfigFeature { |
| 11 | + |
| 12 | + val runtime: zio.Runtime[zio.ZEnv] = zio.Runtime.default |
| 13 | + |
| 14 | + case class UserName(value: String) |
| 15 | + |
| 16 | + /** |
| 17 | + * ADT to contain the config info we want to load to use in our program |
| 18 | + */ |
| 19 | + case class UserConfig(username: UserName, |
| 20 | + password: String, |
| 21 | + age: Int, |
| 22 | + address: Option[String], |
| 23 | + multiValue: String) |
| 24 | + |
| 25 | + /** |
| 26 | + * [ConfigDescriptor] Config structure where we can specify which config alias we want to load into our [UserConfig] structure. |
| 27 | + * The way to bond the config with the ADT is by the order of definition. |
| 28 | + * |
| 29 | + * We can define strong type of T for a primitive, (T.apply, T.unapply) after primitive definition. |
| 30 | + * We can define one attribute as Option[T] type, using [optional] operator after definition. |
| 31 | + * We can define one attribute with default value using [default] passing the value for that config, if it does not exit. |
| 32 | + * We can bond one attribute or in case it is not in config to another, using <> operator, which is a [orElse] sugar syntax. |
| 33 | + */ |
| 34 | + val userConfig: ConfigDescriptor[String, String, UserConfig] = |
| 35 | + (string("USERNAME")(UserName.apply, UserName.unapply) |@| |
| 36 | + string("PASSWORD") |@| |
| 37 | + int("AGE").default(18) |@| |
| 38 | + string("ADDRESS").optional |@| |
| 39 | + string("VALUE1") <> string("VALUE2")) (UserConfig.apply, UserConfig.unapply) |
| 40 | + |
| 41 | + /** |
| 42 | + * An example program where we load as Env argument the Config[UserConfig] |
| 43 | + * Once in the program using config[ADT] we can load the config in the program to be used. |
| 44 | + */ |
| 45 | + val configProgram: ZIO[Config[UserConfig], Nothing, Unit] = for { |
| 46 | + myConfig <- config[UserConfig] |
| 47 | + _ <- ZIO.succeed(println(myConfig.username)) |
| 48 | + _ <- ZIO.succeed(println(myConfig.password)) |
| 49 | + _ <- ZIO.succeed(println(myConfig.age)) |
| 50 | + _ <- ZIO.succeed(println(myConfig.address)) |
| 51 | + _ <- ZIO.succeed(println(myConfig.multiValue)) |
| 52 | + } yield () |
| 53 | + |
| 54 | + /** |
| 55 | + * We can create ZLayer config using [Config.fromMap] where we have to pass the map with the config info |
| 56 | + * and the ConfigDescriptor [userConfig] to bond the config info into the ADT [UserConfig] |
| 57 | + * To run our program passing the config as Env argument we use [provideCustomLayer] passing the |
| 58 | + * ZLayer configLayer |
| 59 | + */ |
| 60 | + @Test |
| 61 | + def configLayerFromMap(): Unit = { |
| 62 | + val map = Map( |
| 63 | + "USERNAME" -> "Politrons", |
| 64 | + "PASSWORD" -> "Rpdiuo34f@rr", |
| 65 | + "VALUE1" -> "Value1", |
| 66 | + "NotUsed" -> "Foo" |
| 67 | + ) |
| 68 | + |
| 69 | + val configLayer: Layer[ReadError[String], Config[UserConfig]] = Config.fromMap(map, userConfig) |
| 70 | + |
| 71 | + runtime.unsafeRun(configProgram.provideCustomLayer(configLayer)) |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * We can also provide the Config in our ADT using java.util.Properties using [fromProperties] |
| 77 | + */ |
| 78 | + @Test |
| 79 | + def configLayerFromProperties(): Unit = { |
| 80 | + val properties = new Properties() |
| 81 | + properties.put("USERNAME", "Politrons") |
| 82 | + properties.put("PASSWORD", "112223344") |
| 83 | + properties.put("AGE", "38") |
| 84 | + properties.put("ADDRESS", "Camilo Jose Cela") |
| 85 | + properties.put("VALUE2", "Value2") |
| 86 | + |
| 87 | + |
| 88 | + val configLayer: Layer[ReadError[String], Config[UserConfig]] = Config.fromProperties(properties, userConfig, "") |
| 89 | + |
| 90 | + runtime.unsafeRun(configProgram.provideCustomLayer(configLayer)) |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Another ADT to load some env variable in our program |
| 96 | + */ |
| 97 | + case class SysEnvConfig(home: String, |
| 98 | + javaHome: String) |
| 99 | + |
| 100 | + val sysEnvConfig: ConfigDescriptor[String, String, SysEnvConfig] = |
| 101 | + (string("HOME") |@| string("JAVA_HOME")) (SysEnvConfig.apply, SysEnvConfig.unapply) |
| 102 | + |
| 103 | + val sysEnvProgram: ZIO[Config[SysEnvConfig], Nothing, Unit] = for { |
| 104 | + myConfig <- config[SysEnvConfig] |
| 105 | + _ <- ZIO.succeed(println(myConfig.home)) |
| 106 | + _ <- ZIO.succeed(println(myConfig.javaHome)) |
| 107 | + } yield () |
| 108 | + |
| 109 | + /** |
| 110 | + * We can also create ZLayer config using [fromSystemEnv] which it will get all System environment of the machine |
| 111 | + * and it will bond with the [ConfigDescriptor] passed in the Config factory |
| 112 | + */ |
| 113 | + @Test |
| 114 | + def configLayerFromSysEnv(): Unit = { |
| 115 | + |
| 116 | + val configLayer: Layer[ReadError[String], Config[SysEnvConfig]] = Config.fromSystemEnv(sysEnvConfig) |
| 117 | + |
| 118 | + runtime.unsafeRun(sysEnvProgram.provideCustomLayer(configLayer)) |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments