2m / ciris-hocon   1.2.0

Apache License 2.0 GitHub

Ciris Source for Hocon file format

Scala versions: 3.x 2.13 2.12

ciris-hocon provides a HOCON configuration source for Ciris configuration loading library.

The implementation of this library was created by following the excellent Ciris documentation.

Setup

Add the dependency to your project build settings:

libraryDependencies += "lt.dvim.ciris-hocon" %% "ciris-hocon" % "1.2.0"

Or a snapshot from a snapshot repository.

version scala ciris
0.1 2.12 0.12.1
0.2.1 2.13 0.13.0-RC1
1.0.x 2.13, 3 2.x.x
1.1.x 2.13, 3 3.x.x

Example usage

This library provides configuration sources as well as decoders from ConfigValue values.

import java.time.Period
import scala.concurrent.duration._

import cats.effect.IO
import cats.implicits._
import com.typesafe.config.ConfigFactory

import lt.dvim.ciris.Hocon._

val config = ConfigFactory.parseString("""
    |rate {
    |  elements = 2
    |  burst-duration = 100 millis
    |  check-interval = 2 weeks
    |  values = [ first, second ]
    |}
  """.stripMargin)

case class Rate(
  elements: Int,
  burstDuration: FiniteDuration,
  checkInterval: Period,
  values: List[String]
)

val hocon = hoconAt(config)("rate")
(
  hocon("elements").as[Int],
  hocon("burst-duration").as[FiniteDuration],
  hocon("check-interval").as[Period],
  hocon("values").as[List[String]]
).parMapN(Rate.apply).load[IO].map { rate =>
  assertEquals(rate.burstDuration, 100.millis)
  assertEquals(rate.checkInterval, Period.ofWeeks(2))
}