Chimney Chimney logo

chimney Scala version support

CI build codecov.io License Join the chat at https://gitter.im/scalalandio/chimney

Build docs Scaladoc 2.11 Scaladoc 2.12 Scaladoc 2.13

Battle tested Scala library for boilerplate-free data transformations.

In the daily life of a strongly-typed language's programmer sometimes it happens we need to transform an object of one type to another object which contains a number of the same or similar fields in their definitions.

case class MakeCoffee(id: Int, kind: String, addict: String)
case class CoffeeMade(id: Int, kind: String, forAddict: String, at: ZonedDateTime)

Usual approach is to just rewrite fields one by one

val command = MakeCoffee(id = Random.nextInt,
                         kind = "Espresso",
                         addict = "Piotr")
val event = CoffeeMade(id = command.id,
                       kind = command.kind,
                       forAddict = command.addict,
                       at = ZonedDateTime.now)

While the example stays lean, in real-life code we usually end up with tons of such boilerplate, especially when:

  • we maintain typed schema and want to migrate between multiple schema versions
  • we apply practices like DDD (Domain-Driven-Design) where suggested approach is to separate model schemas of different bounded contexts
  • we use code-generation tools like Protocol Buffers that generate primitive types like Int or String, while you'd prefer to use value objects in you domain-level code to improve type-safety and readability

Chimney provides a compact DSL with which you can define transformation rules and transform your objects with as little boilerplate as possible.

import io.scalaland.chimney.dsl._

val event = command.into[CoffeeMade]
  .withFieldComputed(_.at, _ => ZonedDateTime.now)
  .withFieldRenamed(_.addict, _.forAddict)
  .transform

Underneath it uses Scala macros to give you:

  • type-safety at compile-time
  • fast generated code, almost equivalent to handwritten version
  • excellent error messages
  • minimal overhead on compilation time

Getting started

To include Chimney to your SBT project, add the following line to your build.sbt:

// if you use Scala on JVM-only
libraryDependencies += "io.scalaland" %% "chimney" % chimneyVersion
// if you cross-compile to Scala.js and/or Scala Native
libraryDependencies += "io.scalaland" %%% "chimney" % chimneyVersion

where the latest versions available on Maven for each platform are

Chimney JVM versions
Chimney Scala.js 1.x versions
Chimney Scala.js 0.6 versions
Chimney Scala Native 0.3 versions

Library is currently supported for Scala 2.12.x and 2.13.x on JVM, SJS 1.x, SN 0.4. Other versions should be considered EOL.

Due to some compiler bugs, it's recommended to use at least Scala 2.12.1.

Trying out with Scala CLI/Ammonite

If you are using Scala CLI you can try out Chimney by adding it with using clause:

//> using scala "2.13.10"
//> using lib "io.scalaland::chimney:0.6.2"
import io.scalaland.chimney.dsl._

case class Foo(x: String, y: Int, z: Boolean = true)
case class Bar(x: String, y: Int)

object Main extends App {
  println(Foo("abc", 10).transformInto[Bar])
  println(Bar("abc", 10).into[Foo].enableDefaultValues.transform)
}

or run the Ammonite REPL:

scala-cli repl --ammonite --scala "2.13.10" --dependency "io.scalaland::chimney:0.6.2"
Loading...
Welcome to the Ammonite Repl 2.5.5-17-df243e14 (Scala 2.13.10 Java 17.0.1)
@ case class Foo(x: String, y: Int, z: Boolean = true)
defined class Foo

@ case class Bar(x: String, y: Int)
defined class Bar

@ import io.scalaland.chimney.dsl._
import io.scalaland.chimney.dsl._

@ Foo("abc", 10).transformInto[Bar]
res3: Bar = Bar(x = "abc", y = 10)

@ Bar("abc", 10).into[Foo].enableDefaultValues.transform
res4: Foo = Foo(x = "abc", y = 10, z = true)

If you don't have Scala CLI installed you can use this quick script that downloads coursier and uses it to fetch Ammonite REPL with the latest version of Chimney. It drops you immediately into a REPL session.

curl -s https://raw.githubusercontent.com/scalalandio/chimney/master/try-chimney.sh | bash

Documentation

Chimney documentation is available at https://scalalandio.github.io/chimney

Building documentation locally

In order to build documentation locally, you need to install Sphinx documentation generator first.

Then in project's root directory run command:

sbt makeSite

HTML Documentation should be generated at target/sphinx/html/index.html.

Alternatively use Docker:

docker run --rm -v ./docs:/docs sphinxdoc/sphinx:3.2.1 bash -c "pip install sphinx-rtd-theme && make html"

Thanks

Thanks to JProfiler (Java profiler) for helping us develop the library and allowing us to use it during development.

Thanks to SwissBorg for sponsoring the development time for this project.