Pureconfig

structible-pureconfig is available for Scala 2.12 and 2.13.

It provides derivations for

  • ConfigConvert
  • ConfigReader
  • ConfigWriter

Make sure to familiarize yourself with creating structible instances before looking at the examples below.

Dependency

sbt
libraryDependencies += "com.github.cerst" % "structible-pureconfig" % "0.6.4"
Maven
<dependency>
  <groupId>com.github.cerst</groupId>
  <artifactId>structible-pureconfig</artifactId>
  <version>0.6.4</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.github.cerst', name: 'structible-pureconfig', version: '0.6.4'
}

Example (Individual)

You can derive ConfigReader and ConfigWriter individually like this (most of the time, only ConfigReader is required):

import com.github.cerst.structible.core.DefaultConstraints._
import com.github.cerst.structible.core._
import com.github.cerst.structible.pureconfig.ops._
import com.typesafe.config.{Config, ConfigValue}
import pureconfig.{ConfigReader, ConfigSource, ConfigWriter}

object IndividualExample {

  final class UserId private(val value: Long) extends AnyVal

  object UserId {

    private val structible: Structible[Long, UserId] = Structible.structible(new UserId(_), _.value, c >= 0)

    implicit val configReaderForUserId: ConfigReader[UserId] = structible.toConfigReader

    implicit val configWriterForUserId: ConfigWriter[UserId] = structible.toConfigWriter

    def apply(value: Long): UserId = structible.construct(value)
  }

  def fromConfig(config: Config): ConfigReader.Result[UserId] = {
    ConfigSource.fromConfig(config).load[UserId]
  }

  def toConfigValue(personId: UserId): ConfigValue = {
    ConfigWriter[UserId].to(personId)
  }

}

Example (Compact)

Whenever you need both of them, you can also create a ConfigConvert instance in their place.

import com.github.cerst.structible.core._
import DefaultConstraints._
import com.github.cerst.structible.pureconfig.ops._
import com.typesafe.config.{Config, ConfigValue}
import pureconfig.{ConfigConvert, ConfigReader, ConfigSource, ConfigWriter}

object CompactExample {

  final class UserId private(val value: Long) extends AnyVal

  object UserId {

    private val structible: Structible[Long, UserId] = Structible.structible(new UserId(_), _.value, c >= 0)

    implicit val configConvertForUserId: ConfigConvert[UserId] = structible.toConfigConvert

    def apply(value: Long): UserId = structible.construct(value)
  }

  def fromConfig(config: Config): ConfigReader.Result[UserId] = {
    ConfigSource.fromConfig(config).load[UserId]
  }

  def toConfigValue(personId: UserId): ConfigValue = {
    ConfigWriter[UserId].to(personId)
  }

}
The source code for this page can be found here.