Case classy is a tiny library to make it easy to decode untyped structured data into case class hierarchies of your choosing. It's completely modular, support Scala 2.11 and 2.12, ScalaJS ready, and the core module has zero external dependencies.
// required
libraryDependencies += "com.47deg" %% "classy-core" % "0.4.0"
// at least one required
libraryDependencies += "com.47deg" %% "classy-config-typesafe" % "0.4.0"
libraryDependencies += "com.47deg" %% "classy-config-shocon" % "0.4.0"
// optional
libraryDependencies += "com.47deg" %% "classy-generic" % "0.4.0"
libraryDependencies += "com.47deg" %% "classy-cats" % "0.4.0"
The modules provide the following support:
classy-core
: Basic set of configuration decoders and combinators. requiredclassy-generic
: Automatic derivation for your case class hierarchies. depends on shapelessclassy-config-typesafe
: Support for Typesafe's Config library.classy-config-shocon
: Support for the Shocon config library.classy-cats
: Instances for Cats.
All module support ScalaJS except classy-config-typesafe
.
Documentation is available on the website.
import classy.generic._
import classy.config._
// Our configuration class hierarchy
sealed trait Shape
case class Circle(radius: Double) extends Shape
case class Rectangle(length: Double, width: Double) extends Shape
case class MyConfig(
someString: Option[String],
shapes: List[Shape])
import com.typesafe.config.Config
val decoder1 = deriveDecoder[Config, MyConfig]
decoder1.fromString("""shapes = []""")
// res4: Either[classy.DecodeError,MyConfig] = Right(MyConfig(None,List()))
decoder1.fromString("""
someString = "hello"
shapes = []""")
// res5: Either[classy.DecodeError,MyConfig] = Right(MyConfig(Some(hello),List()))
decoder1.fromString("""shapes = [
{ circle { radius: 200.0 } },
{ rectangle { length: 10.0, width: 20.0 } }
]""")
// res6: Either[classy.DecodeError,MyConfig] = Right(MyConfig(None,List(Circle(200.0), Rectangle(10.0,20.0))))
// mismatched config
val res = decoder1.fromString("""shapes = [
{ rectangle { radius: 200.0 } },
{ circle { length: 10.0, width: 20.0 } }
]""")
// res: Either[classy.DecodeError,MyConfig] = Left(AtPath(shapes,And(AtIndex(0,Or(AtPath(circle,Missing),List(AtPath(rectangle,And(AtPath(length,Missing),List(AtPath(width,Missing))))))),List(AtIndex(1,Or(AtPath(circle,AtPath(radius,Missing)),List(AtPath(rectangle,Missing))))))))
// error pretty printing
res.fold(
error => error.toPrettyString,
conf => s"success: $conf")
// res9: String =
// errors.shapes (conjunction/AND):
// [0] (disjunction/OR):
// circle: missing value
// rectangle (conjunction/AND):
// length: missing value
// width: missing value
// [1] (disjunction/OR):
// circle.radius: missing value
// rectangle: missing value
If you wish to add your library here please consider a PR to include it in the list below.
47 Degrees offers commercial support for the Case Classy library and associated technologies. To find out more, visit 47 Degrees' Open Source Support.
Case Classy is designed and developed by 47 Degrees
Copyright (C) 2017 47 Degrees. http://47deg.com