Skip to content

Commit 3007879

Browse files
committed
updated patcher and transformer demos
1 parent 354ee9e commit 3007879

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

scala-libraries/src/main/scala/com/baeldung/chimney/PartialTransformer.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ object ChimneyPartialTransformer extends App:
1313
PartialTransformer.fromFunction(fn)
1414

1515
val result: Result[Boolean] = 0.transformIntoPartial[Boolean]
16+
17+
result match
18+
case Result.Value(bool) => println(bool)
19+
case Result.Errors(errs) => println(errs)

scala-libraries/src/main/scala/com/baeldung/chimney/Patcher.scala

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@ import io.scalaland.chimney.dsl.*
44

55
object ChimneyPatcher extends App:
66

7-
case class User(id: Int, email: Option[String], phone: Option[Long])
7+
case class Book(
8+
name: Title,
9+
authors: List[Author],
10+
doi: DOI
11+
)
812

9-
case class UserUpdateForm(email: String, phone: Long)
13+
case class Title(name: String) extends AnyVal
14+
case class Author(name: String, surname: String)
1015

11-
val user = User(10, Some("abc@@domain.com"), Some(1234567890L))
12-
val updateForm = UserUpdateForm("xyz@@domain.com", 123123123L)
16+
type DOI = Option[String]
1317

14-
val patchedValue: User = user.patchUsing(updateForm)
18+
case class BookUpdateForm(name: String, authors: List[Author])
19+
20+
val book = Book(
21+
name = Title("Synergetics"),
22+
authors = List(Author("Buckminster", "Fuller")),
23+
doi = None
24+
)
25+
26+
val updateForm = BookUpdateForm(
27+
name = "Godel, Escher, Bach",
28+
authors = List(Author("Douglas", "Hofstadter"))
29+
)
30+
31+
val patchedValue: Book = book.patchUsing(updateForm)
1532

1633
// Standard Library Alternative
1734

scala-libraries/src/main/scala/com/baeldung/chimney/Transformer.scala

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,36 @@ object ChimneyTransformers extends App:
3434

3535
// Automatic Case Class Transformation
3636

37-
// UserDTO
37+
// BookDTO
3838

39-
case class UserDTO(
39+
case class BookDTO(
4040
name: String, // 1. primitive
41-
addresses: Seq[AddressDTO], // 2. Seq collection
42-
recovery: Option[RecoveryMethodDTO] // 3. Option type
41+
authors: Seq[AuthorDTO], // 2. Seq collection
42+
doi: Option[String] // 3. Option type
4343
)
4444

45-
case class AddressDTO(street: String, city: String)
45+
case class AuthorDTO(name: String, surname: String)
4646

47-
sealed trait RecoveryMethodDTO
47+
// Book Domain Model
4848

49-
object RecoveryMethodDTO:
50-
case class Phone(value: PhoneDTO) extends RecoveryMethodDTO
51-
case class Email(value: EmailDTO) extends RecoveryMethodDTO
52-
53-
case class PhoneDTO(number: String)
54-
case class EmailDTO(email: String)
55-
56-
// User Domain Model
57-
58-
case class User(
59-
name: Username,
60-
addresses: List[Address],
61-
recovery: RecoveryMethod
49+
case class Book(
50+
name: Title,
51+
authors: List[Author],
52+
doi: DOI
6253
)
6354

64-
case class Username(name: String) extends AnyVal
65-
case class Address(street: String, city: String)
55+
case class Title(name: String) extends AnyVal
56+
case class Author(name: String, surname: String)
6657

67-
enum RecoveryMethod:
68-
case Phone(number: String)
69-
case Email(email: String)
58+
type DOI = Option[String]
7059

7160
// we can do a transformation:
7261

73-
User(
74-
Username("John"),
75-
List(Address("Paper St", "Somewhere")),
76-
RecoveryMethod.Email("[email protected]")
77-
).transformInto[UserDTO]
62+
Book(
63+
name = Title("The Universal One"),
64+
authors = List(Author("Walter", "Russell")),
65+
doi = None
66+
).transformInto[BookDTO]
7867

7968
// Standard Library alternatives
8069

0 commit comments

Comments
 (0)