Skip to content

Commit

Permalink
Merge branch 'scalatra-host' of github.com:scalatra/scalatra into sca…
Browse files Browse the repository at this point in the history
…latra-host
  • Loading branch information
casualjim committed Dec 21, 2012
2 parents 73ccad2 + 9129f5c commit ebccb4e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion auth/src/main/scala/org/scalatra/auth/Scentry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ScentryAuthStore.ScentryAuthStore
import collection.mutable
import grizzled.slf4j.Logger
import org.scalatra.util.RicherString._
import util.{MapWithIndifferentAccess, MultiMapHeadView}

object Scentry {

Expand Down Expand Up @@ -53,7 +54,7 @@ class Scentry[UserType <: AnyRef](
}

//def session = app.session
def params = app.params
def params: Params = app.params
def redirect(uri: String) { app.redirect(uri) }

def register(strategy: => ScentryStrategy[UserType]) {
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/scala/org/scalatra/ScalatraBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ object ScalatraBase {
*/
trait ScalatraSyntax extends CoreDsl with RequestResponseScope with Initializable with ServletApiImplicits with ScalatraParamsImplicits with DefaultImplicitConversions {
import ScalatraBase.{HostNameKey, PortKey, ForceHttpsKey}


/**
* The routes registered in this kernel.
*/
Expand Down Expand Up @@ -325,15 +327,15 @@ trait ScalatraSyntax extends CoreDsl with RequestResponseScope with Initializabl
* Assumes that there is never a null or empty value in multiParams. The servlet container won't put them
* in request.getParameters, and we shouldn't either.
*/
protected val _params: MultiMapHeadView[String, String] with MapWithIndifferentAccess[String] = new MultiMapHeadView[String, String] with MapWithIndifferentAccess[String] {
protected val _params: Params = new MultiMapHeadView[String, String] with MapWithIndifferentAccess[String] {
protected def multiMap = multiParams
}

/**
* A view of `multiParams`. Returns the head element for any known param,
* and is undefined for any unknown param. Invalid outside `handle`.
*/
def params = _params
def params: Params = _params

/**
* Pluggable way to convert a path expression to a route matcher.
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/org/scalatra/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org

import scalatra.util.{MapWithIndifferentAccess, MultiMapHeadView}

package object scalatra
extends Control // make halt and pass visible to helpers outside the DSL
// with DefaultValues // make defaults visible
Expand All @@ -10,6 +12,8 @@ package object scalatra

type MultiParams = MultiMap

type Params = MultiMapHeadView[String, String] with MapWithIndifferentAccess[String]

type Action = () => Any

type ErrorHandler = PartialFunction[Throwable, Any]
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/scala/org/scalatra/params.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ trait ScalatraParamsImplicits {

self: DefaultImplicitConversions =>

type ParamsType = MultiMapHeadView[String, String] with MapWithIndifferentAccess[String]
type MultiParamsType = MultiMap

sealed class TypedParams(params: ParamsType) {
sealed class TypedParams(params: Params) {

def getAs[T <: Any](name: String)(implicit tc: TypeConverter[String, T]): Option[T] = params.get(name).flatMap(tc.apply(_))

def getAs[T <: Date](nameAndFormat: (String, String)): Option[Date] = getAs(nameAndFormat._1)(stringToDate(nameAndFormat._2))

}

sealed class TypedMultiParams(multiParams: MultiParamsType) {
sealed class TypedMultiParams(multiParams: MultiParams) {

def getAs[T <: Any](name: String)(implicit tc: TypeConverter[String, T]): Option[Seq[T]] = multiParams.get(name) map {
s =>
Expand All @@ -32,9 +29,9 @@ trait ScalatraParamsImplicits {
def getAs[T <: Date](nameAndFormat: (String, String)): Option[Seq[Date]] = getAs(nameAndFormat._1)(stringToDate(nameAndFormat._2))
}

implicit def toTypedParams(params: ParamsType) = new TypedParams(params)
implicit def toTypedParams(params: Params) = new TypedParams(params)

implicit def toTypedMultiParams(params: MultiParamsType) = new TypedMultiParams(params)
implicit def toTypedMultiParams(params: MultiParams) = new TypedMultiParams(params)
}

object ScalatraParamsImplicits extends ScalatraParamsImplicits with DefaultImplicitConversions
14 changes: 6 additions & 8 deletions core/src/test/scala/org/scalatra/ParamsExtensionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ class ParamsExtensionSpec extends Specification {

import ScalatraParamsImplicits._

case class FakeParams(params: Map[String, String]) extends MultiMapHeadView[String, String]

with MapWithIndifferentAccess[String] {
case class FakeParams(params: Map[String, String]) extends MultiMapHeadView[String, String] with MapWithIndifferentAccess[String] {
protected def multiMap = MultiMap(params.map(e => (e._1, List(e._2).toSeq)))
}

"Scalatra 'Params pimping'" should {

"add a getAs[T] method to Scalatra Params that returns Option[T]" in {

val params: ParamsType = FakeParams(Map("a" -> "1", "b" -> "", "c" -> null))
val params: Params = FakeParams(Map("a" -> "1", "b" -> "", "c" -> null))

params.getAs[Int]("a") must beSome(1)

Expand All @@ -34,7 +32,7 @@ class ParamsExtensionSpec extends Specification {

val (format, dateAsText) = ("dd/MM/yyyy", "9/11/2001")

val params: ParamsType = FakeParams(Map("TwinTowers" -> dateAsText))
val params: Params = FakeParams(Map("TwinTowers" -> dateAsText))

val expectedDate = new SimpleDateFormat(format).parse(dateAsText)

Expand All @@ -43,7 +41,7 @@ class ParamsExtensionSpec extends Specification {
}

"return None if a conversion is invalid" in {
val params: ParamsType = FakeParams(Map("a" -> "hello world"))
val params: Params = FakeParams(Map("a" -> "hello world"))
params.getAs[Int]("a") must beNone
}

Expand All @@ -53,7 +51,7 @@ class ParamsExtensionSpec extends Specification {

implicit val bogusConverter: TypeConverter[String, Bogus] = (s: String) => Some(Bogus(s))

val params: ParamsType = FakeParams(Map("a" -> "buffybuffy"))
val params: Params = FakeParams(Map("a" -> "buffybuffy"))

params.getAs[Bogus]("a") must beSome

Expand All @@ -63,7 +61,7 @@ class ParamsExtensionSpec extends Specification {

"explicitely receive a custom TypeConverter" in {

val params: ParamsType = FakeParams(Map("a" -> "buffybuffy"))
val params: Params = FakeParams(Map("a" -> "buffybuffy"))

params.getAs[Bogus]("a")((s: String) => Some(Bogus(s.toUpperCase))) must beSome(Bogus("BUFFYBUFFY"))

Expand Down
8 changes: 5 additions & 3 deletions sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Author: Paul Phillips <paulp@typesafe.com>

# todo - make this dynamic
declare -r sbt_release_version=0.12.1
declare -r sbt_release_version=0.12.2-RC1
declare -r sbt_snapshot_version=0.13.0-SNAPSHOT

unset sbt_jar sbt_dir sbt_create sbt_snapshot sbt_launch_dir
Expand Down Expand Up @@ -114,7 +114,7 @@ declare -r sbt_opts_file=".sbtopts"
declare -r jvm_opts_file=".jvmopts"
declare -r latest_28="2.8.2"
declare -r latest_29="2.9.2"
declare -r latest_210="2.10.0-RC5"
declare -r latest_210="2.10.0"

declare -r script_path=$(get_script_path "$BASH_SOURCE")
declare -r script_dir="$(dirname $script_path)"
Expand Down Expand Up @@ -272,6 +272,7 @@ Usage: $script_name [options]
-210 use $latest_210
-scala-home <path> use the scala build at the specified directory
-scala-version <version> use the specified version of scala
-binary-version <version> use the specified scala version when searching for dependencies
# java version (default: java from PATH, currently $(java -version |& grep version))
-java-home <path> alternate JAVA_HOME
Expand Down Expand Up @@ -355,7 +356,8 @@ process_args ()
-sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
-sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;;
-sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;;
-scala-version) require_arg version "$1" "$2" && addSbt "set scalaVersion := \"$2\"" && addSbt "set every scalaBinaryVersion := \"$2\"" && shift 2 ;;
-scala-version) require_arg version "$1" "$2" && addSbt "set scalaVersion in ThisBuild := \"$2\"" && shift 2 ;;
-binary-version) require_arg version "$1" "$2" && addSbt "set scalaBinaryVersion in ThisBuild := \"$2\"" && shift 2 ;;
-scala-home) require_arg path "$1" "$2" && addSbt "set every scalaHome := Some(file(\"$2\"))" && shift 2 ;;
-java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;;

Expand Down

0 comments on commit ebccb4e

Please sign in to comment.