3

読んだ後、ケースクラスのリーダー/ライターを作成しようとしています:

しかし、私はそれを機能させるのに問題があります。

複数の単語オブジェクトで構成できる LeadCategory があります。

package models

import org.joda.time.DateTime
import reactivemongo.bson._
import reactivemongo.bson.handlers.{BSONWriter, BSONReader}
import reactivemongo.bson.BSONDateTime
import reactivemongo.bson.BSONString

リードカテゴリ:

case class LeadCategory(
                         id: Option[BSONObjectID],
                         categoryId: Long,
                         categoryName: String,
                         creationDate: Option[DateTime],
                         updateDate: Option[DateTime],
                         words: List[Word]
                         )

object LeadCategory {

  implicit object LeadCategoryBSONReader extends BSONReader[LeadCategory] {
    def fromBSON(document: BSONDocument): LeadCategory = {
      val doc = document.toTraversable
      LeadCategory(
        doc.getAs[BSONObjectID]("_id"),
        doc.getAs[BSONLong]("caregoryId").get.value,
        doc.getAs[BSONString]("categoryName").get.value,
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)),
        doc.getAs[List[Word]]("words").toList.flatten)
    }
  }

  implicit object LeadCategoryBSONWriter extends BSONWriter[LeadCategory] {
    def toBSON(leadCategory: LeadCategory) = {
      BSONDocument(
        "_id" -> leadCategory.id.getOrElse(BSONObjectID.generate),
        "caregoryId" -> BSONLong(leadCategory.categoryId),
        "categoryName" -> BSONString(leadCategory.categoryName),
        "creationDate" -> leadCategory.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> leadCategory.updateDate.map(date => BSONDateTime(date.getMillis)),
        "words" -> leadCategory.words)
    }
  }

語:

case class Word(id: Option[BSONObjectID], word: String)

object Word {

  implicit object WordBSONReader extends BSONReader[Word] {
    def fromBSON(document: BSONDocument): Word = {
      val doc = document.toTraversable
      Word(
        doc.getAs[BSONObjectID]("_id"),
        doc.getAs[BSONString]("word").get.value
      )
    }
  }

  implicit object WordBSONWriter extends BSONWriter[Word] {
    def toBSON(word: Word) = {
      BSONDocument(
        "_id" -> word.id.getOrElse(BSONObjectID.generate),
        "word" -> BSONString(word.word)
      )
    }

  }    
}

次の場所でコンパイル エラーが発生します。

"words" -> leadCategory.words)

述べる:

Too many arguments for method apply(ChannelBuffer)
Type mismatch found: (String, List[Words]) required required implicits.Producer[(String, BsonValue)]

私は何を逃したのですか?多分私はドキュメントを誤解しました...

4

2 に答える 2

0

すべてを同じファイルに入れる場合は、「LeadCategory」の上にある「Word」暗黙オブジェクトを宣言してみてください。

Scala は List[Word] の暗黙的なオブジェクトを見つけることができません - ReactiveMongo 0.9 を使用していると仮定します。

于 2013-04-30T05:48:07.390 に答える