Skip to content

Commit eba8c75

Browse files
committed
more
1 parent 3007879 commit eba8c75

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ object ChimneyPatcher extends App:
77
case class Book(
88
name: Title,
99
authors: List[Author],
10-
doi: DOI
10+
isbn: ISBN
1111
)
1212

1313
case class Title(name: String) extends AnyVal
1414
case class Author(name: String, surname: String)
1515

16-
type DOI = Option[String]
16+
type ISBN = Option[String]
1717

18-
case class BookUpdateForm(name: String, authors: List[Author])
18+
case class UpdateISBN(isbn: ISBN)
1919

2020
val book = Book(
2121
name = Title("Synergetics"),
2222
authors = List(Author("Buckminster", "Fuller")),
23-
doi = None
23+
isbn = None
2424
)
2525

26-
val updateForm = BookUpdateForm(
27-
name = "Godel, Escher, Bach",
28-
authors = List(Author("Douglas", "Hofstadter"))
26+
val isbnUpdateForm = UpdateISBN(
27+
isbn = Some("978-0206532048")
2928
)
3029

31-
val patchedValue: Book = book.patchUsing(updateForm)
30+
val hardcover: Book = book.patchUsing(isbnUpdateForm)
3231

3332
// Standard Library Alternative
3433

35-
case class SimpleCaseClass(a: Int, b: Int)
36-
37-
val simpleClass = SimpleCaseClass(0, 0)
38-
39-
simpleClass.copy(b = 2) // SimpleCaseClass(0, 2)
34+
val softcover: Book =
35+
book.copy(
36+
authors =
37+
List(Author("Buckminster", "Fuller"), Author("Edmund", "Applewhite")),
38+
isbn = Some("978-0020653202")
39+
)

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object ChimneyTransformers extends App:
3939
case class BookDTO(
4040
name: String, // 1. primitive
4141
authors: Seq[AuthorDTO], // 2. Seq collection
42-
doi: Option[String] // 3. Option type
42+
isbn: Option[String] // 3. Option type
4343
)
4444

4545
case class AuthorDTO(name: String, surname: String)
@@ -49,21 +49,23 @@ object ChimneyTransformers extends App:
4949
case class Book(
5050
name: Title,
5151
authors: List[Author],
52-
doi: DOI
52+
isbn: ISBN
5353
)
5454

5555
case class Title(name: String) extends AnyVal
5656
case class Author(name: String, surname: String)
5757

58-
type DOI = Option[String]
58+
type ISBN = Option[String]
5959

6060
// we can do a transformation:
6161

62-
Book(
62+
val book = Book(
6363
name = Title("The Universal One"),
6464
authors = List(Author("Walter", "Russell")),
65-
doi = None
66-
).transformInto[BookDTO]
65+
isbn = None
66+
)
67+
68+
val bookDTO: BookDTO = book.transformInto[BookDTO]
6769

6870
// Standard Library alternatives
6971

@@ -73,23 +75,21 @@ object ChimneyTransformers extends App:
7375
private val fields = elems.toMap
7476
def selectDynamic(name: String): Any = fields(name)
7577

76-
type Person = Record {
77-
val name: String
78-
val age: Int
78+
type BookRecord = Record {
79+
val name: Title
80+
val authors: List[Author]
81+
val isbn: ISBN
7982
}
8083

81-
val person = Record(
82-
"name" -> "Emma",
83-
"age" -> 42
84-
).asInstanceOf[Person]
84+
val naturesOpenSecret: BookRecord = Record(
85+
"name" -> Title("Nature's Open Secret"),
86+
"authors" -> List(Author("Rudolph", "Steiner")),
87+
"isbn" -> Some("978-0880103930")
88+
).asInstanceOf[BookRecord]
8589

8690
// Tuple Generics
8791

88-
case class Employee(name: String, number: Int, manager: Boolean)
89-
90-
val bob: Employee = Employee("Bob", 42, false)
91-
92-
val bobTuple: (String, Int, Boolean) = Tuple.fromProductTyped(bob)
92+
val bookTuple: (Title, List[Author], ISBN) = Tuple.fromProductTyped(book)
9393

94-
val bobAgain: Employee =
95-
summon[deriving.Mirror.Of[Employee]].fromProduct(bobTuple)
94+
val bookAgain: Book =
95+
summon[deriving.Mirror.Of[Book]].fromProduct(bookTuple)

0 commit comments

Comments
 (0)