scala - Deciphering this activator-akka-spray method -



scala - Deciphering this activator-akka-spray method -

i have background in java learning scala utilize spray. while looking through 1 of sample programs, encountered method:

/** * marshals instances of ``either[a, b]`` appropriate http responses marshalling values * in left or right projections; , selecting appropriate http status code * values in left projection. * * @param ma marshaller left projection * @param mb marshaller right projection * @param esa selector converting left projection http status code * @tparam left projection * @tparam b right projection * @return marshaller */ implicit def errorselectingeithermarshaller[a, b](implicit ma: marshaller[a], mb: marshaller[b], esa: errorselector[a]): marshaller[either[a, b]] = marshaller[either[a, b]] { (value, ctx) => value match { case left(a) => val mc = new collectingmarshallingcontext() ma(a, mc) ctx.handleerror(errorresponseexception(esa(a), mc.entity)) case right(b) => mb(b, ctx) } }

what doesn't create sense me (value, ctx). these appear parameters, don't know how passed in. neither value or ctx defined anywhere else far can tell. how method supposed work?

the sample can found typing activator-akka-spray in typesafe activator , looking in src/main/scala/api/defaultjsonformats.scala. (the file shown here contains method, rest of file not identical) here's whole class:

package api import spray.json._ import java.util.uuid import scala.reflect.classtag import spray.httpx.marshalling.{metamarshallers, marshaller, collectingmarshallingcontext} import spray.http.statuscode import spray.httpx.sprayjsonsupport /** * contains useful json formats: ``j.u.date``, ``j.u.uuid`` , others; useful * when creating traits contain ``jsonreader`` , ``jsonwriter`` instances * types contain ``date``s, ``uuid``s , such like. */ trait defaultjsonformats extends defaultjsonprotocol sprayjsonsupport metamarshallers { /** * computes ``rootjsonformat`` type ``a`` if ``a`` object */ def jsonobjectformat[a : classtag]: rootjsonformat[a] = new rootjsonformat[a] { val ct = implicitly[classtag[a]] def write(obj: a): jsvalue = jsobject("value" -> jsstring(ct.runtimeclass.getsimplename)) def read(json: jsvalue): = ct.runtimeclass.newinstance().asinstanceof[a] } /** * instance of ``rootjsonformat`` ``j.u.uuid`` */ implicit object uuidjsonformat extends rootjsonformat[uuid] { def write(x: uuid) = jsstring(x.tostring) def read(value: jsvalue) = value match { case jsstring(x) => uuid.fromstring(x) case x => deserializationerror("expected uuid jsstring, got " + x) } } /** * type alias function converts ``a`` ``statuscode`` * @tparam type of input values */ type errorselector[a] = => statuscode /** * marshals instances of ``either[a, b]`` appropriate http responses marshalling values * in left or right projections; , selecting appropriate http status code * values in left projection. * * @param ma marshaller left projection * @param mb marshaller right projection * @param esa selector converting left projection http status code * @tparam left projection * @tparam b right projection * @return marshaller */ implicit def errorselectingeithermarshaller[a, b](implicit ma: marshaller[a], mb: marshaller[b], esa: errorselector[a]): marshaller[either[a, b]] = marshaller[either[a, b]] { (value, ctx) => value match { case left(a) => val mc = new collectingmarshallingcontext() ma(a, mc) ctx.handleerror(errorresponseexception(esa(a), mc.entity)) case right(b) => mb(b, ctx) } } }

the marshaling called implicitly spray type marshals. spray provide value marshaled request context.

scala spray

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -