The syntax and meaning of Scala / Play! sample code

I have the next Scala / Play! code:

case class Tweet(from: String, text: String)

implicit val tweetReads = (
 (JsPath \ "from_user_name").read[String] ~
 (JsPath \ "text").read[String]) (Tweet.apply _)

I have a few questions regarding the syntax and meaning of the above code :

  • What class / object ~does the method refer to ?
  • What is the class / type of argument for Tweet.apply?

edit 1 : full source:

package models

import play.api.libs.json._
import play.api.libs.json.util._
import play.api.libs.json.Reads._
import play.api.libs.json.Writes._
import play.api.libs.functional.syntax._

case class Tweet(from: String, text: String)

object Tweet {

  implicit val tweetReads = (
      (JsPath \ "from_user_name").read[String] ~
      (JsPath \ "text").read[String])(Tweet.apply _)

  implicit val tweetWrites = (
    (JsPath \ "from").write[String] ~
    (JsPath \ "text").write[String])(unlift(Tweet.unapply))
}
+4
source share
2 answers

Step by step:

Method \object JsPath

val path1: JsPath = JsPath \ "from_user_name"
val path2: JsPath = JsPath \ "text"

Method readfor an object of typeJsPath

val reads1: Reads[String] = path1.read[String]
val reads2: Reads[String] = path2.read[String]

Reads ~, FunctionalBuilderOps M[T] FunctionalBuilderOps[M[_], T] play.api.libs.functional.syntax - toFunctionalBuilderOps.

val reads1FunctionalBuilderOps: FunctionalBuilderOps[Reads, String] =
  toFunctionalBuilderOps(reads1)

val canBuild2: CanBuild2[String, String] = reads1FunctionalBuilderOps.~(reads2)

Tweet.apply _ scala FunctionN N:

val func: (String, String) => Tweet = Tweet.apply _

CanBuild2[A, B] apply. (A, B) => C Reads[C] ( ):

implicit val tweetReads: Reads[Tweet] = canBuild2.apply(func)

JsPath#read, toFunctionalBuilderOps CanBuild2#apply. :

val reads1: Reads[String] = path1.read[String](Reads.StringReads)

...
val reads1FunctionalBuilderOps: FunctionalBuilderOps[Reads, String] =
  toFunctionalBuilderOps(reads1)(
    functionalCanBuildApplicative(
      Reads.applicative(JsResult.applicativeJsResult)))

...
implicit val tweetReads: Reads[Tweet] =
  canBuild2.apply(func)(functorReads)
+8

, ( ). , , scala -

, json, this. . scalaz.

+2

All Articles