This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Foo restful interface. | |
| */ | |
| trait FooRoute extends HttpService with AskSupport { | |
| import examples.FooProtocol._ | |
| import examples.FooProtocolV2._ | |
| implicit val timeout: Timeout | |
| implicit val fooActorPath: ActorPath |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.json4s.{DefaultFormats, Formats} | |
| object Json4sProtocol extends Json4sSupport { | |
| implicit def json4sFormats: Formats = DefaultFormats | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * FooProtocol v2 | |
| */ | |
| object FooProtocolV2 { | |
| sealed trait FooActorMessage | |
| case class HelloFoo(hi: String, there:String) extends FooActorMessage | |
| case class FooResponse(helloBackTo: String) extends FooActorMessage | |
| import examples.Json4sProtocol._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * FooProtocol v1 | |
| */ | |
| object FooProtocol { | |
| sealed trait FooActorMessage | |
| case class HelloFoo(hi: String) extends FooActorMessage | |
| case class FooResponse(helloBackTo: String) extends FooActorMessage | |
| import Json4sProtocol._ | |
| import org.json4s.native.Serialization.{read,write => swrite} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Foo Version Media Types for v1 and v2 | |
| */ | |
| object VersionMediaTypes{ | |
| lazy val `application/vnd.ww.v2.foo+json` = | |
| MediaTypes.register(MediaType.custom("application/vnd.ww.v2.foo+json")) | |
| lazy val `application/vnd.ww.v1.foo+json` = | |
| MediaTypes.register(MediaType.custom("application/vnd.ww.v1.foo+json")) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| GET /user/1 HTTP/1.1 | |
| Accept: application/vnd.company.app.user-v2+json | |
| HTTP/1.1 200 OK | |
| Content-Type: application/vnd.company.app.user-v2+json | |
| {"user": | |
| {"email":"bob@gmail.com"} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object UseScalaz { | |
| def main(args: Array[String]): Unit = { | |
| import scalaz._ | |
| case class Better(total: List[Int]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sum(i:Int) = StateChange[Better,Int](s => Better.sum(i)(s)) | |
| // Lets run the sum computation, and add all the results together | |
| def composeAddResultS(one:Int,two:Int,three:Int): StateChange[Better,Int] = | |
| sum(one).doAgainWithNewState( | |
| firstResult => | |
| sum(two).doAgainWithNewState( secondResult => | |
| sum(three).mapResult( thirdResult => firstResult + secondResult + thirdResult) )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // we notice S => (S,A) is useful, almost composable and testable, but awkward... lets do better | |
| // Lets create a type that wraps the computation, and add some useful combinators: mapResult, doAgainWithResultAndNewState | |
| case class StateChange[S,A](run: S => (S,A)){ | |
| def mapResult[B](f:A => B):StateChange[S,B] = StateChange{ (s:S) => | |
| val(s2,a) = run(s) | |
| (s2,f(a)) | |
| } | |
| /* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // we desire this pattern Better => (Better,Int) | |
| type BetterUpdate = Better => (Better,Int) | |
| val firstF:BetterUpdate = Better.sum(1) | |
| val secondF:BetterUpdate = Better.sum(2) | |
| val thirdF:BetterUpdate = Better.sum(3) | |
| // the external state is threaded through a sequence of computations |