From 8182fd390051bd28b1e8d6a8a5a0b4b3b8176a8c Mon Sep 17 00:00:00 2001 From: TomTriple Date: Tue, 29 Aug 2023 17:51:38 +0200 Subject: [PATCH] change signature to allow type inference (#2406) * change signature to allow type inference * format fix --- .../main/scala/zio/http/codec/PathCodec.scala | 17 +++------- .../scala/zio/http/codec/SegmentCodec.scala | 31 +++++++------------ .../scala/zio/http/codec/PathCodecSpec.scala | 29 +++++++---------- 3 files changed, 27 insertions(+), 50 deletions(-) diff --git a/zio-http/src/main/scala/zio/http/codec/PathCodec.scala b/zio-http/src/main/scala/zio/http/codec/PathCodec.scala index b2d16cc548..ea56ec1905 100644 --- a/zio-http/src/main/scala/zio/http/codec/PathCodec.scala +++ b/zio-http/src/main/scala/zio/http/codec/PathCodec.scala @@ -324,25 +324,16 @@ sealed trait PathCodec[A] { self => override def toString(): String = render - final def transform[A2](f: A => A2, g: A2 => A): PathCodec[A2] = + final def transform[A2](f: A => A2)(g: A2 => A): PathCodec[A2] = PathCodec.TransformOrFail[A, A2](self, in => Right(f(in)), output => Right(g(output))) - final def transformOrFail[A2]( - f: A => Either[String, A2], - g: A2 => Either[String, A], - ): PathCodec[A2] = + final def transformOrFail[A2](f: A => Either[String, A2])(g: A2 => Either[String, A]): PathCodec[A2] = PathCodec.TransformOrFail[A, A2](self, f, g) - final def transformOrFailLeft[A2]( - f: A => Either[String, A2], - g: A2 => A, - ): PathCodec[A2] = + final def transformOrFailLeft[A2](f: A => Either[String, A2])(g: A2 => A): PathCodec[A2] = PathCodec.TransformOrFail[A, A2](self, f, output => Right(g(output))) - final def transformOrFailRight[A2]( - f: A => A2, - g: A2 => Either[String, A], - ): PathCodec[A2] = + final def transformOrFailRight[A2](f: A => A2)(g: A2 => Either[String, A]): PathCodec[A2] = PathCodec.TransformOrFail[A, A2](self, in => Right(f(in)), g) } object PathCodec { diff --git a/zio-http/src/main/scala/zio/http/codec/SegmentCodec.scala b/zio-http/src/main/scala/zio/http/codec/SegmentCodec.scala index b2f517dd71..24137c0ced 100644 --- a/zio-http/src/main/scala/zio/http/codec/SegmentCodec.scala +++ b/zio-http/src/main/scala/zio/http/codec/SegmentCodec.scala @@ -66,26 +66,17 @@ sealed trait SegmentCodec[A] { self => _render } - final def transform[A2](f: A => A2, g: A2 => A): PathCodec[A2] = - PathCodec.Segment(self).transform(f, g) - - final def transformOrFail[A2]( - f: A => Either[String, A2], - g: A2 => Either[String, A], - ): PathCodec[A2] = - PathCodec.Segment(self).transformOrFail(f, g) - - final def transformOrFailLeft[A2]( - f: A => Either[String, A2], - g: A2 => A, - ): PathCodec[A2] = - PathCodec.Segment(self).transformOrFailLeft(f, g) - - final def transformOrFailRight[A2]( - f: A => A2, - g: A2 => Either[String, A], - ): PathCodec[A2] = - PathCodec.Segment(self).transformOrFailRight(f, g) + final def transform[A2](f: A => A2)(g: A2 => A): PathCodec[A2] = + PathCodec.Segment(self).transform(f)(g) + + final def transformOrFail[A2](f: A => Either[String, A2])(g: A2 => Either[String, A]): PathCodec[A2] = + PathCodec.Segment(self).transformOrFail(f)(g) + + final def transformOrFailLeft[A2](f: A => Either[String, A2])(g: A2 => A): PathCodec[A2] = + PathCodec.Segment(self).transformOrFailLeft(f)(g) + + final def transformOrFailRight[A2](f: A => A2)(g: A2 => Either[String, A]): PathCodec[A2] = + PathCodec.Segment(self).transformOrFailRight(f)(g) } object SegmentCodec { def bool(name: String): SegmentCodec[Boolean] = SegmentCodec.BoolSeg(name) diff --git a/zio-http/src/test/scala/zio/http/codec/PathCodecSpec.scala b/zio-http/src/test/scala/zio/http/codec/PathCodecSpec.scala index 25c3313852..8773bf109a 100644 --- a/zio-http/src/test/scala/zio/http/codec/PathCodecSpec.scala +++ b/zio-http/src/test/scala/zio/http/codec/PathCodecSpec.scala @@ -55,15 +55,13 @@ object PathCodecSpec extends ZIOHttpSpec { test("transformed") { val codec = PathCodec.path("/users") / - SegmentCodec.int("user-id").transform(UserId.apply, (uid: UserId) => uid.value) / + SegmentCodec.int("user-id").transform(UserId.apply)(_.value) / SegmentCodec.literal("posts") / SegmentCodec .string("post-id") - .transformOrFailLeft( - s => Try(s.toInt).toEither.left.map(_ => "Not a number").map(n => PostId(n.toString)), - (pid: PostId) => pid.value, - ) - + .transformOrFailLeft(s => + Try(s.toInt).toEither.left.map(_ => "Not a number").map(n => PostId(n.toString)), + )(_.value) assertTrue(codec.segments.length == 5) }, ), @@ -104,15 +102,13 @@ object PathCodecSpec extends ZIOHttpSpec { test("transformed") { val codec = PathCodec.path("/users") / - SegmentCodec.int("user-id").transform(UserId.apply, (uid: UserId) => uid.value) / + SegmentCodec.int("user-id").transform(UserId.apply)(_.value) / SegmentCodec.literal("posts") / SegmentCodec .string("post-id") - .transformOrFailLeft( - s => Try(s.toInt).toEither.left.map(_ => "Not a number").map(n => PostId(n.toString)), - (pid: PostId) => pid.value, - ) - + .transformOrFailLeft(s => + Try(s.toInt).toEither.left.map(_ => "Not a number").map(n => PostId(n.toString)), + )(_.value) assertTrue( codec.decode(Path("/users/1/posts/456")) == Right((UserId(1), PostId("456"))), codec.decode(Path("/users/1/posts/abc")) == Left("Not a number"), @@ -156,14 +152,13 @@ object PathCodecSpec extends ZIOHttpSpec { test("transformed") { val codec = PathCodec.path("/users") / - SegmentCodec.int("user-id").transform(UserId.apply, (uid: UserId) => uid.value) / + SegmentCodec.int("user-id").transform(UserId.apply)(_.value) / SegmentCodec.literal("posts") / SegmentCodec .string("post-id") - .transformOrFailLeft( - s => Try(s.toInt).toEither.left.map(_ => "Not a number").map(n => PostId(n.toString)), - (pid: PostId) => pid.value, - ) + .transformOrFailLeft(s => + Try(s.toInt).toEither.left.map(_ => "Not a number").map(n => PostId(n.toString)), + )(_.value) assertTrue( codec.render == "/users/{user-id}/posts/{post-id}",