@@ -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