Skip to content

Commit

Permalink
use Some instead of Option for unapply if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Nov 29, 2020
1 parent 9761e6d commit 35219b6
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion filter/src/main/scala/request/paths.scala
Expand Up @@ -4,7 +4,7 @@ import unfiltered.request.HttpRequest
import javax.servlet.http.HttpServletRequest

object ContextPath {
def unapply[T <: HttpServletRequest](req: HttpRequest[T]) =
def unapply[T <: HttpServletRequest](req: HttpRequest[T]): Some[(String, String)] =
req.underlying.getContextPath() match {
case ctx => Some((ctx, req.uri.substring(ctx.length).split('?')(0)))
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/scala/request/headers.scala
Expand Up @@ -31,7 +31,7 @@ object DateFormatting {

/** A header with values mapped to keys in a Map. */
private [request] class MappedRequestHeader[A, B](val name: String)(parser: Iterator[String] => Map[A, B]) extends RequestExtractor[Map[A, B]] {
def unapply[T](req: HttpRequest[T]) = Some(parser(req.headers(name)))
def unapply[T](req: HttpRequest[T]): Some[Map[A, B]] = Some(parser(req.headers(name)))
def apply[T](req: HttpRequest[T]) = parser(req.headers(name))
}

Expand Down
4 changes: 2 additions & 2 deletions library/src/main/scala/request/params.scala
Expand Up @@ -12,7 +12,7 @@ object Params {
* The Map is assigned a default value of Nil, so param("p") would return Nil if there
* is no such parameter, or (as normal for servlets) a single empty string if the
* parameter was supplied without a value. */
def unapply[T](req: HttpRequest[T]) = {
def unapply[T](req: HttpRequest[T]): Some[Map] = {
val names = req.parameterNames
Some(names.foldLeft(Map.empty[String, Seq[String]]) ((m, n) =>
m + (n -> req.parameterValues(n))
Expand Down Expand Up @@ -69,7 +69,7 @@ object QueryParams {
* The Map is assigned a default value of Nil, so param("p") would return Nil if there
* is no such parameter, or (as normal for servlets) a single empty string if the
* parameter was supplied without a value. */
def unapply[T](req: HttpRequest[T]) = Some(urldecode(req.uri))
def unapply[T](req: HttpRequest[T]): Some[Map[String, Seq[String]]] = Some(urldecode(req.uri))

def urldecode(enc: String) : Map[String, Seq[String]] = {
def decode(raw: String) = URLDecoder.decode(raw, "UTF-8")
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/scala/request/paths.scala
@@ -1,7 +1,7 @@
package unfiltered.request

object Path {
def unapply[T](req: HttpRequest[T]) = Some(req.uri.split('?')(0))
def unapply[T](req: HttpRequest[T]): Some[String] = Some(req.uri.split('?')(0))
def apply[T](req: HttpRequest[T]) = req.uri.split('?')(0)
}

Expand All @@ -13,7 +13,7 @@ object QueryString {
}

object Seg {
def unapply(path: String): Option[List[String]] = path.split("/").toList match {
def unapply(path: String): Some[List[String]] = path.split("/").toList match {
case "" :: rest => Some(rest) // skip a leading slash
case all => Some(all)
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/scala/request/remotes.scala
Expand Up @@ -9,7 +9,7 @@ object RemoteAddr {
* private IP 192.168.x.x
*/
val TrustedProxies = """(^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|3[0-1])|192\.168)\.\S+)""".r
def unapply[T](req: HttpRequest[T]) = Some(req match {
def unapply[T](req: HttpRequest[T]): Some[String] = Some(req match {
case XForwardedFor(forwarded) =>
forwarded.filter(!TrustedProxies.findFirstMatchIn(_).isDefined) match {
case addr :: _ => addr
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/scala/request/utils.scala
Expand Up @@ -4,9 +4,9 @@ import scala.language.reflectiveCalls

object & {
/** Used to bind extractors to a single request object */
def unapply[A](a: A) = Some((a, a))
def unapply[A](a: A): Some[(A, A)] = Some((a, a))
/** Used to extract nested tuples produced by directives anded together */
def unapply[A,B](tup: Tuple2[A,B]) = Some(tup)
def unapply[A,B](tup: Tuple2[A,B]): Some[(A, B)] = Some(tup)
}

abstract class RequestExtractor[E] {
Expand Down

0 comments on commit 35219b6

Please sign in to comment.