Skip to content

Cirisconfiguration #1256

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

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,13 @@ lazy val scala_libraries_config = (project in file("scala-libraries-config"))
munitDep,
"com.github.japgolly.clearconfig" %% "core" % "3.1.0",
catsEffect,
"io.circe" %% "circe-yaml" % "1.15.0",
"io.circe" %% "circe-yaml" % "0.15.1",
circeDep,
circeParserDep
)
),
libraryDependencies += "is.cir" %% "ciris" % "3.5.0",
libraryDependencies += "is.cir" %% "ciris-circe" % "3.5.0",
libraryDependencies += "is.cir" %% "ciris-circe-yaml" % "3.5.0"
)

lazy val scala_strings = (project in file("scala-strings"))
Expand Down
4 changes: 4 additions & 0 deletions scala-libraries-config/src/main/resources/postgresConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username": "username",
"password": "password"
}
2 changes: 2 additions & 0 deletions scala-libraries-config/src/main/resources/postgresConfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
username: username
password: password
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package cirisconfig

import ciris.*
import cats.syntax.all.*
import cats.effect.*
import java.nio.file.Path
import ciris.circe.circeConfigDecoder
import io.circe.Decoder
import cats.effect.Async
import ciris.circe.yaml.circeYamlConfigDecoder

object Configuration:

final case class PostgresConfig(username: String, password: String)

def postgresConfig: ConfigValue[Effect, PostgresConfig] =
(
env("POSTGRES_USERNAME").as[String],
env("POSTGRES_PASSWORD").as[String]
).parMapN(PostgresConfig.apply)

final case class PostgresConfig2(
username: Option[String],
password: Option[String]
)

def postgresConfig2: ConfigValue[Effect, PostgresConfig2] =
(
env("POSTGRES_USERNAME").as[String].option,
env("POSTGRES_PASSWORD").as[String].option
).parMapN(PostgresConfig2.apply)

case class Username(name: String)
object Username:
given ConfigDecoder[String, Username] =
ConfigDecoder[String, String].map(Username.apply)

case class Password(value: String)
object Password:
given ConfigDecoder[String, Password] =
ConfigDecoder[String, String].map(Password.apply)

final case class PostgresConfig3(
username: Option[Username],
password: Option[Password]
)

def postgresConfig3: ConfigValue[Effect, PostgresConfig3] =
(
env("POSTGRES_USERNAME").as[Username].option,
env("POSTGRES_PASSWORD").as[Password].option
).parMapN(PostgresConfig3.apply)

final case class PostgresConfig4(username: Username, password: Password)

object PostgresConfig4:
// given ConfigDecoder[String, PostgresConfig4] =
// circeConfigDecoder("PostgresConfig4")

given ConfigDecoder[String, PostgresConfig4] =
circeYamlConfigDecoder("PostgresConfig4")

given Decoder[PostgresConfig4] = Decoder.instance { h =>
for
username <- h.get[String]("username")
password <- h.get[String]("password")
yield PostgresConfig4(Username(username), Password(password))
}

val postgresConfig4: ConfigValue[Effect, PostgresConfig4] =
file(
Path.of("src/main/resources/postgresConfig.json")
).as[PostgresConfig4]

def postgresConfig5[F[_]: Async]: F[Either[ConfigError, PostgresConfig4]] =
file(
Path.of("src/main/resources/postgresConfig.json")
).as[PostgresConfig4].attempt[F]

def postgresConfig6[F[_]: Async]: F[Either[ConfigError, PostgresConfig4]] =
file(
Path.of("src/main/resources/postgresConfig.yaml")
).as[PostgresConfig4].attempt[F]

// handling secrets
case class Password2(value: Secret[String])
object Password2:
def apply(value: String) =
new Password2(Secret(value))

def postgresConfig7[F[_]: Async]: F[Either[ConfigError, PostgresConfig4]] =
file(
Path.of("src/main/resources/missing.yaml")
).as[PostgresConfig4]
.default {
PostgresConfig4(Username("username"), Password("password"))
}
.attempt[F]

object program extends IOApp.Simple:
import Configuration.*

override def run: IO[Unit] =
// blows up application
// postgresConfig.load[IO].map(println).void

// handled errors
// postgresConfig2.load[IO].map(println).void

// added typeclasses
// postgresConfig3.load[IO].map(println).void

// config loading from json file
// postgresConfig4.load[IO].map(println).void

// managing errors
// postgresConfig5[IO].map{config =>
// config match
// case Right(value) => println(value)
// case Left(err) => err.messages.map(println)
// }

// config loading from yaml file
// postgresConfig6[IO].map{config =>
// config match
// case Right(value) => println(value)
// case Left(err) => err.messages.map(println)
// }

// config loading with secret
// IO(println(Password2(Secret("password"))))

// config loading with fallback values
postgresConfig7[IO].map { config =>
config match
case Right(value) => println(value)
case Left(err) => err.messages.map(println)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cirisconfig

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.prop.TableDrivenPropertyChecks
import Configuration.*
import ciris.*
import cats.effect.IO

class ConfigurationSuite
extends AnyFlatSpec
with Matchers
with TableDrivenPropertyChecks:

"postgresConfig" should "be of type ConfigValue[Effect, PostgresConfig]" in {
postgresConfig shouldBe an[ConfigValue[Effect, PostgresConfig]]
}

"postgresConfig2" should "be of type ConfigValue[Effect, PostgresConfig2]" in {
postgresConfig2 shouldBe an[ConfigValue[Effect, PostgresConfig2]]
}

"postgresConfig3" should "be of type ConfigValue[Effect, PostgresConfig3]" in {
postgresConfig3 shouldBe an[ConfigValue[Effect, PostgresConfig3]]
}

"postgresConfig4, " should "be of type ConfigValue[Effect, PostgresConfig4]" in {
postgresConfig4 shouldBe an[ConfigValue[Effect, PostgresConfig4]]
}

"postgresConfig5, postgresConfig6, postgresConfig7" should "be of type IO[Either[ConfigError, PostgresConfig4]]" in {
forAll(
Table(
"postgresConfig567",
postgresConfig5[IO],
postgresConfig6[IO],
postgresConfig7[IO]
)
) { v =>
v shouldBe an[IO[Either[ConfigError, PostgresConfig4]]]
}
}

"PostgresConfig" should "be of contain type String" in {
val pconfig = PostgresConfig("u", "p")
pconfig.username shouldBe an[String]
pconfig.password shouldBe an[String]
}

"PostgresConfig2" should "be of contain type Option[String]" in {
val pconfig2 = PostgresConfig2(Some("u"), Some("p"))
pconfig2.username shouldBe an[Option[String]]
pconfig2.password shouldBe an[Option[String]]
}

"PostgresConfig3" should "be of contain type Option[Username] and Option[Password]" in {
val pconfig3 = PostgresConfig3(Some(Username("u")), Some(Password("p")))
pconfig3.username shouldBe an[Option[Username]]
pconfig3.password shouldBe an[Option[Password]]
}

"PostgresConfig4" should "be of contain type Username and Password" in {
val pconfig4 = PostgresConfig4(Username("u"), Password("p"))
pconfig4.username shouldBe an[Username]
pconfig4.password shouldBe an[Password]
}