Skip to content

Commit

Permalink
change signature to allow type inference (#2406)
Browse files Browse the repository at this point in the history
* change signature to allow type inference

* format fix
  • Loading branch information
TomTriple authored Aug 29, 2023
1 parent 74c3220 commit 8182fd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 50 deletions.
17 changes: 4 additions & 13 deletions zio-http/src/main/scala/zio/http/codec/PathCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 11 additions & 20 deletions zio-http/src/main/scala/zio/http/codec/SegmentCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 12 additions & 17 deletions zio-http/src/test/scala/zio/http/codec/PathCodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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}",
Expand Down

0 comments on commit 8182fd3

Please sign in to comment.