Jas
Jas

Reputation: 15093

Couchbase scala observable n1ql query results with missing parameter type for expanded function ((x$12) => x$12.rows())

I'm trying to use the couchbase async bucket n1ql query as following:

I have an example of this non-compiling code below that you can run: (note I have to use scala: 2.11.7)

https://scastie.scala-lang.org/nkWOuCOrRbKbzieEK2D8yA

couchbaseBucket.async().query(N1qlQuery.simple(s"SELECT META(${couchbaseBucket.name()}).id FROM diy WHERE META(${couchbaseBucket.name()}).id LIKE MyKeysPrefix%;"))
  .flatMap(_.rows()) // missing parameter type for expanded function ((x$12) => x$12.rows())
  .map(result => result.asInstanceOf[AsyncN1qlQueryRow].value().toMap)
  .toList
  .timeout(10, TimeUnit.SECONDS)
  .toBlocking
  .single()

I get in the row: .flatMap(_.rows())

missing parameter type for expanded function ((x$12) => x$12.rows())

I tried updating it to be:

.flatMap(rows => rows.rows())

Then I get:

missing parameter type

So I tried updating to:

.flatMap((rows: AsyncN1qlQueryResult) => rows.rows())

Then I get:

overloaded method value flatMap with alternatives:

overloaded method value flatMap with alternatives:
[error]   [U, R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: U]], x$2: rx.functions.Func2[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ >: U, _ <: R], x$3: Int)rx.Observable[R] <and>
[error]   [U, R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: U]], x$2: rx.functions.Func2[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ >: U, _ <: R])rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x$2: rx.functions.Func1[_ >: Throwable, _ <: rx.Observable[_ <: R]], x$3: rx.functions.Func0[_ <: rx.Observable[_ <: R]], x$4: Int)rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x$2: rx.functions.Func1[_ >: Throwable, _ <: rx.Observable[_ <: R]], x$3: rx.functions.Func0[_ <: rx.Observable[_ <: R]])rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x$2: Int)rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]])rx.Observable[R]
[error]  cannot be applied to (com.couchbase.client.java.query.AsyncN1qlQueryResult => rx.Observable[com.couchbase.client.java.query.AsyncN1qlQueryRow])
[error]       .flatMap((rows: AsyncN1qlQueryResult) => rows.rows())
[error]        ^
[error] one error found

At this point, I was blown away by this error and I have no idea how to fix it... How do I fix it?

Upvotes: 0

Views: 329

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51648

Try

new MockCouchbaseServer()
  .createClient().async()
  .query(N1qlQuery.simple("SELECT META(somebucket).id FROM diy WHERE META(somebucket).id LIKE MyKeyPrefix%;"))
  .flatMap(new rx.functions.Func1[AsyncN1qlQueryResult, rx.Observable[AsyncN1qlQueryRow]] {
    override def call(rows: AsyncN1qlQueryResult): rx.Observable[AsyncN1qlQueryRow] = rows.rows()
  })
  .map[java.util.Map[String, Object]](new rx.functions.Func1[AsyncN1qlQueryRow, java.util.Map[String, Object]] {
    override def call(result: AsyncN1qlQueryRow): util.Map[String, Object] = result.value().toMap
  })
  .toList
  .timeout(10, TimeUnit.SECONDS)
  .toBlocking
  .single()

This code compiles with your imports at Scastie and the following build.sbt:

scalaVersion := "2.11.7"

libraryDependencies += "com.couchbase.client" % "java-client" % "2.5.8"
libraryDependencies += "com.couchbase.mock" % "CouchbaseMock" % "1.5.19"

Alternatively you can define implicit conversion manually and this simplifies the code:

implicit def convert[T, R](f: T => R): rx.functions.Func1[T, R] = (t: T) => f(t)

def main(args: Array[String]): Unit = {
  new MockCouchbaseServer()
    .createClient().async()
    .query(N1qlQuery.simple("SELECT META(somebucket).id FROM diy WHERE META(somebucket).id LIKE MyKeyPrefix%;"))
    .flatMap((rows: AsyncN1qlQueryResult) => rows.rows())
    .map[java.util.Map[String, Object]]((result: AsyncN1qlQueryRow) => result.value().toMap)
    .toList
    .timeout(10, TimeUnit.SECONDS)
    .toBlocking
    .single()
}

In Scala 2.12 it's enough to write:

new MockCouchbaseServer()
  .createClient().async()
  .query(N1qlQuery.simple("SELECT META(somebucket).id FROM diy WHERE META(somebucket).id LIKE MyKeyPrefix%;"))
  .flatMap((rows: AsyncN1qlQueryResult) => rows.rows())
  .map[java.util.Map[String, Object]](result => result.asInstanceOf[AsyncN1qlQueryRow].value().toMap)
  .toList
  .timeout(10, TimeUnit.SECONDS)
  .toBlocking
  .single()

Upvotes: 2

Related Questions