Skip to content

Commit

Permalink
emap => mapEither
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicoulaud-ledger committed Nov 6, 2023
1 parent 762d10a commit f4b4d6b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/sttp/tapir/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ trait Codec[L, H, +CF <: CodecFormat] { outer =>
* [[map]]
* @see
* [[mapDecode]]
* @see
* [[mapValidate]]
*/
def emap[HH](f: H => Either[String, HH])(g: HH => H): Codec[L, HH, CF] =
def mapEither[HH](f: H => Either[String, HH])(g: HH => H): Codec[L, HH, CF] =
mapDecode(s => DecodeResult.fromEitherString(s.toString, f(s)))(g)

/** Adds the given validator to the codec's schema, and maps this codec to the given higher-level type `HH`.
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/scala/sttp/tapir/CodecTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class CodecTest extends AnyFlatSpec with Matchers with Checkers with Inside {
codec.schema.validator should not be (Validator.pass)
}

it should "provide an emap function" in {
val codec: Codec[String, Int, TextPlain] = Codec.string.emap(s => Try(s.toInt).toEither.left.map(_.getMessage))(_.toString)
it should "provide a mapEither function" in {
val codec: Codec[String, Int, TextPlain] = Codec.string.mapEither(s => Try(s.toInt).toEither.left.map(_.getMessage))(_.toString)
codec.encode(10) should be("10")
codec.decode("10") should be(DecodeResult.Value(10))
inside(codec.decode("foo")) { case DecodeResult.Error(original, error) =>
Expand Down

0 comments on commit f4b4d6b

Please sign in to comment.