Skip to content

Commit

Permalink
Added support for Float value types. Not thoroughly tested...yet
Browse files Browse the repository at this point in the history
  • Loading branch information
isaka committed Mar 7, 2012
1 parent 6b87c78 commit 27f9522
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions framework/src/anorm/src/main/scala/Anorm.scala
Expand Up @@ -131,6 +131,16 @@ package anorm {
}
}

implicit def rowToFloat: Column[Float] = Column.nonNull { (value, meta) =>
val MetaDataItem(qualified, nullable, clazz) = meta
value match {
case int: Int => Right(int: Float)
case long: Long => Right(long: Float)
case float: Float => Right(float)
case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to Float for column " + qualified))
}
}

implicit def rowToBigInteger: Column[java.math.BigInteger] = Column.nonNull { (value, meta) =>
import java.math.BigInteger
val MetaDataItem(qualified, nullable, clazz) = meta
Expand Down Expand Up @@ -259,6 +269,7 @@ package anorm {

private def getType(t: String) = t match {
case "long" => Class.forName("java.lang.Long")
case "float" => Class.forName("java.lang.Float")
case "int" => Class.forName("java.lang.Integer")
case "boolean" => Class.forName("java.lang.Boolean")
case _ => Class.forName(t)
Expand Down
12 changes: 12 additions & 0 deletions framework/src/play/src/main/scala/play/api/data/Forms.scala
Expand Up @@ -271,6 +271,18 @@ object Forms {
*/
val longNumber: Mapping[Long] = of[Long]


/**
* Constructs a simple mapping for a numeric field (using a Float type behind).
*
* For example:
* {{{
* Form("size" -> floatNumber)
* }}}
*/
val floatNumber: Mapping[Float] = of[Float]


/**
* Constructs a simple mapping for a numeric field.
*
Expand Down
Expand Up @@ -57,6 +57,26 @@ object Formats {
def unbind(key: String, value: String) = Map(key -> value)
}

/**
* Default formatter for the `Float` type.
*/
implicit def floatFormat: Formatter[Float] = new Formatter[Float] {

override val format = Some("format.numeric", Nil)

def bind(key: String, data: Map[String, String]) = {
stringFormat.bind(key, data).right.flatMap { s =>
scala.util.control.Exception.allCatch[Float]
.either(java.lang.Float.parseFloat(s))
.left.map(e => Seq(FormError(key, "error.number", Nil)))
}
}

def unbind(key: String, value: Float) = Map(key -> value.toString)
}



/**
* Default formatter for the `Long` type.
*/
Expand Down

0 comments on commit 27f9522

Please sign in to comment.