当前位置: 首页>>代码示例>>Scala>>正文


Scala QueryStringBindable类代码示例

本文整理汇总了Scala中play.api.mvc.QueryStringBindable的典型用法代码示例。如果您正苦于以下问题:Scala QueryStringBindable类的具体用法?Scala QueryStringBindable怎么用?Scala QueryStringBindable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了QueryStringBindable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: MultipartPostResponses200

//设置package包名称以及导入依赖的类
package form_data


    import java.io.File
    import scala.math.BigInt

    import de.zalando.play.controllers.PlayPathBindables


//noinspection ScalaStyle
package yaml {


    case class MultipartPostResponses200(name: BothPostResponses200Name, year: BothPostYear, fileSize: BothPostYear, fileName: BothPostResponses200Name) 
    case class BothPostResponses200(name: BothPostResponses200Name, year: BothPostYear, avatarSize: BothPostYear, ringtoneSize: BothPostYear) 


    import play.api.libs.json._
    import play.api.libs.functional.syntax._
    import de.zalando.play.controllers.MissingDefaultWrites
    object ResponseWrites extends MissingDefaultWrites {
    implicit val BothPostResponses200Writes: Writes[BothPostResponses200] = new Writes[BothPostResponses200] {
        def writes(ss: BothPostResponses200) =
          Json.obj(
            "name" -> ss.name, 
            "year" -> ss.year, 
            "avatarSize" -> ss.avatarSize, 
            "ringtoneSize" -> ss.ringtoneSize
          )
        }
    implicit val MultipartPostResponses200Writes: Writes[MultipartPostResponses200] = new Writes[MultipartPostResponses200] {
        def writes(ss: MultipartPostResponses200) =
          Json.obj(
            "name" -> ss.name, 
            "year" -> ss.year, 
            "fileSize" -> ss.fileSize, 
            "fileName" -> ss.fileName
          )
        }
    }
}

// should be defined after the package because of the https://issues.scala-lang.org/browse/SI-9922

//noinspection ScalaStyle
package object yaml {

    type MultipartPostAvatar = Option[File]
    type BothPostResponses200Name = Option[String]
    type BothPostYear = Option[BigInt]


import play.api.mvc.{QueryStringBindable, PathBindable}

    implicit val bindable_FileQuery = PlayPathBindables.queryBindableFile
    implicit val bindable_BigIntQuery = PlayPathBindables.queryBindableBigInt
    implicit val bindable_OptionFileQuery: QueryStringBindable[Option[File]] = PlayPathBindables.createOptionQueryBindable[File]
    implicit val bindable_OptionBigIntQuery: QueryStringBindable[Option[BigInt]] = PlayPathBindables.createOptionQueryBindable[BigInt]

} 
开发者ID:LappleApple,项目名称:api-first-hand,代码行数:61,代码来源:form_data_yaml.scala

示例2: ErrorModel

//设置package包名称以及导入依赖的类
import de.zalando.play.controllers.ArrayWrapper

    import de.zalando.play.controllers.PlayPathBindables


//noinspection ScalaStyle
package simple_petstore_api_yaml {


    case class ErrorModel(code: Int, message: String) 
    case class Pet(id: Long, name: String, tag: NewPetTag) 
    case class NewPet(name: String, id: NewPetId, tag: NewPetTag) 


    import play.api.libs.json._
    import play.api.libs.functional.syntax._
    import de.zalando.play.controllers.MissingDefaultReads
    object BodyReads extends MissingDefaultReads {
        implicit val NewPetReads: Reads[NewPet] = (
            (JsPath \ "name").read[String] and (JsPath \ "id").readNullable[Long] and (JsPath \ "tag").readNullable[String]
        )(NewPet.apply _)
    }

    import play.api.libs.json._
    import play.api.libs.functional.syntax._
    import de.zalando.play.controllers.MissingDefaultWrites
    object ResponseWrites extends MissingDefaultWrites {
    implicit val PetWrites: Writes[Pet] = new Writes[Pet] {
        def writes(ss: Pet) =
          Json.obj(
            "id" -> ss.id, 
            "name" -> ss.name, 
            "tag" -> ss.tag
          )
        }
    }
}

// should be defined after the package because of the https://issues.scala-lang.org/browse/SI-9922

//noinspection ScalaStyle
package object simple_petstore_api_yaml {

    type PetsIdDeleteResponses204 = Null
    type NewPetTag = Option[String]
    type PetsGetLimit = Option[Int]
    type NewPetId = Option[Long]
    type PetsGetTagsOpt = ArrayWrapper[String]
    type PetsGetResponses200 = Seq[Pet]
    type PetsGetTags = Option[PetsGetTagsOpt]


import play.api.mvc.{QueryStringBindable, PathBindable}

    implicit val bindable_OptionIntQuery: QueryStringBindable[Option[Int]] = PlayPathBindables.createOptionQueryBindable[Int]
    implicit val bindable_ArrayWrapperStringQuery: QueryStringBindable[ArrayWrapper[String]] = PlayPathBindables.createArrayWrapperQueryBindable[String]("csv")
    implicit val bindable_OptionPetsGetTagsOptQuery: QueryStringBindable[Option[PetsGetTagsOpt]] = PlayPathBindables.createOptionQueryBindable[PetsGetTagsOpt]

} 
开发者ID:LappleApple,项目名称:api-first-hand,代码行数:60,代码来源:simple_petstore_api_yaml.scala

示例3: Company

//设置package包名称以及导入依赖的类
package models

import java.util.Date

import com.github.stonexx.play.mvc.binders.enum._
import com.github.stonexx.scala.data.OrderedEnumeration
import play.api.mvc.{QueryStringBindable, PathBindable}

case class Company(id: Option[Long], name: String)

case class Computer(
  id: Option[Long] = None,
  name: String,
  introduced: Option[Date] = None,
  discontinued: Option[Date] = None,
  companyId: Option[Long] = None
)

object Computer {
  object forms {
    object Sorts extends OrderedEnumeration {
      type Sorts = Value

      val Id           = Value
      val Name         = Value
      val Introduced   = Value
      val Discontinued = Value
      val Company      = Value

      implicit val pathBindable       : PathBindable[Ordered]        = enumOrderedPathBindable(this)
      implicit val queryStringBindable: QueryStringBindable[Ordered] = enumOrderedQueryStringBindable(this)
    }
  }
} 
开发者ID:stonexx,项目名称:utils,代码行数:35,代码来源:Models.scala

示例4: Task

//设置package包名称以及导入依赖的类
package models

import com.github.stonexx.play.mvc.binders.enum._
import com.github.stonexx.scala.data.OrderedEnumeration
import play.api.libs.json.{Json, OFormat}
import play.api.mvc.{PathBindable, QueryStringBindable}

case class Task(id: Option[Long], label: Option[String])

object Task {
  implicit val jsonFormat: OFormat[Task] = Json.format[Task]

  object forms {
    import play.api.data._
    import play.api.data.Forms._

    val labelForm = Form(
      "label" -> nonEmptyText(maxLength = 20)
    )

    object Sorts extends OrderedEnumeration {
      type Sorts = Value
      val Id    = Value
      val Label = Value

      implicit val pathBindable       : PathBindable[Ordered]        = enumOrderedPathBindable(this)
      implicit val queryStringBindable: QueryStringBindable[Ordered] = enumOrderedQueryStringBindable(this)
    }
  }
} 
开发者ID:stonexx,项目名称:utils,代码行数:31,代码来源:Task.scala

示例5: enumIdPathBindable

//设置package包名称以及导入依赖的类
package com.github.stonexx.play.mvc.binders

import com.github.stonexx.scala.data.OrderedEnumeration
import com.github.stonexx.scala.util.string._
import play.api.mvc.{QueryStringBindable, PathBindable}

trait EnumBinders {

  def enumIdPathBindable[E <: Enumeration](enum: E): PathBindable[E#Value] = new PathBindable.Parsing[E#Value](
    value => enum(value.toInt), _.id.toString,
    (key, e) => "Cannot parse parameter %s as %s[%s]: %s".format(key, enum, classOf[Enumeration].getSimpleName, e.getMessage)
  )

  def enumIdQueryStringBindable[E <: Enumeration](enum: E): QueryStringBindable[E#Value] = new QueryStringBindable.Parsing[E#Value](
    value => enum(value.toInt), _.id.toString,
    (key, e) => "Cannot parse parameter %s as %s[%s]: %s".format(key, enum, classOf[Enumeration].getSimpleName, e.getMessage)
  )

  def enumNamePathBindable[E <: Enumeration](enum: E): PathBindable[E#Value] = new PathBindable.Parsing[E#Value](
    value => enum.values.find(_.toString.camelToUnderscore == value.camelToUnderscore).get, _.toString,
    (key, e) => "Cannot parse parameter %s as %s[%s]: %s".format(key, enum, classOf[Enumeration].getSimpleName, e.getMessage)
  )

  def enumNameQueryStringBindable[E <: Enumeration](enum: E): QueryStringBindable[E#Value] = new QueryStringBindable.Parsing[E#Value](
    value => enum.values.find(_.toString.camelToUnderscore == value.camelToUnderscore).get, _.toString,
    (key, e) => "Cannot parse parameter %s as %s[%s]: %s".format(key, enum, classOf[Enumeration].getSimpleName, e.getMessage)
  )

  def enumOrderedPathBindable[E <: OrderedEnumeration](enum: E): PathBindable[E#Ordered] = new PathBindable.Parsing[E#Ordered](
    enum.Ordered.parse(_), _.toString,
    (key, e) => "Cannot parse parameter %s as %s[%s]: %s".format(key, enum.getClass.getName, classOf[OrderedEnumeration].getSimpleName, e.getMessage)
  )

  def enumOrderedQueryStringBindable[E <: OrderedEnumeration](enum: E): QueryStringBindable[E#Ordered] = new QueryStringBindable.Parsing[E#Ordered](
    enum.Ordered.parse(_), _.toString,
    (key, e) => "Cannot parse parameter %s as %s[%s]: %s".format(key, enum.getClass.getName, classOf[OrderedEnumeration].getSimpleName, e.getMessage)
  )

  // aliases

  @inline def enumPathBindable[E <: Enumeration](enum: E): PathBindable[E#Value] = enumNamePathBindable(enum)
  @inline def enumQueryStringBindable[E <: Enumeration](enum: E): QueryStringBindable[E#Value] = enumNameQueryStringBindable(enum)
} 
开发者ID:stonexx,项目名称:utils,代码行数:44,代码来源:EnumBinders.scala

示例6: reads

//设置package包名称以及导入依赖的类
package com.wellfactored.restless.play

import atto.Atto._
import atto.ParseResult.Done
import com.wellfactored.restless.query.QueryAST.{Path, Query}
import com.wellfactored.restless.query.{QueryAST, QueryParser, QueryPrinter}
import play.api.libs.json._
import play.api.mvc.QueryStringBindable

package object actions {
  implicit val pathR = new Reads[Path] {
    override def reads(json: JsValue): JsResult[Path] = implicitly[Reads[String]].reads(json).flatMap { js =>
      JsSuccess(Path(js), JsPath(List()))
    }
  }

  implicit val queryBinding = new QueryStringBindable[QueryAST.Query] {
    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Query]] = {
      params.get(key).map { qs =>
        QueryParser.query.parseOnly(qs.headOption.getOrElse("")) match {
          case Done(_, q) => Right(q)
          case _ => Left("failed to parse query string")
        }
      }
    }

    override def unbind(key: String, value: Query): String = QueryPrinter.print(value)
  }


  implicit val queryFormat = new Format[Query] {
    override def reads(json: JsValue): JsResult[Query] = {
      implicitly[Reads[String]].reads(json).flatMap { qs =>
        QueryParser.query.parseOnly(qs) match {
          case Done(_, q) => JsSuccess(q, JsPath())
          case _ => JsError("failed to parse query string")
        }
      }
    }

    override def writes(q: Query): JsValue = JsString(QueryPrinter.print(q))
  }
} 
开发者ID:WellFactored,项目名称:restless,代码行数:44,代码来源:package.scala

示例7: queryStringLocalDateBinder

//设置package包名称以及导入依赖的类
package controllers.support

import java.text.SimpleDateFormat
import java.util.Date

import play.api.mvc.QueryStringBindable

trait DateBinders {
  implicit def queryStringLocalDateBinder = new QueryStringBindable[Date] {

    val format = "yyyyMMdd"

    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Date]] = {
      params(key).headOption map { dateString =>
        try {
          val sdf = new SimpleDateFormat(format)
          Right(sdf.parse(dateString))
        } catch {
          case e: IllegalArgumentException => Left("Failed to parse query string as Date.")
        }
      }
    }
    override def unbind(key: String, date: Date): String = {
      import java.net.URLEncoder
      URLEncoder.encode(key, "utf-8") + "&" + URLEncoder.encode(date.toString, "utf-8")
    }
  }
}

object Binders extends DateBinders 
开发者ID:takat0-h0rikosh1,项目名称:play_boilerplate,代码行数:31,代码来源:Binders.scala

示例8: BirthDate

//设置package包名称以及导入依赖的类
package models

import org.joda.time.DateTime
import play.api.mvc.QueryStringBindable


case class BirthDate(year: Int, month: Int, day: Int) {
  val date = new DateTime(year, month, day, 0, 0)
}


object BirthDateBinder {
  implicit def queryStringBinder(implicit intBinder: QueryStringBindable[Int]) = new QueryStringBindable[BirthDate] {
    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, BirthDate]] = {
      for {
        year <- intBinder.bind(key + ".year", params)
        month <- intBinder.bind(key + ".month", params)
        day <- intBinder.bind(key + ".day", params)
      } yield {
        (year, month, day) match {
          case (Right(year), Right(month), Right(day)) => Right(BirthDate(year, month, day))
          case _ => Left(s"Unable to bind BirthDate for $key")
        }
      }
    }

    override def unbind(key: String, bdate: BirthDate): String = {
      intBinder.unbind(key + ".year", bdate.year) + "&" + intBinder.unbind(key + ".month", bdate.month) + "&" + intBinder.unbind(key + ".day", bdate.day)
    }
  }
} 
开发者ID:rafax,项目名称:play-swagger-repro,代码行数:32,代码来源:BirthDate.scala

示例9: Binders

//设置package包名称以及导入依赖的类
package controllers

import java.time.LocalDate
import java.time.format.DateTimeParseException

import models.Stats.TimePeriod
import play.api.mvc.QueryStringBindable

object Binders {

  implicit def queryStringBindable(implicit stringBinder: QueryStringBindable[String]) =
    new QueryStringBindable[LocalDate] {
      override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, LocalDate]] = {
        stringBinder.bind(key, params).map {
          _.right.flatMap { dateStr =>
            try {
              Right(LocalDate.parse(dateStr))
            } catch {
              case e: DateTimeParseException => Left("Failed to parse date")
            }
          }
        }
      }

      override def unbind(key: String, localDate: LocalDate): String =
        stringBinder.unbind(key, localDate.toString)
    }
} 
开发者ID:ovotech,项目名称:comms-audit-log,代码行数:29,代码来源:Binders.scala

示例10: queryStringBinder

//设置package包名称以及导入依赖的类
package com.talenthouse.enum

import play.api.libs.json.{ Json, JsString }
import play.api.mvc.{ JavascriptLiteral, PathBindable, QueryStringBindable }

import com.netaporter.uri.{ PathPart, QueryString }
import com.netaporter.uri.config.UriConfig

trait SealedBindableEnumeration {
  self: BaseSealedEnumeration =>

  implicit def queryStringBinder = new QueryStringBindable[Value] {
    def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Value]] = {
      for {
        values <- params.get(key)
        value <- values.headOption
      } yield {
        withName(value)
          .toRight(s"Invalid value for $key: $value")
      }
    }

    def unbind(key: String, o: Value): String = {
      QueryString(Seq((key, Some(o.value))))
        .queryToString(UriConfig.default)
        .drop(1) // Drop leading `?'
    }
  }

  implicit def pathBindable = new PathBindable[Value] {
    def unbind(key: String, value: Value): String = {
      PathPart(value.value)
        .partToString(UriConfig.default)
    }

    def bind(key: String, value: String): Either[String, Value] = {
      withName(value)
        .toRight(s"Invalid value for $key: $value")
    }
  }

  implicit def jsLiteral: JavascriptLiteral[Value] = new JavascriptLiteral[Value] {
    def to(value: Value) = Json.stringify(JsString(value.value))
  }
} 
开发者ID:talenthouse-dev,项目名称:sealed-enumerations,代码行数:46,代码来源:PlayBindable.scala

示例11: QueryStringBinders

//设置package包名称以及导入依赖的类
package util

import play.api.mvc.QueryStringBindable
import java.util.Date


object QueryStringBinders {

  implicit def bindableDate(implicit longBinder: QueryStringBindable[Long]) =
    new QueryStringBindable[Date] {

      override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Date]] =
        longBinder.bind(key, params) match {
          case Some(Right(value)) => Some(Right(new Date(value)))
          case None => None
          case _ => Some(Left("Unable to parse Date."))
        }

      def unbind(key: String, value: Date): String = longBinder.unbind(key, value.getTime)
    }

  implicit def bindableOrdering(implicit stringBinder: QueryStringBindable[String]) =
    new QueryStringBindable[Ordering.Value] {

      override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Ordering.Value]] =
        stringBinder.bind(key, params) match {
          case Some(Right("ascending")) => Some(Right(Ordering.Ascending))
          case Some(Right("descending")) => Some(Right(Ordering.Descending))
          case None => None
          case _ => Some(Left("Ordering must be either 'ascending' or 'descending'."))
        }

      def unbind(key: String, value: Ordering.Value): String =
        stringBinder.unbind(key, value match {
          case Ordering.Ascending => "ascending"
          case Ordering.Descending => "descending"
          case _ => "unspecified"
        })
    }

} 
开发者ID:epidataio,项目名称:epidata-community,代码行数:42,代码来源:QueryStringBinders.scala

示例12: Binders

//设置package包名称以及导入依赖的类
package kipsigman.play.mvc

import kipsigman.domain.entity.Status
import play.api.mvc.QueryStringBindable

object Binders {
  
  implicit def statusQueryStringBinder(implicit stringBinder: QueryStringBindable[String]) = new QueryStringBindable[Status] {
    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Status]] = {
      for {
        nameEither <- stringBinder.bind(key, params)
      } yield {
        nameEither match {
          case Right(name) => Right(Status(name))
          case _ => Left("Unable to bind Status")
        }
      }
    }
    override def unbind(key: String, value: Status): String = stringBinder.unbind(key, value.name)
  }
} 
开发者ID:kipsigman,项目名称:play-extensions,代码行数:22,代码来源:Binders.scala

示例13: QueryStringBinders

//设置package包名称以及导入依赖的类
package binders

import play.api.i18n.Lang
import play.api.mvc.QueryStringBindable


object QueryStringBinders {
  implicit object LangQueryStringBindable extends QueryStringBindable[Lang] {
    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Lang]] = {
      val code = params.get(key).flatMap(_.headOption)
      code.map { c =>
        Lang.get(c).toRight(s"$c is not a valid language")
      }
    }

    override def unbind(key: String, value: Lang): String = value.code
  }
} 
开发者ID:chibariyo,项目名称:simpleVocabularyTeacher,代码行数:19,代码来源:QueryStringBinders.scala

示例14: urlQueryStringBindable

//设置package包名称以及导入依赖的类
package binders

import java.net.URL

import play.api.Logger
import play.api.mvc.QueryStringBindable

import scala.util.Try


  // TODO: THis can probably be simplified by implementing Parsing
  implicit def urlQueryStringBindable: QueryStringBindable[URL] = new QueryStringBindable[URL] {
    private val onSuccess: (URL) => Either[String, URL] = url => Right(url)
    private val onFailure: PartialFunction[Throwable, Either[String, URL]] = {
      case t: Throwable =>
        val message = s"Could not bind Query String Parameter URL: ${t.getMessage}"
        logger.error(message, t)
        Left(message)
    }

    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, URL]] = {
      val values = params.get(key).withFilter(_ != null)
      val allResults = values.map { seq =>
        seq.map { value =>
          Try(new URL(value)).map(onSuccess).recover(onFailure).get
        }
      }
      allResults.flatMap(_.headOption)
    }

    override def unbind(key: String, value: URL): String = s"$key=${value.toString}"
  }

} 
开发者ID:tom-ogle,项目名称:affiliate-referer-server,代码行数:35,代码来源:ProjectBinders.scala

示例15: Coordinate

//设置package包名称以及导入依赖的类
package models

import play.api.mvc.QueryStringBindable


case class Coordinate(lat: Double = 49.26382D, long: Double = -123.104321D)

object Coordinate {

  implicit def queryStringBindable(implicit doubleBinder: QueryStringBindable[Double]) = new QueryStringBindable[Coordinate] {
    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Coordinate]] = {
      (doubleBinder.bind("lat", params), doubleBinder.bind("long", params)) match {

        case (Some(Right(lat)), Some(Right(long))) if lat >= -90 && lat <= 90 && long >= -180 && long <= 180 =>
          Some(Right(Coordinate(lat, long)))
        case (None, None) =>
          Some(Right(Coordinate()))
        case _ =>
          Some(Left("Unable to bind an Coordinate"))
      }
    }

    override def unbind(key: String, coord: Coordinate): String = {
      doubleBinder.unbind("lat", coord.lat) + "&" + doubleBinder.unbind("long", coord.long)
    }
  }
} 
开发者ID:lvhangrobin,项目名称:PlayIntro,代码行数:28,代码来源:Coordinate.scala


注:本文中的play.api.mvc.QueryStringBindable类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。