Last active
October 9, 2024 18:08
-
-
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/af5c7f8c55ed2389653582f4544013c2a89cc028
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.5.1" | |
//> using dep "io.scalaland::chimney:1.5.0" | |
//> using dep com.lihaoyi::pprint::0.9.0 | |
//> using option "-deprecation" | |
import io.scalaland.chimney.dsl._ | |
import java.time.ZonedDateTime | |
import scala.util.Random | |
import pprint.* | |
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 | |
) | |
pprintln(eventLegacyWay) | |
val eventWithChimney = | |
command | |
.into[CoffeeMade] | |
.withFieldComputed(_.at, _ => ZonedDateTime.now) | |
.withFieldRenamed(_.addict, _.forAddict) | |
.transform | |
pprintln(eventWithChimney) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment