ReactiveMongo:如何将宏处理程序写入Enumeration对象?

时间:2014-07-20 14:54:40

标签: scala-2.10 reactivemongo

我使用ReactiveMongo 0.10.0,我有以下用户案例类和性别Enumeration对象:

case class User(
                 _id: Option[BSONObjectID] = None,
                 name: String,
                 gender: Option[Gender.Gender] = None)

object Gender extends Enumeration {
  type Gender = Value
  val MALE = Value("male")
  val FEMALE = Value("female")
  val BOTH = Value("both")
}

我声明了两个隐式宏处理程序:

implicit val genderHandler = Macros.handler[Gender.Gender]

implicit val userHandler = Macros.handler[User]

但是,当我运行应用程序时,我收到以下错误:

Error:(123, 48) No apply function found for reactive.userservice.Gender.Gender
    implicit val genderHandler = Macros.handler[Gender.Gender]
                                               ^
Error:(125, 46) Implicit reactive.userservice.Gender.Gender for 'value gender' not found
    implicit val userHandler = Macros.handler[User]
                                         ^

有人知道如何将宏处理程序写入Enumeration对象吗?

提前致谢!

1 个答案:

答案 0 :(得分:6)

我偶然发现你的问题几次寻找同样的答案。我是这样做的:

import myproject.utils.EnumUtils
import play.api.libs.json.{Reads, Writes}
import reactivemongo.bson._

object DBExecutionStatus extends Enumeration {
  type DBExecutionStatus = Value

  val Error = Value("Error")
  val Started = Value("Success")
  val Created = Value("Running")

  implicit val enumReads: Reads[DBExecutionStatus] = EnumUtils.enumReads(DBExecutionStatus)

  implicit def enumWrites: Writes[DBExecutionStatus] = EnumUtils.enumWrites

  implicit object BSONEnumHandler extends BSONHandler[BSONString, DBExecutionStatus] {
    def read(doc: BSONString) = DBExecutionStatus.Value(doc.value)
    def write(stats: DBExecutionStatus) = BSON.write(stats.toString)
  }

}

您必须手动创建读/写对并填充您的值。 希望你已经解决了这个问题,考虑到问题年龄:D