Skip to content

Commit

Permalink
Merge pull request finagle#373 from finagle/vkostyukov/hnil-and-matcher
Browse files Browse the repository at this point in the history
Allow the Router to be mapped to value/future value
  • Loading branch information
vkostyukov committed Aug 4, 2015
2 parents a13609f + 24e15c0 commit 29c0204
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/src/main/scala/io/finch/route/Mapper.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.finch.route

import com.twitter.util.Future
import shapeless.HNil
import shapeless.ops.function.FnToProduct

/**
Expand Down Expand Up @@ -43,4 +44,14 @@ object Mapper extends MidPriorityMapperConversions {
type Out = B
def apply(r: Router[A]): Router[Out] = r.embedFlatMap(value => ev(ftp(f)(value)))
}

implicit def mapperFromValue[A](v: A): Mapper.Aux[HNil, A] = new Mapper[HNil] {
type Out = A
def apply(r: Router[HNil]): Router[Out] = r.map(_ => v)
}

implicit def mapperFromFutureValue[A](f: Future[A]): Mapper.Aux[HNil, A] = new Mapper[HNil] {
type Out = A
def apply(r: Router[HNil]): Router[Out] = r.embedFlatMap(_ => f)
}
}
7 changes: 7 additions & 0 deletions core/src/test/scala/io/finch/route/RouterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ class RouterSpec extends FlatSpec with Matchers with Checkers {
val r4: Router2[Int, Int] = Router.value(10) / Router.value(100)
val r5: Router[Int] = r4 { (a: Int, b: Int) => a + b }
val r6: Router[Int] = r4 { (a: Int, b: Int) => Future.value(a + b) }
val r7: Router0 = /
val r8: Router[String] = r7 { "foo" }
val r9: Router[String] = r7 { Future.value("foo") }


val route = Input(Request())
runRouter(r1, route) shouldBe Some((route, 100))
Expand All @@ -356,6 +360,9 @@ class RouterSpec extends FlatSpec with Matchers with Checkers {
runRouter(r4, route) shouldBe Some((route, 10 :: 100 :: HNil))
runRouter(r5, route) shouldBe Some((route, 110))
runRouter(r6, route) shouldBe Some((route, 110))
runRouter(r7, route) shouldBe Some((route, HNil))
runRouter(r8, route) shouldBe Some((route, "foo"))
runRouter(r9, route) shouldBe Some((route, "foo"))
}

"A string matcher" should "have the correct string representation" in {
Expand Down

0 comments on commit 29c0204

Please sign in to comment.