0

我正在使用 Play 框架和 ReactiveMongoDB。我正在尝试为我的名为 Customer 的班级写一个读者和一个作家。这一切都在 models.scala 文件中按以下顺序完成:

import reactivemongo.bson._


case class StreetAddressLine(
  id: Option[BSONObjectID],
  StreetAddressLine: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object StreetAddressLine {
  implicit object StreetAddressLineBSONReader extends BSONDocumentReader[StreetAddressLine] {
    def read(doc: BSONDocument): StreetAddressLine =
      StreetAddressLine(
        doc.getAs[BSONObjectID]("_id"), 
        doc.getAs[String]("StreetAddressLine").get, 
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }
}  



case class PrimaryAddress(
  id: Option[BSONObjectID],
  StreetAddressLine: List[StreetAddressLine],
  PrimaryTownName: String,
  CountryISOAlpha2Code: String,
  PostalCode: String,
  PostalCodeExtensionCode: String,
  TerritoryOfficialName: String,
  TerritoryAbbreviatedName: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object PrimaryAddress {
  implicit object PrimaryAddressBSONReader extends BSONDocumentReader[PrimaryAddress] {
    def read(doc: BSONDocument): PrimaryAddress =
      PrimaryAddress(
        doc.getAs[BSONObjectID]("_id"), //Mongos internal identifier
        doc.getAs[List[StreetAddressLine]]("StreetAddressLine").get, 
        doc.getAs[String]("PrimaryTownName").get,        
        doc.getAS[String]("CountryISOAlpha2Code").get, 
        doc.getAS[String]("PostalCode").get,      
        doc.getAs[String]("PostalCodeExtensionCode").get,  
        doc.getAs[String]("TerritoryOfficialName").get,     
        doc.getAs[String]("TerritoryAbbreviatedName").get,  
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
  }
}  

但我收到一个错误

[error] C:\Users\xxxxx\git\oneid-scala\oneid-scala\app\models\models.scala:54:
 value getAS is not a member of reactivemongo.bson.BSONDocument
[error]         doc.getAS[String]("CountryISOAlpha2Code").get,
[error]             ^
[error] C:\Users\xxxxx\git\oneid-scala\oneid-scala\app\models\models.scala:55:
 value getAS is not a member of reactivemongo.bson.BSONDocument
[error]         doc.getAS[String]("PostalCode").get,
[error]  

对于以下几行

doc.getAS[String]("PostalCode").get,      
doc.getAs[String]("PostalCodeExtensionCode").get,

我决定将作者添加到

package models

import org.jboss.netty.buffer._
import org.joda.time.DateTime
import play.api.data._
import play.api.data.Forms._
import play.api.data.format.Formats._
import play.api.data.validation.Constraints._

import reactivemongo.bson._





case class StreetAddressLine(
  id: Option[BSONObjectID],
  StreetAddressLine: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object StreetAddressLine {
  implicit object StreetAddressLineBSONReader extends BSONDocumentReader[StreetAddressLine] {
    def read(doc: BSONDocument): StreetAddressLine =
      StreetAddressLine(
        doc.getAs[BSONObjectID]("_id"), 
        doc.getAs[String]("StreetAddressLine").get, 
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }
 implicit object StreetAddressLineBSONWriter extends BSONDocumentWriter[StreetAddressLine] {
    def write(streetAddressLine: StreetAddressLine): BSONDocument =
      BSONDocument(
        "_id" -> streetAddressLine.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> streetAddressLine.StreetAddressLine,
        "creationDate" -> streetAddressLine.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> streetAddressLine.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 

}  



case class PrimaryAddress(
  id: Option[BSONObjectID],
  StreetAddressLine: List[StreetAddressLine],
  PrimaryTownName: String,
  CountryISOAlpha2Code: String,
  PostalCode: String,
  PostalCodeExtensionCode: String,
  TerritoryOfficialName: String,
  TerritoryAbbreviatedName: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object PrimaryAddress {

 implicit object PrimaryAddressBSONReader extends BSONDocumentReader[PrimaryAddress] {
   def read(doc: BSONDocument): PrimaryAddress =
     PrimaryAddress(
       doc.getAs[BSONObjectID]("_id"), 
       doc.getAs[String]("StreetAddressLine").get, 
       doc.getAs[String]("PrimaryTownName").get,
       doc.getAs[String]("CountryISOAlpha2Code").get, 
       doc.getAs[String]("PostalCode").get, 
       doc.getAs[String]("PostalCodeExtensionCode").get, 
       doc.getAs[String]("TerritoryOfficialName").get, 
       doc.getAs[String]("TerritoryAbbreviatedName").get, 
       doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
       doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }  


 implicit object PrimaryAddressBSONWriter extends BSONDocumentWriter[PrimaryAddress] {
    def write(primaryAddress: PrimaryAddress): BSONDocument =
      BSONDocument(
        "_id" -> primaryAddress.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> primaryAddress.StreetAddressLine,
        "PrimaryTownName" -> primaryAddress.PrimaryTownName,
        "CountryISOAlpha2Code" -> primaryAddress.CountryISOAlpha2Code,
        "PostalCode" -> primaryAddress.PostalCode,
        "TerritoryOfficialName" -> primaryAddress.TerritoryOfficialName,
        "TerritoryAbbreviatedName" -> primaryAddress.TerritoryAbbreviatedName,
        "creationDate" -> primaryAddress.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> primaryAddress.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 



}  

并且编译错误更改为以下

[info] Compiling 2 Scala sources to C:\Users\xxxxx\git\oneid-scala\oneid-scala
\target\scala-2.11\classes...
[error] C:\Users\xxxxx\git\oneid-scala\oneid-scala\app\models\models.scala:62:
 type mismatch;
[error]  found   : String
[error]  required: List[models.StreetAddressLine]
[error]  Note: implicit object PrimaryAddressBSONWriter is not applicable here b
ecause it comes after the application point and it lacks an explicit result type

[error]        doc.getAs[String]("StreetAddressLine").get,
[error]                                               ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed Jul 11, 2015 12:40:20 PM
4

1 回答 1

1

谢谢 cchantep,下面的代码现在可以编译了。我发现这里的答案有两个部分。

A. 为了在另一个类中嵌入自定义类,必须定义每个定义的类。

B. 必须已经为所有类定义了编写器和读取器,否则会出现编译器错误。你不能只定义没有作者的读者。

以下代码编译良好

package models

import org.jboss.netty.buffer._
import org.joda.time.DateTime
import play.api.data._
import play.api.data.Forms._
import play.api.data.format.Formats._
import play.api.data.validation.Constraints._

import reactivemongo.bson._





case class StreetAddressLine(
  id: Option[BSONObjectID],
  StreetAddressLine: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object StreetAddressLine {
  implicit object StreetAddressLineBSONReader extends BSONDocumentReader[StreetAddressLine] {
    def read(doc: BSONDocument): StreetAddressLine =
      StreetAddressLine(
        doc.getAs[BSONObjectID]("_id"), 
        doc.getAs[String]("StreetAddressLine").get, 
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }
 implicit object StreetAddressLineBSONWriter extends BSONDocumentWriter[StreetAddressLine] {
    def write(streetAddressLine: StreetAddressLine): BSONDocument =
      BSONDocument(
        "_id" -> streetAddressLine.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> streetAddressLine.StreetAddressLine,
        "creationDate" -> streetAddressLine.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> streetAddressLine.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 

}  



case class PrimaryAddress(
  id: Option[BSONObjectID],
  StreetAddressLine: List[StreetAddressLine],
  PrimaryTownName: String,
  CountryISOAlpha2Code: String,
  PostalCode: String,
  PostalCodeExtensionCode: String,
  TerritoryOfficialName: String,
  TerritoryAbbreviatedName: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object PrimaryAddress {

 implicit object PrimaryAddressBSONReader extends BSONDocumentReader[PrimaryAddress] {
   def read(doc: BSONDocument): PrimaryAddress =
     PrimaryAddress(
       doc.getAs[BSONObjectID]("_id"), 
       doc.getAs[List[StreetAddressLine]]("StreetAddressLine").get, 
       doc.getAs[String]("PrimaryTownName").get,
       doc.getAs[String]("CountryISOAlpha2Code").get, 
       doc.getAs[String]("PostalCode").get, 
       doc.getAs[String]("PostalCodeExtensionCode").get, 
       doc.getAs[String]("TerritoryOfficialName").get, 
       doc.getAs[String]("TerritoryAbbreviatedName").get, 
       doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
       doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }  



 implicit object PrimaryAddressBSONWriter extends BSONDocumentWriter[PrimaryAddress] {
    def write(primaryAddress: PrimaryAddress): BSONDocument =
      BSONDocument(
        "_id" -> primaryAddress.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> primaryAddress.StreetAddressLine,
        "PrimaryTownName" -> primaryAddress.PrimaryTownName,
        "CountryISOAlpha2Code" -> primaryAddress.CountryISOAlpha2Code,
        "PostalCode" -> primaryAddress.PostalCode,
        "TerritoryOfficialName" -> primaryAddress.TerritoryOfficialName,
        "TerritoryAbbreviatedName" -> primaryAddress.TerritoryAbbreviatedName,
        "creationDate" -> primaryAddress.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> primaryAddress.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 



}  
于 2015-07-11T19:49:46.140 回答