2

I have the following:

Account.scala

package modules.accounts

import java.time.Instant
import reactivemongo.api.bson._

case class Account(id: String, name: String)

object Account {
  type ID = String

  implicit val accountHandler: BSONDocumentHandler[Account] = Macros.handler[Account]

//   implicit def accountWriter: BSONDocumentWriter[Account] = Macros.writer[Account]
//   implicit def accountReader: BSONDocumentReader[Account] = Macros.reader[Account]
}

AccountRepo.scala

package modules.accounts

import java.time.Instant
import reactivemongo.api.collections.bson.BSONCollection

import scala.concurrent.ExecutionContext

final class AccountRepo(
    val coll: BSONCollection
)(implicit ec: ExecutionContext) {
  import Account.{ accountHandler, ID }

  def insertTest() = {
    val doc = Account(s"account123", "accountName") //, Instant.now)
    coll.insert.one(doc)
  }
}

The error I am getting is:

could not find implicit value for parameter writer: AccountRepo.this.coll.pack.Writer[modules.accounts.Account]
[error]     coll.insert.one(doc)

From what I understand the implicit handler that is generated by the macro should be enough and create the Writer. What am I doing wrong?

Reference: http://reactivemongo.org/releases/1.0/documentation/bson/typeclasses.html

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • It looks like the compiler cannot find a `Writer[_]`, probably the macro code needs that `Writer` in scope to build a `BSONDocumentHandler[Account]`. Make sure you have a `AccountRepo.this.coll.pack.Writer[modules.accounts.Account]` in scope when calling that macro. – simao Jul 03 '21 at 21:03
  • 1
    Can't reproduce. The code seems to compile https://scastie.scala-lang.org/jILAbk8nSNGQgIIC2lHcpw Probably you haven't provided the whole code (minimal reproducible example). – Dmytro Mitin Jul 04 '21 at 09:08
  • The code compiles fine. If you are getting this error when running within Intellij, it might be due to outdated or conflicting compiled files (which happens due to some bug in intellij plugin), then delete your target folders and let intellij recomiple everything from scratch. – sarveshseri Jul 04 '21 at 11:29

1 Answers1

4

The code is mismixing different versions.

The macro generated handler is using the new BSON API, as it can be seen with the import reactivemongo.api.bson, whereas the collection is using an old driver, as it can be seen as it uses reactivemongo.api.collections.bson instead of reactivemongo.api.bson.collection.

It's recommended to have a look at the documentation, and not mixing incompatible versions of related libraries.

cchantep
  • 9,118
  • 3
  • 30
  • 41