Skip to content

Commit

Permalink
Fix finagle#62 (const readers)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkostyukov committed Jul 31, 2014
1 parent b9e1331 commit 3f38305
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ Request Reader Monad

A `FutureRequestReader` has return type `Future[A]` so it might be simply used as an additional monad-transformation in a top-level for-comprehension statement. This is dramatically useful when a service should fetch some params from a request before doing a real job (and not doing it at all if some of the params are not found/not valid).

There are three common implementations of a `FutureRequestReader`:
There are four common implementations of a `FutureRequestReader`:
* `io.finch.request.ConstFutureParam` - fetches a const param
* `io.finch.request.RequiredParam` - fetches required params within specified type
* `io.finch.request.OptionalParam` - fetches optional params
* `io.finch.request.ValidationRule` - fails if given predicate is false
Expand Down Expand Up @@ -232,7 +233,6 @@ val service = new Service[HttpRequest, JsonResponse] {
* `Some[A]` if param is presented in the request and may be converted to a requested type `IntParam`, `LongParam` or `BooleanParam`.
* `None` otherwise.


#### A `io.finch.request.ValidationRule(param, rule)(predicate)`
* returns `Future.Done` when predicate is `true`
* throws `ValidationFailed` exception with `rule` and `param` fields
Expand All @@ -243,6 +243,9 @@ There is also a couple of empty request readers that raises `NoSuchElementExcept
* `io.finch.request.NoFutureParams` implementing `FutureRequestReader[Nothing]`
* `io.finch.request.NoParams` implementing `RequestReader[Nothing]`

#### Const readers
Readers that read const params are also available: `io.finch.request.ConstFutureParam` and `io.finch.request.ConstParam`.

### Multiple-Value Params
All the readers have companion readers that can read multiple-value params `List[A]` instead of single-value params `A`. Multiple-value readers have `s` postfix in their names. So, `Param` has `Params`, `OptionalParam` has `OptipnalParams` and finally `RequiredParam` has `RequiredParams` companions. There are also typed versions for every reader, like `IntParams` or even `OptionalLongParams`.

Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Finch extends Build {

lazy val buildSettings = Seq(
organization := "io",
version := "0.1.3",
version := "0.1.4",
scalaVersion := "2.10.3"
)

Expand Down
32 changes: 32 additions & 0 deletions src/main/scala/io/finch/request/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,38 @@ package object request {
def apply(req: HttpRequest) = throw new NoSuchElementException("Empty reader.")
}

/**
* A const param.
*/
object ConstFutureParam {

/**
* Creates a ''FutureRequestReader'' that reads given ''const'' param from
* the request.
*
* @return a const param value
*/
def apply[A](const: A) = new FutureRequestReader[A] {
def apply(req: HttpRequest) = const.toFuture
}
}

/**
* A const param.
*/
object ConstParam {

/**
* Creates a ''RequestReader'' that reads given ''const'' param from
* the request.
*
* @return a const param value
*/
def apply[A](const: A) = new RequestReader[A] {
def apply(req: HttpRequest) = const
}
}

private[this] object StringToNumberOrFail {
def apply[A](param: String, rule: String)(number: => A) = new FutureRequestReader[A] {
def apply(req: HttpRequest) =
Expand Down

0 comments on commit 3f38305

Please sign in to comment.