diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a21f8d9192..dd12575b2f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -25,6 +25,9 @@ New Features * finagle-core: introduce type-safe `ReqRep` variant ``PHAB_ID=D520027`` +* finagle-core: Added a new variant of `Filter.andThenIf` which allows passing the parameters + as individual parameters instead of a Scala tuple. ``PHAB_ID=D523010`` + Breaking API Changes ~~~~~~~~~~~~~~~~~~~~ diff --git a/finagle-core/src/main/scala/com/twitter/finagle/Filter.scala b/finagle-core/src/main/scala/com/twitter/finagle/Filter.scala index 4b90308e6d..492e332357 100644 --- a/finagle-core/src/main/scala/com/twitter/finagle/Filter.scala +++ b/finagle-core/src/main/scala/com/twitter/finagle/Filter.scala @@ -127,7 +127,23 @@ abstract class Filter[-ReqIn, +RepOut, +ReqOut, -RepIn] /** * Conditionally propagates requests down the filter chain. This may - * useful if you are statically wiring together filter chains based + * be useful if you are statically wiring together filter chains based + * on a configuration file, for instance. + * + * @param conditional a boolean value indicating whether the filter should be + * included in the filter chain. + * @param filter the filter to be conditionally included. + */ + def andThenIf[Req2 >: ReqOut, Rep2 <: RepIn]( + conditional: Boolean, + filter: Filter[ReqOut, RepIn, Req2, Rep2] + ): Filter[ReqIn, RepOut, Req2, Rep2] = + if (conditional) andThen(filter) + else this + + /** + * Conditionally propagates requests down the filter chain. This may + * be useful if you are statically wiring together filter chains based * on a configuration file, for instance. * * @param condAndFilter a tuple of boolean and filter. diff --git a/finagle-core/src/test/scala/com/twitter/finagle/FilterTest.scala b/finagle-core/src/test/scala/com/twitter/finagle/FilterTest.scala index 0317c57ad9..bf1d3c6637 100644 --- a/finagle-core/src/test/scala/com/twitter/finagle/FilterTest.scala +++ b/finagle-core/src/test/scala/com/twitter/finagle/FilterTest.scala @@ -379,20 +379,34 @@ class FilterTest extends FunSuite { verify(spied).apply(any[ClientConnection]) } - test("Filter.andThenIf: applies next filter when true") { + test("Filter.andThenIf (tuple): applies next filter when true") { val spied = spy(new PassThruFilter) val svc = (new PassThruFilter).andThenIf((true, spied)).andThen(constSvc) await(svc(4)) verify(spied).apply(any[Int], any[Service[Int, Int]]) } - test("Filter.andThenIf: doesn't apply next filter when false") { + test("Filter.andThenIf (tuple): doesn't apply next filter when false") { val spied = spy(new PassThruFilter) val svc = (new PassThruFilter).andThenIf((false, spied)).andThen(constSvc) await(svc(4)) verify(spied, never).apply(any[Int], any[Service[Int, Int]]) } + test("Filter.andThenIf (params): applies next filter when true") { + val spied = spy(new PassThruFilter) + val svc = (new PassThruFilter).andThenIf(true, spied).andThen(constSvc) + await(svc(4)) + verify(spied).apply(any[Int], any[Service[Int, Int]]) + } + + test("Filter.andThenIf (params): doesn't apply next filter when false") { + val spied = spy(new PassThruFilter) + val svc = (new PassThruFilter).andThenIf(false, spied).andThen(constSvc) + await(svc(4)) + verify(spied, never).apply(any[Int], any[Service[Int, Int]]) + } + test("Filter.choose: apply the underlying filter to certain requests") { val spied = spy(new PassThruFilter) val svc = Filter