@@ -39,7 +39,7 @@ object ChimneyTransformers extends App:
39
39
case class BookDTO (
40
40
name : String , // 1. primitive
41
41
authors : Seq [AuthorDTO ], // 2. Seq collection
42
- doi : Option [String ] // 3. Option type
42
+ isbn : Option [String ] // 3. Option type
43
43
)
44
44
45
45
case class AuthorDTO (name : String , surname : String )
@@ -49,21 +49,23 @@ object ChimneyTransformers extends App:
49
49
case class Book (
50
50
name : Title ,
51
51
authors : List [Author ],
52
- doi : DOI
52
+ isbn : ISBN
53
53
)
54
54
55
55
case class Title (name : String ) extends AnyVal
56
56
case class Author (name : String , surname : String )
57
57
58
- type DOI = Option [String ]
58
+ type ISBN = Option [String ]
59
59
60
60
// we can do a transformation:
61
61
62
- Book (
62
+ val book = Book (
63
63
name = Title (" The Universal One" ),
64
64
authors = List (Author (" Walter" , " Russell" )),
65
- doi = None
66
- ).transformInto[BookDTO ]
65
+ isbn = None
66
+ )
67
+
68
+ val bookDTO : BookDTO = book.transformInto[BookDTO ]
67
69
68
70
// Standard Library alternatives
69
71
@@ -73,23 +75,21 @@ object ChimneyTransformers extends App:
73
75
private val fields = elems.toMap
74
76
def selectDynamic (name : String ): Any = fields(name)
75
77
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
79
82
}
80
83
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 ]
85
89
86
90
// Tuple Generics
87
91
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)
93
93
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