2

I've a very simple case class which is part of a bigger case class.

case class PublisherStatus(status: String)
case class Publisher(id: Option[BSONObjectID], name: String, status: PublisherStatus, keywords: List[String], updatedAt: Option[DateTime], outputChannelIP: String, outputChannelName: String)

and I've defined BSON Reader and Writer for it as follows.

  implicit object StatusBSONReader extends BSONDocumentReader[PublisherStatus] {
    def read(doc: BSONDocument): PublisherStatus =
      PublisherStatus(
        doc.getAs[String]("status").get
      )
  }

  implicit object StatusBSONWriter extends BSONDocumentWriter[PublisherStatus] {
    def write(status: PublisherStatus): BSONDocument =
      BSONDocument(
        "status" -> status.status)
  }

However, when I try to do the following I get a compile error.

  def updateState(id: String, s: String) {
    import models.PublisherBSONWriter._
    implicit val statusWrites = Json.writes[PublisherStatus]
    val objectId = new BSONObjectID(id)
    val status = PublisherStatus(s)
    val modifier = BSONDocument("$set" -> status)

    val updateFuture = collection.update(BSONDocument("_id" -> objectId), modifier)

  }

Error -

could not find implicit value for parameter writer: reactivemongo.bson.BSONWriter[models.PublisherStatus, _ <: reactivemongo.bson.BSONValue]
[error]     val modifier = BSONDocument("$set" -> status)
[error]                                        ^
[error] one error found
[error] (compile:compile) Compilation failed
Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161

1 Answers1

5

The reader and writer for PublisherStatus should not be a BSONDocumentReader and BSONDocumentWriter, but rather a BSONReader and BSONWriter.

Or to make things even easier, use a BSONHandler. Something along the lines of

implicit val bson = new BSONHandler[BSONString, PublisherStatus] {
  def read(bson: BSONString) = PublisherStatus(bson.value)
  def write(status: PublisherStatus) = BSONString(status.status)
}
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156