Mongodb 播放ReactiveMongo:需要使用ReactiveMongo为RawCommand创建通用结果类
我想使用带有play框架的被动mongo为RawCommand创建通用结果模型。但我犯了个错误。下面是我的通用模型案例类结构Mongodb 播放ReactiveMongo:需要使用ReactiveMongo为RawCommand创建通用结果类,mongodb,scala,reactive-programming,playframework-2.3,reactivemongo,Mongodb,Scala,Reactive Programming,Playframework 2.3,Reactivemongo,我想使用带有play框架的被动mongo为RawCommand创建通用结果模型。但我犯了个错误。下面是我的通用模型案例类结构 case class DistinctRawCommandResult[T] ( val values: List[T], val stats: CommandStatus, val ok: Double ) case class CommandStatus( val n: Int, val nscanned: Int, val nscannedObje
case class DistinctRawCommandResult[T] (
val values: List[T],
val stats: CommandStatus,
val ok: Double
)
case class CommandStatus(
val n: Int,
val nscanned: Int,
val nscannedObjects: Int,
val timems: Int,
val cursor: String
)
object DistinctRawCommandResultBsonFormatter {
implicit val commandStatusReader: BSONDocumentReader[CommandStatus] = Macros.reader[CommandStatus];
implicit val distinctRawCommandReader: BSONDocumentReader[DistinctRawCommandResult[T]] = Macros.reader[DistinctRawCommandResult[T]];
}
在第行implicit val distinctRawCommandReader:BSONDocumentReader[DistinctRawCommandResult[T]=Macros.reader[DistinctRawCommandResult[T]]生成错误:◾未找到:键入T,因为DistinctRawCommandResult接受参数。但是,当我使用objectdistinctrawcommandersultbsonformatter[T]时,这也会生成一个错误
如何为RawCommand创建通用结果模型 首先,如果类val修饰符是冗余的,并且;也在队伍的末尾 接下来,您不能用泛型类型定义一些值,因此Scala编译器生成错误not found:这一行中的类型t真的,什么类型t在代码中传递给这一行
implicit val distinctRawCommandReader: BSONDocumentReader[DistinctRawCommandResult[T]] = Macros.reader[DistinctRawCommandResult[T]];
要使用具有多种类型的读卡器,请使用import reactivemongo.bson.Macros.Options中的Macros.handlerOps_
有关更多文档,请参阅reactivemongo.bson.Macros.Options对象 下一版本将提供一个独特的命令。同时,您可以从master对代码进行编码:
//define generic top trait (must be sealed)
sealed trait MyGenericType
//trait implementations. Driver saves classname field to database to know, what instance of class need to create.
case class OneType(n: Int) extends MyGenericType
case class TwoType(s: String) extends MyGenericType
//and so on
case class DistinctRawCommandResult(values: List[MyGenericType],
stats: CommandStatus,
ok: Double)
case class CommandStatus(n: Int,
nscanned: Int,
nscannedObjects: Int,
timems: Int,
cursor: String)
object DistinctRawCommandResultBsonFormatter {
implicit val commandStatusReader = Macros.reader[CommandStatus]
// find all subtypes of MyGenericType
implicit val distinctRawCommandReader = Macros.handlerOpts[MyGenericType, AllImplementations]
}