|
| 1 | +package cirisconfig |
| 2 | + |
| 3 | +import ciris.* |
| 4 | +import cats.syntax.all.* |
| 5 | +import cats.effect.* |
| 6 | +import java.nio.file.Path |
| 7 | +import ciris.circe.circeConfigDecoder |
| 8 | +import io.circe.Decoder |
| 9 | +import cats.effect.Async |
| 10 | +import ciris.circe.yaml.circeYamlConfigDecoder |
| 11 | + |
| 12 | +object Configuration: |
| 13 | + |
| 14 | + final case class PostgresConfig(username: String, password: String) |
| 15 | + |
| 16 | + def postgresConfig: ConfigValue[Effect, PostgresConfig] = |
| 17 | + ( |
| 18 | + env("POSTGRES_USERNAME").as[String], |
| 19 | + env("POSTGRES_PASSWORD").as[String] |
| 20 | + ).parMapN(PostgresConfig.apply) |
| 21 | + |
| 22 | + final case class PostgresConfig2( |
| 23 | + username: Option[String], |
| 24 | + password: Option[String] |
| 25 | + ) |
| 26 | + |
| 27 | + def postgresConfig2: ConfigValue[Effect, PostgresConfig2] = |
| 28 | + ( |
| 29 | + env("POSTGRES_USERNAME").as[String].option, |
| 30 | + env("POSTGRES_PASSWORD").as[String].option |
| 31 | + ).parMapN(PostgresConfig2.apply) |
| 32 | + |
| 33 | + case class Username(name: String) |
| 34 | + object Username: |
| 35 | + given ConfigDecoder[String, Username] = |
| 36 | + ConfigDecoder[String, String].map(Username.apply) |
| 37 | + |
| 38 | + case class Password(value: String) |
| 39 | + object Password: |
| 40 | + given ConfigDecoder[String, Password] = |
| 41 | + ConfigDecoder[String, String].map(Password.apply) |
| 42 | + |
| 43 | + final case class PostgresConfig3( |
| 44 | + username: Option[Username], |
| 45 | + password: Option[Password] |
| 46 | + ) |
| 47 | + |
| 48 | + def postgresConfig3: ConfigValue[Effect, PostgresConfig3] = |
| 49 | + ( |
| 50 | + env("POSTGRES_USERNAME").as[Username].option, |
| 51 | + env("POSTGRES_PASSWORD").as[Password].option |
| 52 | + ).parMapN(PostgresConfig3.apply) |
| 53 | + |
| 54 | + final case class PostgresConfig4(username: Username, password: Password) |
| 55 | + |
| 56 | + object PostgresConfig4: |
| 57 | + // given ConfigDecoder[String, PostgresConfig4] = |
| 58 | + // circeConfigDecoder("PostgresConfig4") |
| 59 | + |
| 60 | + given ConfigDecoder[String, PostgresConfig4] = |
| 61 | + circeYamlConfigDecoder("PostgresConfig4") |
| 62 | + |
| 63 | + given Decoder[PostgresConfig4] = Decoder.instance { h => |
| 64 | + for |
| 65 | + username <- h.get[String]("username") |
| 66 | + password <- h.get[String]("password") |
| 67 | + yield PostgresConfig4(Username(username), Password(password)) |
| 68 | + } |
| 69 | + |
| 70 | + val postgresConfig4: ConfigValue[Effect, PostgresConfig4] = |
| 71 | + file( |
| 72 | + Path.of("src/main/resources/postgresConfig.json") |
| 73 | + ).as[PostgresConfig4] |
| 74 | + |
| 75 | + def postgresConfig5[F[_]: Async]: F[Either[ConfigError, PostgresConfig4]] = |
| 76 | + file( |
| 77 | + Path.of("src/main/resources/postgresConfig.json") |
| 78 | + ).as[PostgresConfig4].attempt[F] |
| 79 | + |
| 80 | + def postgresConfig6[F[_]: Async]: F[Either[ConfigError, PostgresConfig4]] = |
| 81 | + file( |
| 82 | + Path.of("src/main/resources/postgresConfig.yaml") |
| 83 | + ).as[PostgresConfig4].attempt[F] |
| 84 | + |
| 85 | + // handling secrets |
| 86 | + case class Password2(value: Secret[String]) |
| 87 | + object Password2: |
| 88 | + def apply(value: String) = |
| 89 | + new Password2(Secret(value)) |
| 90 | + |
| 91 | + def postgresConfig7[F[_]: Async]: F[Either[ConfigError, PostgresConfig4]] = |
| 92 | + file( |
| 93 | + Path.of("src/main/resources/missing.yaml") |
| 94 | + ).as[PostgresConfig4] |
| 95 | + .default { |
| 96 | + PostgresConfig4(Username("username"), Password("password")) |
| 97 | + } |
| 98 | + .attempt[F] |
| 99 | + |
| 100 | +object program extends IOApp.Simple: |
| 101 | + import Configuration.* |
| 102 | + |
| 103 | + override def run: IO[Unit] = |
| 104 | + // blows up application |
| 105 | + // postgresConfig.load[IO].map(println).void |
| 106 | + |
| 107 | + // handled errors |
| 108 | + // postgresConfig2.load[IO].map(println).void |
| 109 | + |
| 110 | + // added typeclasses |
| 111 | + // postgresConfig3.load[IO].map(println).void |
| 112 | + |
| 113 | + // config loading from json file |
| 114 | + // postgresConfig4.load[IO].map(println).void |
| 115 | + |
| 116 | + // managing errors |
| 117 | + // postgresConfig5[IO].map{config => |
| 118 | + // config match |
| 119 | + // case Right(value) => println(value) |
| 120 | + // case Left(err) => err.messages.map(println) |
| 121 | + // } |
| 122 | + |
| 123 | + // config loading from yaml file |
| 124 | + // postgresConfig6[IO].map{config => |
| 125 | + // config match |
| 126 | + // case Right(value) => println(value) |
| 127 | + // case Left(err) => err.messages.map(println) |
| 128 | + // } |
| 129 | + |
| 130 | + // config loading with secret |
| 131 | + // IO(println(Password2(Secret("password")))) |
| 132 | + |
| 133 | + // config loading with fallback values |
| 134 | + postgresConfig7[IO].map { config => |
| 135 | + config match |
| 136 | + case Right(value) => println(value) |
| 137 | + case Left(err) => err.messages.map(println) |
| 138 | + } |
0 commit comments