Created
September 15, 2017 10:10
-
-
Save vsuharnikov/67bace790e3afbc5e1fd592f00a5215f to your computer and use it in GitHub Desktop.
pureconfig: a case class of a sealed trait by the boolean value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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