Skip to content

Commit

Permalink
Add tests for PartialTransformer#contramap
Browse files Browse the repository at this point in the history
  • Loading branch information
danicheg committed Aug 31, 2024
1 parent b6882c6 commit 0733e9a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,54 +180,54 @@ object PartialTransformer extends PartialTransformerCompanionPlatform {
def transform(src: From, failFast: Boolean): partial.Result[To]

/** Creates a new [[io.scalaland.chimney.PartialTransformer PartialTransformer]] by applying a pure function to a
* [[io.scalaland.chimney.partial.Result Result]] of transforming `From` to `To`. See an example:
* source of type `A` before transforming it to `To`. See an example:
* {{{
* val stringTransformer: PartialTransformer[String, Int] =
* PartialTransformer.fromFunction(_.length)
*
* case class Length(length: Int)
* case class Id(id: String)
*
* implicit val toLengthTransformer: PartialTransformer[String, Length] =
* stringTransformer.map(id => Length(id))
* implicit val idTransformer: PartialTransformer[Id, Int] =
* stringTransformer.contramap(_.id)
* }}}
*
* @param f
* a pure function that maps a value of `To` to `A`
* a pure function that maps a value of `A` to `From`
* @return
* new [[io.scalaland.chimney.PartialTransformer PartialTransformer]]
*
* @since 1.5.0
*/
final def map[A](f: To => A): PartialTransformer[From, A] = new PartialTransformer[From, A] {
override def transform(src: From, failFast: Boolean): partial.Result[A] =
self.transform(src, failFast) match {
case partial.Result.Value(to) => partial.Result.Value(f(to))
case errs: partial.Result.Errors => errs.asInstanceOf[partial.Result[A]]
}
final def contramap[A](f: A => From): PartialTransformer[A, To] = new PartialTransformer[A, To] {
override def transform(src: A, failFast: Boolean): partial.Result[To] =
self.transform(f(src), failFast)
}

/** Creates a new [[io.scalaland.chimney.PartialTransformer PartialTransformer]] by applying a pure function to a
* source of type `A` before transforming it to `To`. See an example:
* [[io.scalaland.chimney.partial.Result Result]] of transforming `From` to `To`. See an example:
* {{{
* val stringTransformer: PartialTransformer[String, Int] =
* PartialTransformer.fromFunction(_.length)
*
* case class Id(id: String)
* case class Length(length: Int)
*
* implicit val idTransformer: PartialTransformer[Id, Int] =
* stringTransformer.contramap(_.id)
* implicit val toLengthTransformer: PartialTransformer[String, Length] =
* stringTransformer.map(id => Length(id))
* }}}
*
* @param f
* a pure function that maps a value of `A` to `From`
* a pure function that maps a value of `To` to `A`
* @return
* new [[io.scalaland.chimney.PartialTransformer PartialTransformer]]
*
* @since 1.5.0
*/
final def contramap[A](f: A => From): PartialTransformer[A, To] = new PartialTransformer[A, To] {
override def transform(src: A, failFast: Boolean): partial.Result[To] =
self.transform(f(src), failFast)
final def map[A](f: To => A): PartialTransformer[From, A] = new PartialTransformer[From, A] {
override def transform(src: From, failFast: Boolean): partial.Result[A] =
self.transform(src, failFast) match {
case partial.Result.Value(to) => partial.Result.Value(f(to))
case errs: partial.Result.Errors => errs.asInstanceOf[partial.Result[A]]
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,34 @@ class PartialTransformerSpec extends ChimneySpec {
id.intoPartial[Length].transform ==> partial.Result.fromValue(Length(id.toInt))
id.intoPartial[Prefix].transform ==> partial.Result.fromValue(Prefix.BarPrefix)
}

test("contramap") {
case class Id(id: String)

trait Prefix {
def value: String
}

object Prefix {
case object FooPrefix extends Prefix {
override def value: String = "1"
}

case object BarPrefix extends Prefix {
override def value: String = "2"
}
}

implicit val idTransformer: PartialTransformer[Id, Int] =
pt1.contramap(_.id)

implicit val prefixTransformer: PartialTransformer[Prefix, Int] =
pt1.contramap(_.value)

val id = "1"
Id(id).intoPartial[Int].transform ==> partial.Result.fromValue(id.toInt)

val prefix: Prefix = Prefix.FooPrefix
prefix.intoPartial[Int].transform ==> partial.Result.fromValue(id.toInt)
}
}

0 comments on commit 0733e9a

Please sign in to comment.