[`refined4s-chimney`] Add `refined4s.modules.chimney.derivation.generic.auto` for auto derivation for Chimney
kevin-lee opened this issue · 0 comments
kevin-lee commented
Summary
[refined4s-chimney
] Add refined4s.modules.chimney.derivation.generic.auto
for auto derivation for Chimney
Version
0.16.0
Description
import io.scalaland.chimney
import refined4s.modules.chimney.derivation.generic.auto.given
import refined4s.types.all.PosInt
import refined4s.{Newtype, Refined}
val emailRegEx =
"""([a-zA-Z0-9]+([-_\.\+]+[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_]+[a-zA-Z0-9]+)*(?:[.][a-zA-Z0-9]+([-_]+[a-zA-Z0-9]+)*)+)""".r
final case class Foo(id: Foo.Id, baz: Foo.Baz)
object Foo {
type Id = Id.Type
object Id extends Newtype[PosInt]
type Name = Name.Type
object Name extends Newtype[String]
type Email = Email.Type
object Email extends Refined[String] {
override def invalidReason(a: String): String = s"Invalid email: $a"
override def predicate(a: String): Boolean = emailRegEx.findFirstMatchIn(a).isDefined
}
final case class Baz(name: Name, email: Email)
}
final case class Bar(id: Bar.Code, baz: Bar.Baz)
object Bar {
type Code = Code.Type
object Code extends Newtype[PosInt]
type Label = Label.Type
object Label extends Newtype[String]
type Email = Email.Type
object Email extends Refined[String] {
override def invalidReason(a: String): String = s"Invalid email: $a"
override def predicate(a: String): Boolean = emailRegEx.findFirstMatchIn(a).isDefined
}
final case class Baz(name: Label, email: Email)
}
val id = Foo.Id(PosInt(123))
id.into[Bar.Code].transform
// Bar.Code(PosInt(123))
val name = Foo.Name("Kevin")
name.into[Bar.Label].transform
// Bar.Label("Kevin")
val email = Foo.Email("aaa@aaa.aa")
email.intoPartial[Bar.Email].transform
// Result[Bar.Email] = Value(Bar.Email("aaa@aaa.aa"))
Foo(id, Foo.Baz(name, email).intoPartial[Bar].transform
// Result[Bar] = Value(Bar(id = Code(PosInt(123)), Bar.Baz(name = Bar.Label("Kevin"), Bar.Email("aaa@aaa.aa"))))