Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override `org.elasticsearch.client.FilterClient#doExecute()` in Scala?

Specifically with "org.elasticsearch" % "elasticsearch" % "2.4.x" (it does work with later versions of ElasticSearch where ActionRequest no longer takes a parameter type, but we can't update to those just yet!).

The Java method we're trying to override is defined like this (source):

protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
    in().execute(action, request, listener);
}

The naive attempt to override it...

import org.elasticsearch.action._
import org.elasticsearch.client.{Client, FilterClient}

class DemoFilterClient(underlyingClient: Client) extends FilterClient(underlyingClient) {
  override def doExecute[
    Request <: ActionRequest[_],
    Response <: ActionResponse,
    RequestBuilder <: ActionRequestBuilder[Request, Response, RequestBuilder]
  ](
    action: Action[Request, Response, RequestBuilder],
    request: Request,
    listener: ActionListener[Response]
  ) = super.doExecute(action, request, listener)
}

...fails in all versions of Scala we've tried (2.11.11, 2.11.12, and 2.12.4):

[info] Compiling 1 Scala source to /home/roberto/development/elasticsearch-scala-client-test/target/scala-2.11/classes ...
[error] /home/roberto/development/elasticsearch-scala-client-test/src/main/scala/com/gu/DemoFilterClient.scala:7:101: type arguments [Request,Response,RequestBuilder] do not conform to class ActionRequestBuilder's type parameter bounds [Request <: org.elasticsearch.action.ActionRequest[_ <: org.elasticsearch.action.ActionRequest[_ <: org.elasticsearch.action.ActionRequest[_ <: AnyRef]]],Response <: org.elasticsearch.action.ActionResponse,RequestBuilder <: org.elasticsearch.action.ActionRequestBuilder[Request,Response,RequestBuilder]]
[error]   override def doExecute[Request <: ActionRequest[_], Response <: ActionResponse, RequestBuilder <: ActionRequestBuilder[Request, Response, RequestBuilder]](action: Action[Request, Response, RequestBuilder], request: Request, listener: ActionListener[Response]) = super.doExecute(action, request, listener)
[error]                                                                                                     ^
[error] one error found

To lay out that compiler error more clearly:

type arguments
[Request,Response,RequestBuilder]
do not conform to class ActionRequestBuilder's type parameter bounds
[
  Request <: 
    org.elasticsearch.action.ActionRequest[_ <: 
      org.elasticsearch.action.ActionRequest[_ <: 
        org.elasticsearch.action.ActionRequest[_ <: AnyRef]]],
  Response <: org.elasticsearch.action.ActionResponse,
  RequestBuilder <: org.elasticsearch.action.ActionRequestBuilder[Request,Response,RequestBuilder]
]

Note that amazing recursive declaration on the Request type!

sample sbt project available at https://github.com/guardian/elasticsearch-scala-client-test

like image 801
Roberto Tyley Avatar asked Jan 18 '18 12:01

Roberto Tyley


1 Answers

I've run into situations where you have this kind of F-bounded polymorphism with a raw Java type before, and if I remember correctly something like this should work (at least it compiles in your example project):

package com.gu

import org.elasticsearch.action._
import org.elasticsearch.client.{Client, FilterClient}

class DemoFilterClient(underlyingClient: Client)
    extends FilterClient(underlyingClient) {

  type AR[x <: ActionRequest[x]] = ActionRequest[x]

  override def doExecute[
    Request <: AR[_],
    Response <: ActionResponse,
    RequestBuilder <: ActionRequestBuilder[Request, Response, RequestBuilder]
  ](
    action: Action[Request, Response, RequestBuilder],
    request: Request,
    listener: ActionListener[Response]
  ) = super.doExecute(action, request, listener)
}

It seems like it should also be possible to do this inline with a forSome, but none of the iterations I've tried just now have worked.

like image 147
Travis Brown Avatar answered Oct 14 '22 13:10

Travis Brown