2016-04-14 34 views
1

是否有可能做這樣的事情在斯卡拉/播放2.4:播放2.4參數代數數據類型JSON驗證

sealed trait Location { val `type`: String } 
case class CityLocation(zip_code: String, override val `type`: String = "city") extends Location 
case class AddressLocation(
    society: Option[String], 
    first_name: String, 
    last_name: String, 
    ... 
    override val `type`: String = "address" 
) extends Location 

sealed trait Point[T <: Location] { val `type`: String; val location: T } 
case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T] 
case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T] 

object CityLocation { 
    implicit val format = Json.format[CityLocation] 
} 
object AddressLocation { 
    implicit val format = Json.format[AddressLocation] 
} 
object ShippingPoint { 
    implicit def write[T] = Json.writes[ShippingPoint[T]] 
    implicit def read[T]: Reads[ShippingPoint[T]] = (
    (__ \ "location").read[T] and 
     (__ \ "type").read[String](exactWordRead("shipping", "error.route.shipping.type")) 
)(ShippingPoint[T].apply _) 
} 
object PickupPoint { 
    implicit def write[T] = Json.writes[ShippingPoint[T]] 
    implicit def read[T]: Reads[PickupPoint[T]] = (
    (__ \ "location").read[T] and 
     (__ \ "type").read[String](exactWordRead("pickup", "error.route.pickup.type")) 
)(PickupPoint[T].apply _) 
} 

現在,它不編譯。

exactWordRead方法定義如下:

def exactWordRead(word: String, errorMessage: String): Reads[String] = Reads.constraints.pattern(s"^$word${"$"}".r, errorMessage) 

編輯

看來我女僕向前邁進了一步感謝這個答案https://stackoverflow.com/a/29834113/2431728

sealed trait Location { val `type`: String } 
case class CityLocation(zip_code: String, override val `type`: String = "city") extends Location 
case class AddressLocation(
    society: Option[String], 
    first_name: String, 
    last_name: String, 
    ... 
    override val `type`: String = "address" 
) extends Location 

sealed trait Point[T <: Location] { val location: T; val `type`: String } 
case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T] 
case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T] 

object CityLocation { 
    implicit val format = Json.format[CityLocation] 
} 
object AddressLocation { 
    implicit val format = Json.format[AddressLocation] 
} 
object ShippingPoint { 
    implicit def format[T: Format]: Format[ShippingPoint[T]] = (
    (__ \ "location").format[T] and 
     (__ \ "type").format[String](exactWordRead("shipping", "error.route.shipping.type")) 
)(ShippingPoint.apply, unlift(ShippingPoint.unapply)) 
} 
object PickupPoint { 
    implicit def format[T: Format]: Format[PickupPoint[T]] = (
    (__ \ "location").format[T] and 
     (__ \ "type").format[String](exactWordRead("pickup", "error.route.pickup.type")) 
)(PickupPoint.apply, unlift(PickupPoint.unapply)) 
} 

但是,我不'還沒有編譯。編譯錯誤是:

[error] type arguments [T] do not conform to trait Point's type parameter bounds [T <: services.coliswebApi.data.Location] 
[error] case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T] 
[error]                          ^
[error] type arguments [T] do not conform to trait Point's type parameter bounds [T <: services.coliswebApi.data.Location] 
[error] case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T] 

回答

1

我想你也需要在你的格式結合你的類型,這樣

implicit def format[T <: Location: Format]: Format[ShippingPoint[T]] = (
     (__ \ "location").format[T] and 
     (__ \ "type").format[String](exactWordRead("shipping", "error.route.shipping.type")) 
    )(ShippingPoint.apply, unlift(ShippingPoint.unapply)) 
+1

它編譯! Thaaaanks很多= D –

2

至於說由編譯器,一方面你有,

sealed trait Point[T <: Location] 

...並在另一側,

case class ShippingPoint[T](???) extends Point[T] 
case class PickupPoint[T](???) extends Point[T] 

但沒有在ShippingPointPickupPoint的聲明證明類型參數與約束條件<: Location兼容,需要擴展Point

所以你需要解決這個問題。

case class ShippingPoint[T <: Location](???) extends Point[T] 
case class PickupPoint[T <: Location](???) extends Point[T] 
+0

的確。 但現在它是不編譯的格式。你有關於如何糾正它們的想法嗎? –

+1

原因是一樣的。 – cchantep

+0

確實。感謝您的幫助=) –