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