Skip to content

Instantly share code, notes, and snippets.

@kailuowang
Created November 25, 2015 16:22
Show Gist options
  • Save kailuowang/98414e3b99a870ba0310 to your computer and use it in GitHub Desktop.
Save kailuowang/98414e3b99a870ba0310 to your computer and use it in GitHub Desktop.
val endpoint0 = handle(
fromJson[PartialRequestMessage].body and from(req ⇒ 'name ->> req.headers("my_name") :: HNil),
process[RequestMessage] using myActor `then` expect[ResponseMessage].respondJson(Ok(_))
)
val endPoint1 = handleParams(
process[RequestMessage] using myActor `then` expect[ResponseMessage].respond(Ok) `with` authentication
)
val endPoint2 = handle(
fromAuthorized(SessionInfo)(si ⇒ 'id ->> si.sessionId :: HNil),
process[RequestMessage] using myActor `then` expect[ResponseMessage].respondJson(Ok(_))
)
val endPoint3 = handleParams(
process[RequestMessage] using myActor `then`
expect[ProcessResult].respondJson(Ok(_)).ifEmpty(_.content).respond(NotFound)
)
val endpoint = handleParams(
`with`(caching(3.hours) and authentication) {
process[RequestMsg] using actor `then`
(expect[ResponseMsg] respondJson (Ok(_)) `with` eTag(_.updated))
}
)
@williamho
Copy link

endPoint -> endpoint

then could probably be andThen (edit: ok, apparently not)

not sure about with, though. what type is authentication/eTag?

@williamho
Copy link

on a different note, having the users construct HLists would mean they'd have to include Shapeless in their codebase (include the library, add the necessary imports, etc) independent from the play-dsl library. or maybe that's not such a big deal.

I wonder if there's a way to have the from method to have an alternate version that accepts a function Request => Tuple (of arbitrary size) and convert that to an HList. This way they could use built-in scala features to call it.

e.g.,

from(req => ('name -> req.headers("my_name"), 'somethingElse -> req.headers("abc"))

where each element in the tuple is of type (Symbol, T), which then gets converted to a record HList inside the play-dsl library. Not sure how much effort that would require (particularly, the part about from accepting a tuple of arbitrary size/types)

@kailuowang
Copy link
Author

both authentication and eTag are Filters which is
type Filter[-RMT] = (Request[RMT], ⇒ Future[Result]) ⇒ Future[Result]

The type (Symbol, T) doens't translate the value of the symbol into the type information, i.e. ('name, 1) and ('age, 2) are exactly the same type. I think the ->> constructor from shapeless manage to convert the value of symbol into a singleton type. So I am not sure if it is achievable through the tuple values you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment