Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:18
Show Gist options
  • Save dacr/3e5949f0ae9fd185284da8e52b303617 to your computer and use it in GitHub Desktop.
Save dacr/3e5949f0ae9fd185284da8e52b303617 to your computer and use it in GitHub Desktop.
chimney simple usage example / published by https://github.com/dacr/code-examples-manager #ab486c41-ac65-48f0-8f19-097e16a27d83/5fba5057ba6fb142977b37c1e03d31cb36a79e3f
// summary : chimney simple usage example
// keywords : scala, chimney, @testable
// publish : gist
// authors : chimney
// license : Apache2
// id : ab486c41-ac65-48f0-8f19-097e16a27d83
// created-on : 2023-06-23T16:20:02+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
//> using scala "3.4.2"
//> using dep "io.scalaland::chimney:0.8.0-M1"
//> using option "-deprecation"
import io.scalaland.chimney.dsl._
import java.time.ZonedDateTime
import scala.util.Random
case class MakeCoffee(id: Int, kind: String, addict: String)
case class CoffeeMade(id: Int, kind: String, forAddict: String, at: ZonedDateTime)
val command = MakeCoffee(
id = Random.nextInt(),
kind = "Espresso",
addict = "Piotr"
)
val eventLegacyWay = CoffeeMade(
id = command.id,
kind = command.kind,
forAddict = command.addict,
at = ZonedDateTime.now
)
println(eventLegacyWay)
val eventWithChimney = command
.into[CoffeeMade]
.withFieldComputed(_.at, _ => ZonedDateTime.now)
.withFieldRenamed(_.addict, _.forAddict)
.transform
println(eventWithChimney)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment