Coder Social home page Coder Social logo

dhall-generic's Introduction

Dhall Scala Generic

Load Dhall config directly into Scala case class

Build and Test

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

There is a dhall config

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

And you can parse them into dhall Expr

import org.dhallj.syntax._

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

Scala 3

Supposed that you have some Scala case classes

enum Shape:
  case Rectangle(width: Double, height: Double)
  case Circle(radius: Double)

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

  1. derives Decoder
import us.oyanglul.dhall.generic.Decoder

enum Shape derives Decoder:  // derive will generate decoder inline
  case Rectangle(width: Double, height: Double)
  case Circle(radius: Double)
  1. as[Shape]
import us.oyanglul.dhall.generic.as

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

Scala 2

Supposed that you have some Scala case classes

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

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

import org.dhallj.codec.syntax._   // for `.as[A]` syntax
import org.dhallj.codec.Decoder._  // Decoders for primity types
import us.oyanglul.dhall.generic._ // generate generic decoders

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

Use cases

Load application config

It is very simple to replace HOCON application.conf with dhall.

To load src/main/resources/application.dhall from classpath your will need dhall-imports

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

  BlazeClientBuilder[IO](ExecutionContext.global).resource.use { implicit c =>
        IO.fromEither("classpath:/application.dhall".parseExpr)
          .flatMap(_.resolveImports[IO])
          .flatMap{expr => IO.fromEither(expr.normalize.as[Config])}
    }

Load dhall to sbt libraryDependencies

This project itself is an example of how to load dhall into build.sbt

It is recursively using previous version to load ./build.dhall to libraryDependencies

libraryDependencies ++= dhall.load.modules.map{case Module(o, n, v) => o %% n % v},

Custom Decoder

Create new decoder from existing decoder

i.e. UUID

Scala 3

given (using d: Decoder[String]): Decoder[UUID] = d.map(UUID.fromString)

Scala 2

implicit val decodeUUID: Decoder[UUID] = decodeString.map(UUID.from)

dhall-generic's People

Contributors

scala-steward avatar mergify[bot] avatar jcouyang avatar custommonkey avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.