Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntactic sugar: add Codec.mapEither #3299

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions core/src/main/scala/sttp/tapir/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ trait Codec[L, H, +CF <: CodecFormat] { outer =>
def mapDecode[HH](f: H => DecodeResult[HH])(g: HH => H): Codec[L, HH, CF] = map(Mapping.fromDecode(f)(g))
def map[HH](f: H => HH)(g: HH => H): Codec[L, HH, CF] = mapDecode(f.andThen(Value(_)))(g)

/** Maps this codec to the given higher-level type `HH`.
*
* @param f
* decoding function
* @param g
* encoding function
* @tparam HH
* target type
* @see
* [[map]]
* @see
* [[mapDecode]]
* @see
* [[mapValidate]]
*/
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`.
*
* Unlike a `.validate(v).map(f)(g)` invocation, during decoding the validator is run before applying the `f` function. If there are
Expand Down
15 changes: 13 additions & 2 deletions core/src/test/scala/sttp/tapir/CodecTest.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sttp.tapir

import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.Assertion
import org.scalatest.{Assertion, Inside}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.Checkers
Expand All @@ -15,9 +15,10 @@ import java.nio.charset.StandardCharsets
import java.time._
import java.util.{Date, UUID}
import scala.reflect.ClassTag
import scala.util.Try

// see also CodecTestDateTime and CodecDelimitedTest
class CodecTest extends AnyFlatSpec with Matchers with Checkers {
class CodecTest extends AnyFlatSpec with Matchers with Checkers with Inside {

private implicit val arbitraryUri: Arbitrary[Uri] = Arbitrary(for {
scheme <- Gen.alphaLowerStr if scheme.nonEmpty
Expand Down Expand Up @@ -88,6 +89,16 @@ class CodecTest extends AnyFlatSpec with Matchers with Checkers {
codec.schema.validator should not be (Validator.pass)
}

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) =>
original should be("foo")
error.getMessage should be("""For input string: "foo"""")
}
}

case class Member(age: Int) {
require(age >= 18)
}
Expand Down
Loading