本文整理汇总了Scala中scala.concurrent.Future.successful类的典型用法代码示例。如果您正苦于以下问题:Scala successful类的具体用法?Scala successful怎么用?Scala successful使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了successful类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: q
//设置package包名称以及导入依赖的类
package dao
import play.api.db.slick.HasDatabaseConfigProvider
import play.api.libs.concurrent.Execution.defaultContext
import slick.driver.H2Driver.api._
import slick.driver.JdbcProfile
import slick.lifted.{Rep, TableQuery}
import scala.concurrent.Future.successful
import scala.concurrent.{ExecutionContext, Future}
trait DefaultDAO[ID, E, Z <: Table[E]] extends BaseDAO[ID, E] with HasExecutionContext {
this: HasDatabaseConfigProvider[JdbcProfile] =>
// @formatter:off
def q: TableQuery[Z]
def filterIds(e: Seq[ID]): Query[Z, Z#TableElementType, Seq]
def getId(e: E): Option[ID]
def id(): Query[Rep[ID], ID, Seq]
// @formatter:on
def insertSeq(e: Seq[E]): Future[Seq[ID]] =
if (e.nonEmpty) db.run(((q returning id()) ++= e).transactionally) else successful(Seq.empty[ID])
def insertForce(e: Seq[E]): Future[Unit] =
if (e.nonEmpty) db.run((q ++= e).transactionally) map (_ => ()) else successful((): Unit)
def delete(): Future[Int] =
db.run(q.delete.transactionally)
def delete(ids: Seq[ID]): Future[Int] =
if (ids.nonEmpty) db.run(filterIds(ids).delete.transactionally) else successful(0)
def update(e: E): Future[Int] =
getId(e).fold(successful(0))(id => db.run(filterIds(id :: Nil).update(e).transactionally))
def seq(offset: Int, limit: Int): Future[Seq[E]] =
db.run(q.drop(offset).take(limit).result.transactionally)
def byId(e: Seq[ID]): Future[Seq[E]] =
db.run(filterIds(e).result.transactionally)
def count(): Future[Int] =
db.run(q.countDistinct.result.transactionally)
def whereOne(f: Z => Rep[Boolean]): Future[E] =
whereSeq(f) map (_.head)
def whereOpt(f: Z => Rep[Boolean]): Future[Option[E]] =
whereSeq(f) map (_.headOption)
def whereSeq(f: Z => Rep[Boolean]): Future[Seq[E]] =
db.run(q.filter(f).result.transactionally)
// play execution context by default
implicit def executionContext: ExecutionContext = defaultContext
}
示例2: CrudServiceSpec
//设置package包名称以及导入依赖的类
package io.shedin.crud.lib
import org.mockito.Mockito.{verify, when}
import org.scalatest.FlatSpec
import org.scalatest.mockito.MockitoSugar
import scala.concurrent.Await.result
import scala.concurrent.Future
import scala.concurrent.Future.successful
import scala.concurrent.duration.{Duration, SECONDS}
class CrudServiceSpec extends FlatSpec with MockitoSugar {
class CrudServiceTestImpl(crudRepository: CrudRepository[Int]) extends CrudService[Int](crudRepository)
private val crudRepository = mock[CrudRepository[Int]]
private val crudService = new CrudServiceTestImpl(crudRepository)
private val resource = 123
private val resourceId = "resourceId"
private implicit val timeout = Duration(2, SECONDS)
def await[T](future: Future[T])(implicit timeout: Duration) = result(future, timeout)
"Crud service" should "delegate creation to a repository" in {
when(crudRepository.create(resource)).thenReturn(successful(resource))
await(crudService.create(resource))
verify(crudRepository).create(resource)
}
it should "delegate reading to a repository" in {
when(crudRepository.read(resourceId)).thenReturn(successful(None))
await(crudService.read(resourceId))
verify(crudRepository).read(resourceId)
}
it should "delegate whole updates to a repository" in {
when(crudRepository.update(resourceId, resource)).thenReturn(successful(None))
await(crudService.update(resourceId, resource))
verify(crudRepository).update(resourceId, resource)
}
it should "delegate partial updates to a repository" in {
when(crudRepository.update(resourceId, "partial payload")).thenReturn(successful(None))
await(crudService.update(resourceId, "partial payload"))
verify(crudRepository).update(resourceId, "partial payload")
}
it should "delegate deletion to a repository" in {
when(crudRepository.delete(resourceId)).thenReturn(successful(true))
await(crudService.delete(resourceId))
verify(crudRepository).delete(resourceId)
}
}
示例3: AuthFilter
//设置package包名称以及导入依赖的类
package uk.co.telegraph.utils.server.filters
import java.nio.charset.StandardCharsets.UTF_8
import java.util.Base64
import javax.inject.{Inject, Singleton}
import akka.stream.Materializer
import play.api.Configuration
import play.api.mvc.Results._
import play.api.mvc.{Filter, RequestHeader, Result}
import uk.co.telegraph.utils.server.filters.AuthFilter._
import scala.concurrent.Future
import scala.concurrent.Future.successful
@Singleton
class AuthFilter @Inject()(config:Configuration)(implicit val mat:Materializer)
extends Filter
{
private val securePaths:Set[String] = config.getOptional[Seq[String]]("app.auth.paths")
.map(_.toSet)
.getOrElse(DefaultPaths)
private val authToken = {
val username:String = config.getOptional[String]("app.auth.user").getOrElse(DefaultUsername)
val password:String = config.getOptional[String]("app.auth.pwd" ).getOrElse(DefaultPassword)
"Basic " + Base64.getEncoder.encodeToString( s"$username:$password".getBytes(UTF_8) )
}
def apply(f: (RequestHeader) => Future[Result])(request: RequestHeader): Future[Result] = {
if (securePaths.exists(request.path endsWith _)) {
if( !request.headers.get("Authorization").contains(authToken) ) {
return UnauthorizedResponse
}
}
f(request)
}
}
object AuthFilter {
val DefaultUsername = "admin"
val DefaultPassword = "admin"
val DefaultPaths = Set("/admin")
val UnauthorizedResponse: Future[Result] = successful(Unauthorized.withHeaders("WWW-Authenticate" -> "Basic realm=Unauthorized"))
}