Skip to content

Instantly share code, notes, and snippets.

@vsuharnikov
Created September 15, 2017 10:10
Show Gist options
  • Save vsuharnikov/67bace790e3afbc5e1fd592f00a5215f to your computer and use it in GitHub Desktop.
Save vsuharnikov/67bace790e3afbc5e1fd592f00a5215f to your computer and use it in GitHub Desktop.
pureconfig: a case class of a sealed trait by the boolean value
import com.typesafe.config.ConfigObject
import pureconfig._
import pureconfig.error.{CannotParse, ConfigReaderFailures}
sealed trait Enigma
object Enigma {
case class Foo(value: Int) extends Enigma
case object Bar extends Enigma
}
object Main extends App {
implicit val enigmaReader: ConfigReader[Enigma] = ConfigReader.fromFunction[Enigma] {
case configObj: ConfigObject =>
val config = configObj.toConfig
if (config.getBoolean("is-foo")) loadConfig[Enigma.Foo](config)
else Right(Enigma.Bar)
case _ =>
Left(ConfigReaderFailures(CannotParse("eto ditch", None)))
}
/* application.conf:
enigma1 {
is-foo = true
value = 1
}
enigma2 {
is-foo = false
}
*/
println(loadConfig[Enigma]("enigma1")) // Right(Foo(1))
println(loadConfig[Enigma]("enigma2")) // Right(Bar)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment