diff options
author | Li Haoyi <haoyi.li@databricks.com> | 2019-09-14 18:34:57 +0800 |
---|---|---|
committer | Li Haoyi <haoyi.li@databricks.com> | 2019-09-14 18:36:52 +0800 |
commit | 85e982a6bf9bd82524baf53546b31d85b426fa62 (patch) | |
tree | 4504cdc4f11543dcbeec3496707b95a464b70b86 /cask/src/cask/main | |
parent | 2c1dcc3cd33fbd2c2c921f20f67c45ce48c1e8bc (diff) | |
download | cask-85e982a6bf9bd82524baf53546b31d85b426fa62.tar.gz cask-85e982a6bf9bd82524baf53546b31d85b426fa62.tar.bz2 cask-85e982a6bf9bd82524baf53546b31d85b426fa62.zip |
`InnerResponse` is now a type param instead of member, allowing better error messages through `cask.internal.Conversion`:
```
Cannot return java.io.ByteArrayInputStream as a cask.model.Response[cask.endpoints.JsonData]
```
Diffstat (limited to 'cask/src/cask/main')
-rw-r--r-- | cask/src/cask/main/Decorators.scala | 10 | ||||
-rw-r--r-- | cask/src/cask/main/Main.scala | 3 | ||||
-rw-r--r-- | cask/src/cask/main/Routes.scala | 8 |
3 files changed, 10 insertions, 11 deletions
diff --git a/cask/src/cask/main/Decorators.scala b/cask/src/cask/main/Decorators.scala index 28d44f5..e395d5b 100644 --- a/cask/src/cask/main/Decorators.scala +++ b/cask/src/cask/main/Decorators.scala @@ -9,7 +9,7 @@ import cask.model.{Request, Response} * Annotates a Cask endpoint that returns a HTTP [[Response]]; similar to a * [[Decorator]] but with additional metadata and capabilities. */ -trait Endpoint extends BaseEndpoint { +trait Endpoint[InnerReturned] extends BaseEndpoint[InnerReturned] { type OuterReturned = Router.Result[Response.Raw] } @@ -17,7 +17,7 @@ trait Endpoint extends BaseEndpoint { * An [[Endpoint]] that may return something else than a HTTP response, e.g. * a websocket endpoint which may instead return a websocket event handler */ -trait BaseEndpoint extends BaseDecorator{ +trait BaseEndpoint[InnerReturned] extends BaseDecorator[InnerReturned]{ /** * What is the path that this particular endpoint matches? */ @@ -55,10 +55,9 @@ trait BaseEndpoint extends BaseDecorator{ /** * A [[Decorator]] that may deal with values other than HTTP [[Response]]s */ -trait BaseDecorator{ +trait BaseDecorator[InnerReturned]{ type Input type InputParser[T] <: ArgReader[Input, T, Request] - type InnerReturned type Delegate = Map[String, Input] => Router.Result[InnerReturned] type OuterReturned <: Router.Result[Any] def wrapFunction(ctx: Request, delegate: Delegate): OuterReturned @@ -76,10 +75,9 @@ trait BaseDecorator{ * to `wrapFunction`, which takes a `Map` representing any additional argument * lists (if any). */ -trait Decorator extends BaseDecorator{ +trait Decorator extends BaseDecorator[Response.Raw]{ type OuterReturned = Router.Result[Response.Raw] type Input = Any - type InnerReturned = Response.Raw type InputParser[T] = NoOpParser[Input, T] } diff --git a/cask/src/cask/main/Main.scala b/cask/src/cask/main/Main.scala index 7128006..c3a4ace 100644 --- a/cask/src/cask/main/Main.scala +++ b/cask/src/cask/main/Main.scala @@ -94,7 +94,8 @@ abstract class BaseMain{ case head :: rest => head.wrapFunction( ctx, - args => rec(rest, args :: bindings).asInstanceOf[Router.Result[head.InnerReturned]] + args => rec(rest, args :: bindings) + .asInstanceOf[cask.internal.Router.Result[cask.model.Response.Raw]] ) case Nil => diff --git a/cask/src/cask/main/Routes.scala b/cask/src/cask/main/Routes.scala index a4660a9..5b02542 100644 --- a/cask/src/cask/main/Routes.scala +++ b/cask/src/cask/main/Routes.scala @@ -8,7 +8,7 @@ import language.experimental.macros object Routes{ case class EndpointMetadata[T](decorators: Seq[Decorator], - endpoint: BaseEndpoint, + endpoint: BaseEndpoint[_], entryPoint: EntryPoint[T, _]) case class RoutesEndpointsMetadata[T](value: EndpointMetadata[T]*) object RoutesEndpointsMetadata{ @@ -19,15 +19,15 @@ object Routes{ val routeParts = for{ m <- c.weakTypeOf[T].members - val annotations = m.annotations.filter(_.tree.tpe <:< c.weakTypeOf[BaseDecorator]).reverse + val annotations = m.annotations.filter(_.tree.tpe <:< c.weakTypeOf[BaseDecorator[_]]).reverse if annotations.nonEmpty } yield { - if(!(annotations.head.tree.tpe <:< weakTypeOf[BaseEndpoint])) c.abort( + if(!(annotations.head.tree.tpe <:< weakTypeOf[BaseEndpoint[_]])) c.abort( annotations.head.tree.pos, s"Last annotation applied to a function must be an instance of Endpoint, " + s"not ${annotations.head.tree.tpe}" ) - val allEndpoints = annotations.filter(_.tree.tpe <:< weakTypeOf[BaseEndpoint]) + val allEndpoints = annotations.filter(_.tree.tpe <:< weakTypeOf[BaseEndpoint[_]]) if(allEndpoints.length > 1) c.abort( annotations.head.tree.pos, s"You can only apply one Endpoint annotation to a function, not " + |