jcouyang / dhall-generic

Dhall generic decoder for Scala

GitHub

Dhall Scala Generic

Load Dhall config directly into Scala case class

Build and Test

libraryDependencies += "us.oyanglul" %% "dhall-generic" % "0.2.0"

Supposed that you have some Scala case classes

sealed trait Shape
case class Rectangle(width: Double, height: Double) extends Shape
case class Circle(radius: Double) extends Shape

and a dhall config

let Shape = <Rectangle: {width: Double, height: Double}| Circle: {radius: Double}>
in Shape.Circle {radius = 1.2}

to load a dhall configuration into Scala case classes, simply just

import org.dhallj.syntax._
import org.dhallj.codec.syntax._
import us.oyanglul.dhall.generic._

val expr = """
let Shape = <Rectangle: {width: Double, height: Double}| Circle: {radius: Double}>
in Shape.Circle {radius = 1.2}
""".parseExpr

expr.normalize.as[Shape]
// => Right(Circle(1.2)): Either[DecodingFailure, Shape]