Skip to content

Commit 6e7f92e

Browse files
Merge pull request #1594 from bradovitt/chimney
chimney
2 parents 4ef3726 + cfd7ede commit 6e7f92e

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

build.sbt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ lazy val scala_libraries = (project in file("scala-libraries"))
457457
Defaults.itSettings
458458
)
459459

460+
lazy val scala_libraries_2 = (project in file("scala-libraries-2"))
461+
.configs(IntegrationTest)
462+
.settings(
463+
name := "scala-libraries-2",
464+
scalaVersion := scala3Version,
465+
libraryDependencies ++= scalaTestDeps
466+
.map(_.withConfigurations(Some("it,test"))),
467+
libraryDependencies += "io.scalaland" %% "chimney" % "1.4.0",
468+
Defaults.itSettings
469+
)
470+
460471
val http4sBlaze = "0.23.16"
461472
val http4sVersion = "0.23.28"
462473
val osLibVersion = "0.11.1"

scala-libraries-2/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Relevant Articles:
2+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.chimney
2+
3+
import io.scalaland.chimney.*, dsl.*, partial.*
4+
5+
object ChimneyCodec extends App:
6+
7+
case class Domain(a: Int, b: String)
8+
case class Dto(b: Option[String], a: Option[Int])
9+
10+
given Codec[Domain, Dto] = Codec.derive
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.chimney
2+
3+
import io.scalaland.chimney.*, dsl.*, partial.*
4+
5+
object ChimneyIso extends App:
6+
7+
case class StructuredItem(uuid: java.util.UUID)
8+
case class DomainItem(uuid: java.util.UUID)
9+
10+
given Iso[StructuredItem, DomainItem] = Iso.derive
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.chimney
2+
3+
import io.scalaland.chimney.*, dsl.*, partial.*
4+
5+
object ChimneyPartialTransformer extends App:
6+
7+
val fn: Int => Boolean =
8+
case 0 => false
9+
case 1 => true
10+
case i => throw Exception(s"Provided integer invalid: $i")
11+
12+
given PartialTransformer[Int, Boolean] =
13+
PartialTransformer.fromFunction(fn)
14+
15+
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)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.chimney
2+
3+
import io.scalaland.chimney.dsl.*
4+
5+
object ChimneyPatcher extends App:
6+
7+
case class Book(
8+
name: Title,
9+
authors: List[Author],
10+
isbn: ISBN
11+
)
12+
13+
case class Title(name: String) extends AnyVal
14+
case class Author(name: String, surname: String)
15+
16+
type ISBN = Option[String]
17+
18+
case class UpdateISBN(isbn: ISBN)
19+
20+
val book = Book(
21+
name = Title("Synergetics"),
22+
authors = List(Author("Buckminster", "Fuller")),
23+
isbn = None
24+
)
25+
26+
val isbnUpdateForm = UpdateISBN(
27+
isbn = Some("978-0206532048")
28+
)
29+
30+
val hardcover: Book = book.patchUsing(isbnUpdateForm)
31+
32+
// Standard Library Alternative
33+
34+
val softcover: Book =
35+
book.copy(
36+
authors =
37+
List(Author("Buckminster", "Fuller"), Author("Edmund", "Applewhite")),
38+
isbn = Some("978-0020653202")
39+
)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.chimney
2+
3+
import io.scalaland.chimney.*, dsl.*, partial.*
4+
5+
object ChimneyTransformers extends App:
6+
7+
class MyType(val a: Int)
8+
9+
class MyOtherType(val b: String):
10+
override def toString: String = s"MyOtherType($b)"
11+
12+
val transformer: Transformer[MyType, MyOtherType] = (src: MyType) =>
13+
new MyOtherType(src.a.toString)
14+
15+
transformer.transform(new MyType(10)) // new MyOtherType("10")
16+
17+
implicit val transformerAsImplicit: Transformer[MyType, MyOtherType] =
18+
transformer
19+
20+
(new MyType(10)).transformInto[MyOtherType]
21+
22+
// Transitive Given Instances
23+
24+
trait Serial[T]:
25+
def serial(v: T): String
26+
27+
given Serial[MyOtherType] with
28+
def serial(v: MyOtherType): String = v.toString
29+
30+
given [F, T](using Serial[T], Transformer[F, T]): Serial[F] with
31+
def serial(v: F): String =
32+
summon[Serial[T]].serial:
33+
summon[Transformer[F, T]].transform(v)
34+
35+
// Automatic Case Class Transformation
36+
37+
// BookDTO
38+
39+
case class BookDTO(
40+
name: String, // 1. primitive
41+
authors: Seq[AuthorDTO], // 2. Seq collection
42+
isbn: Option[String] // 3. Option type
43+
)
44+
45+
case class AuthorDTO(name: String, surname: String)
46+
47+
// Book Domain Model
48+
49+
case class Book(
50+
name: Title,
51+
authors: List[Author],
52+
isbn: ISBN
53+
)
54+
55+
case class Title(name: String) extends AnyVal
56+
case class Author(name: String, surname: String)
57+
58+
type ISBN = Option[String]
59+
60+
// we can do a transformation:
61+
62+
val book = Book(
63+
name = Title("The Universal One"),
64+
authors = List(Author("Walter", "Russell")),
65+
isbn = None
66+
)
67+
68+
val bookDTO: BookDTO = book.transformInto[BookDTO]
69+
70+
// Standard Library alternatives
71+
72+
// Selectable
73+
74+
class Record(elems: (String, Any)*) extends Selectable:
75+
private val fields = elems.toMap
76+
def selectDynamic(name: String): Any = fields(name)
77+
78+
type BookRecord = Record {
79+
val name: Title
80+
val authors: List[Author]
81+
val isbn: ISBN
82+
}
83+
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]
89+
90+
// Tuple Generics
91+
92+
val bookTuple: (Title, List[Author], ISBN) = Tuple.fromProductTyped(book)
93+
94+
val bookAgain: Book =
95+
summon[deriving.Mirror.Of[Book]].fromProduct(bookTuple)

0 commit comments

Comments
 (0)