Skip to content

Commit

Permalink
finagle-core: Java compat for TypeAgnostic.Identity
Browse files Browse the repository at this point in the history
Problem

Java users don't have a clean API to access `Filter.TypeAgnostic.Identity`.

Solution

Introduce `Filter.typeAgnosticIdentity()`.

JIRA Issues: CSL-7227

Differential Revision: https://phabricator.twitter.biz/D242006
  • Loading branch information
kevinoliver authored and jenkins committed Nov 14, 2018
1 parent bee5874 commit cff9aed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Expand Up @@ -7,8 +7,14 @@ Note that ``PHAB_ID=#`` and ``RB_ID=#`` correspond to associated messages in com
Unreleased
----------

New Features
~~~~~~~~~~~~

* finagle-core: Add Java compatibility for `c.t.f.Filter.TypeAgnostic.Identity`
via `c.t.f.Filter.typeAgnosticIdentity()`. ``PHAB_ID=D242006``

* finagle-thrift: Add `c.t.finagle.thrift.MethodMetadata` which provides a `LocalContext` Key
for setting information about the current Thrift method and an accessor for retrieving
for setting information about the current Thrift method and an accessor for retrieving
the currently set value. ``PHAB_ID=D241295``

18.11.0
Expand Down
22 changes: 20 additions & 2 deletions finagle-core/src/main/scala/com/twitter/finagle/Filter.scala
Expand Up @@ -300,9 +300,16 @@ object Filter {
}

object TypeAgnostic {

/**
* A pass-through [[TypeAgnostic]] that in turn uses a
* [[Filter.Identity]] for its implementation.
*
* @see [[Filter.typeAgnosticIdentity]] for Java compatibility.
*/
val Identity: TypeAgnostic =
new TypeAgnostic {
def toFilter[Req, Rep]: Filter[Req, Rep, Req, Rep] = identity[Req, Rep]
def toFilter[Req, Rep]: Filter[Req, Rep, Req, Rep] = Filter.identity[Req, Rep]

override def andThen(next: TypeAgnostic): TypeAgnostic = next

Expand All @@ -320,9 +327,20 @@ object Filter {
}
}

/**
* Returns a [[Filter]] that does nothing beyond passing inputs through
* to the [[Service]].
*/
def identity[Req, Rep]: SimpleFilter[Req, Rep] =
Identity.asInstanceOf[SimpleFilter[Req, Rep]]

/**
* A pass-through [[TypeAgnostic]] that in turn uses a
* [[Filter.Identity]] for its implementation.
*/
def typeAgnosticIdentity: TypeAgnostic =
TypeAgnostic.Identity

def mk[ReqIn, RepOut, ReqOut, RepIn](
f: (ReqIn, ReqOut => Future[RepIn]) => Future[RepOut]
): Filter[ReqIn, RepOut, ReqOut, RepIn] = new Filter[ReqIn, RepOut, ReqOut, RepIn] {
Expand All @@ -341,7 +359,7 @@ object Filter {
def choose[Req, Rep](
pf: PartialFunction[Req, Filter[Req, Rep, Req, Rep]]
): Filter[Req, Rep, Req, Rep] = new Filter[Req, Rep, Req, Rep] {
private[this] val const: (Req => SimpleFilter[Req, Rep]) =
private[this] val const: Req => SimpleFilter[Req, Rep] =
Function.const(Filter.identity[Req, Rep])

def apply(request: Req, service: Service[Req, Rep]): Future[Rep] =
Expand Down
Expand Up @@ -35,13 +35,13 @@ public Future<U> apply(T request, Service<T, U> service) {
};

passThruFilter.andThen(Filter.identity());
typeAgnosticPassThruFilter.andThen(Filter.TypeAgnostic$.MODULE$.Identity());
typeAgnosticPassThruFilter.andThen(Filter.typeAgnosticIdentity());

passThruFilter.agnosticAndThen(typeAgnosticPassThruFilter);
typeAgnosticPassThruFilter.andThen(passThruFilter);

Filter.<Object, String>identity().andThen(passThruFilter);
Filter.TypeAgnostic$.MODULE$.Identity().andThen(typeAgnosticPassThruFilter);
Filter.typeAgnosticIdentity().andThen(typeAgnosticPassThruFilter);

passThruFilter.andThen(constSvc);
typeAgnosticPassThruFilter.andThen(constSvc);
Expand Down Expand Up @@ -96,7 +96,7 @@ public String toString() {

assertEquals(
"com.twitter.finagle.TypeAgnosticPassThruFilter",
typeAgnosticPassThruFilter.andThen(Filter.TypeAgnostic$.MODULE$.Identity()).toString());
typeAgnosticPassThruFilter.andThen(Filter.typeAgnosticIdentity()).toString());

assertEquals(
"com.twitter.finagle.PassThruFilter"
Expand All @@ -114,7 +114,7 @@ public String toString() {

assertEquals(
"com.twitter.finagle.TypeAgnosticPassThruFilter",
Filter.TypeAgnostic$.MODULE$.Identity().andThen(typeAgnosticPassThruFilter).toString());
Filter.typeAgnosticIdentity().andThen(typeAgnosticPassThruFilter).toString());

assertEquals(
"com.twitter.finagle.PassThruFilter"
Expand Down

0 comments on commit cff9aed

Please sign in to comment.