本文整理汇总了Scala中play.api.data.format.Formatter类的典型用法代码示例。如果您正苦于以下问题:Scala Formatter类的具体用法?Scala Formatter怎么用?Scala Formatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Formatter类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: enumFormat
//设置package包名称以及导入依赖的类
package com.github.stonexx.play.data.formats
import com.github.stonexx.scala.data.OrderedEnumeration
import com.github.stonexx.scala.util.string._
import play.api.data.FormError
import play.api.data.format.Formatter
trait EnumFormats {
def enumFormat[E <: Enumeration](
parse: String => E#Value,
serialize: E#Value => String,
error: (String, Throwable) => Seq[FormError]
): Formatter[E#Value] = new Formatter[E#Value] {
def bind(key: String, data: Map[String, String]): Either[Seq[FormError], E#Value] =
play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s =>
scala.util.control.Exception.allCatch[E#Value].either(parse(s)).left.map(error(key, _))
}
def unbind(key: String, value: E#Value) = Map(key -> serialize(value))
}
def enumIdFormat[E <: Enumeration](enum: E): Formatter[E#Value] = enumFormat(
s => enum(s.toInt), _.id.toString,
(key, _) => Seq(FormError(key, "error.enum.id", Nil))
)
def enumNameFormat[E <: Enumeration](enum: E): Formatter[E#Value] = enumFormat(
s => enum.values.find(_.toString.camelToUnderscore == s.camelToUnderscore).get, _.toString,
(key, _) => Seq(FormError(key, "error.enum.name", Nil))
)
def enumOrderedFormat[E <: OrderedEnumeration](enum: E): Formatter[E#Ordered] = new Formatter[E#Ordered] {
def bind(key: String, data: Map[String, String]): Either[Seq[FormError], E#Ordered] =
play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s =>
scala.util.control.Exception.allCatch[E#Ordered].either(enum.Ordered.parse(s))
.left.map(_ => Seq(FormError(key, "error.sort", Seq(s))))
}
def unbind(key: String, value: E#Ordered) = Map(key -> value.toString)
}
}
示例2: ValidationUtils
//设置package包名称以及导入依赖的类
package utils
import java.time.LocalDate
import models.DateComponents
import org.apache.commons.lang3.StringUtils
import play.api.data.format.Formatter
import play.api.data.validation.{Constraint, Invalid, Valid, ValidationError}
import play.api.data.{FieldMapping, FormError, Forms}
import scala.util.{Failure, Success}
object ValidationUtils {
implicit val mandatoryBooleanFormatter = new Formatter[Boolean] {
def bind(key: String, data: Map[String, String]) = {
Right(data.getOrElse(key, "")).right.flatMap {
case "true" => Right(true)
case "false" => Right(false)
case _ => Left(Seq(FormError(key, s"$key.error.boolean", Nil)))
}
}
def unbind(key: String, value: Boolean) = Map(key -> value.toString)
}
val mandatoryBoolean: FieldMapping[Boolean] = Forms.of[Boolean]
val notBlank: (String) => Boolean = StringUtils.isNotBlank
def unconstrained[T] = Constraint[T] { (t: T) => Valid }
def inRange[T](minValue: T, maxValue: T, errorCode: String = "")(implicit ordering: scala.math.Ordering[T]): Constraint[T] =
Constraint[T] { (t: T) =>
assert(ordering.compare(minValue, maxValue) < 0, "min bound must be less than max bound")
(ordering.compare(t, minValue).signum, ordering.compare(t, maxValue).signum) match {
case (1, -1) | (0, _) | (_, 0) => Valid
case (_, 1) => Invalid(ValidationError(s"error$errorCode.range.above", maxValue))
case (-1, _) => Invalid(ValidationError(s"error$errorCode.range.below", minValue))
}
}
def validDate(constraint: Constraint[LocalDate] = unconstrained) = Constraint[DateComponents] {
(dcs: DateComponents) =>
DateComponents.toLocalDate(dcs) match {
case Failure(_) => Invalid(ValidationError("error.date.invalid", dcs))
case Success(localDate) => constraint(localDate)
}
}
def optionallyMatchingPattern(regex: String): Constraint[String] =
Constraint[String] { s: String =>
Option(s) match {
case None | Some("") => Valid
case _ if s.matches(regex) => Valid
case _ => Invalid(ValidationError("error.string.pattern", s))
}
}
}
示例3: Form
//设置package包名称以及导入依赖的类
package lila.common
import org.joda.time.DateTimeZone
import play.api.data.format.Formats._
import play.api.data.format.Formatter
import play.api.data.Forms._
object Form {
def options(it: Iterable[Int], pattern: String) = it map { d =>
d -> (pluralize(pattern, d) format d)
}
def options(it: Iterable[Int], transformer: Int => Int, pattern: String) = it map { d =>
d -> (pluralize(pattern, transformer(d)) format transformer(d))
}
def options(it: Iterable[Int], code: String, pattern: String) = it map { d =>
(d + code) -> (pluralize(pattern, d) format d)
}
def optionsDouble(it: Iterable[Double], format: Double => String) = it map { d =>
d -> format(d)
}
def numberIn(choices: Iterable[(Int, String)]) =
number.verifying(hasKey(choices, _))
def numberInDouble(choices: Iterable[(Double, String)]) =
of[Double].verifying(hasKey(choices, _))
def stringIn(choices: Iterable[(String, String)]) =
text.verifying(hasKey(choices, _))
def hasKey[A](choices: Iterable[(A, _)], key: A) =
choices.map(_._1).toList contains key
private def pluralize(pattern: String, nb: Int) =
pattern.replace("{s}", (nb != 1).fold("s", ""))
object formatter {
def stringFormatter[A](from: A => String, to: String => A): Formatter[A] = new Formatter[A] {
def bind(key: String, data: Map[String, String]) = stringFormat.bind(key, data).right map to
def unbind(key: String, value: A) = stringFormat.unbind(key, from(value))
}
def intFormatter[A](from: A => Int, to: Int => A): Formatter[A] = new Formatter[A] {
def bind(key: String, data: Map[String, String]) = intFormat.bind(key, data).right map to
def unbind(key: String, value: A) = intFormat.unbind(key, from(value))
}
}
object UTCDate {
val dateTimePattern = "yyyy-MM-dd HH:mm"
val utcDate = jodaDate(dateTimePattern, DateTimeZone.UTC)
implicit val dateTimeFormat = jodaDateTimeFormat(dateTimePattern)
}
}