2

我使用 ReactiveMongo 驱动程序定义BSONDocumentWriter了 s 来将域对象(案例类)映射到要在 MongoDB 中持久保存的 BSON 文档。对于案例类来说,定义作者是非常直接的(虽然乏味且容易出错:我希望有一个类似 Salat 的解决方案)。但是,我似乎无法对 a 做同样的事情Map[String,Any](其中值可以是数字、日期或字符串类型)。我找到了一个为地图定义通用编写器(和读取器)的代码示例

implicit def MapWriter[V](implicit vw: BSONDocumentWriter[V]): BSONDocumentWriter[Map[String, V]] = 
  new BSONDocumentWriter[Map[String, V]] {
  def write(map: Map[String, V]): BSONDocument = {
    val elements = map.toStream.map { tuple =>
      tuple._1 -> vw.write(tuple._2)
    }
    BSONDocument(elements)
  }
}

BSONDocumentWriter但是如果type没有隐式,则这不起作用V,即代码段:

BSONDocument(
  "_id" -> "asd",
  "map" -> MapWriter[Any].write(Map("x" -> 1, "y" -> "2"))
)

不编译:

could not find implicit value for parameter vw: reactivemongo.bson.BSONDocumentWriter[Any]
    "map" -> MapWriter[Any].write(Map("x" -> 1, "y" -> "2"))
                      ^

我想也许作者应该写 aBSONValue而不是 aBSONDocument所以我修改了这个例子如下:

implicit def ValueMapWriter[V](implicit vw: BSONWriter[V, BSONValue]): BSONDocumentWriter[Map[String, V]] = 
  new BSONDocumentWriter[Map[String, V]] {
  def write(map: Map[String, V]): BSONDocument = {
    val elements = map.toStream.map {
      tuple =>
        tuple._1 -> vw.write(tuple._2)
    }
    BSONDocument(elements)
  }
}

为了简单起见,我尝试将其Int用作值类型,但再次使用以下代码段:

BSONDocument(
  "_id" -> "asd",
  "map" -> ValueMapWriter[Int].write(Map("x" -> 1, "y" -> 2))
)

不编译:

could not find implicit value for parameter vw: reactivemongo.bson.BSONWriter[Int,reactivemongo.bson.BSONValue]
    "map" -> ValueMapWriter[Int].write(Map("x" -> 1, "y" -> 2))
                          ^

如果上述方法有效,我可以使用一些基类作为值类型并定义其隐式编写器。

我不确定为什么会发生这种情况以及如何解决它。也许我错过了一些明显的东西?想法?

4

1 回答 1

1

ValueMapWriter定义中BSONValue的泛型类型参数边界不正确。如果换行

implicit def ValueMapWriter[V](implicit vw: BSONWriter[V, BSONValue]): BSONDocumentWriter[Map[String, V]] =

implicit def ValueMapWriter[V](implicit vw: BSONWriter[V, _ <: BSONValue]): BSONDocumentWriter[Map[String, V]] =

那么它应该解析 Int 的隐式编写器。

BTW simple-reactivemongo已经这样做了。而且我还计划将此功能添加到ReactiveMongo Extensions

于 2014-05-03T21:08:50.200 回答