1
我正在使用Play框架和ReactiveMongo。我正在爲我的班級寫一篇名爲Platforms的讀者和作家。我試圖使用我創建的類型作爲scala枚舉,但我不知道應如何定義讀寫器語法。有人能幫我弄清楚正確的語法嗎?如何將ReactiveMongo與枚舉一起使用?
import reactivemongo.bson._
sealed trait PlatformType { def name: String }
case object PROPER extends PlatformType { val name = "PROPER" }
case object TRANSACT extends PlatformType { val name = "TRANSACT" }
case object UPPER extends PlatformType { val name = "UPPPER" }
case class Platforms(
id: Option[BSONObjectID],
Platform: PlatformType,
Active: Boolean,
SystemIds:List[String],
creationDate: Option[DateTime],
updateDate: Option[DateTime])
object Platforms {
implicit object PlatformsBSONReader extends BSONDocumentReader[Platforms] {
def read(doc: BSONDocument): Platforms =
Platforms(
doc.getAs[BSONObjectID]("_id"),
doc.getAs[PlatformType]("Platform").get,
doc.getAs[Boolean]("Active").get,
doc.getAs[List[String]]("SystemIds").get,
doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
}
implicit object PlatformsBSONWriter extends BSONDocumentWriter[Platforms] {
def write(platforms: Platforms): BSONDocument =
BSONDocument(
"_id" -> platforms.id.getOrElse(BSONObjectID.generate),
"Platform" -> platforms.Platform,
"Active" -> platforms.Active,
"SystemIds" -> platforms.SystemIds,
"creationDate" -> platforms.creationDate.map(date => BSONDateTime(date.getMillis)),
"updateDate" -> platforms.updateDate.map(date => BSONDateTime(date.getMillis)))
}
}
沒有什麼特定的枚舉類型提供BSON類型類。這是您可以提供'BSONWriter'和'BSONReader'的普通特質或類。 – cchantep
你能告訴我你將使用的語法嗎?我是scala的新手,我對如何定義枚舉有點困惑。 – pitchblack408