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


Scala OptionT类代码示例

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


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

示例1: CompileTests

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

#+cats
import cats.Monad
import cats.data.OptionT
#-cats

#+scalaz
import scalaz.{Monad, OptionT}
#-scalaz

import aiyou.implicits._

object CompileTests {
  val mockIO: IO[Int] = IO.pure(5)
  val mockOptionT: OptionT[IO, Int] = OptionT(mockIO.map(i => Some(i): Option[Int]))

  def mtlAmbiguity[F[_]: MonadIO: MonadCatch]: Monad[F] = Monad[F]

  def monadThrowImpliesMonadCatch[F[_]: MonadCatch]: MonadThrow[F] = MonadThrow[F]

  def monadCatchImpliesMonad[F[_]: MonadCatch]: Monad[F] = Monad[F]

  // MonadCatch syntax
  mockOptionT.catchM(t => mockOptionT)
  mockOptionT.onException(mockOptionT)
  mockOptionT.ensuring(mockOptionT)

  // MonadThrow syntax
  val t = new Exception()
  t.throwM[IO, Int]
} 
开发者ID:adelbertc,项目名称:aiyou,代码行数:33,代码来源:CompileTests.scala

示例2: AuthorizedAction

//设置package包名称以及导入依赖的类
package uk.gov.bis.levyApiMock.actions

import cats.data.OptionT
import cats.instances.future._
import com.google.inject.Inject
import uk.gov.bis.levyApiMock.auth.OAuthTrace
import uk.gov.bis.levyApiMock.data.oauth2.{AuthRecord, AuthRecordOps}
import uk.gov.bis.levyApiMock.data.{GatewayUserOps, TimeSource}

import scala.concurrent.{ExecutionContext, Future}


class AuthorizedAction @Inject()(authRecords: AuthRecordOps, users: GatewayUserOps, timeSource: TimeSource)(implicit executionContext: ExecutionContext) {
  def apply(empref: String): AuthAction = new AuthorizedActionBuilder(empref, authRecords, users, timeSource)
}

class AuthorizedActionBuilder(empref: String, authRecords: AuthRecordOps, users: GatewayUserOps, timeSource: TimeSource)(implicit val ec: ExecutionContext) extends AuthAction {
  override def validateToken(accessToken: String): Future[Option[AuthRecord]] = {
    OAuthTrace(s"looking for auth record for access token `$accessToken`")

    for {
      ar <- OptionT(authRecords.find(accessToken)) if !ar.accessTokenExpired(timeSource.currentTimeMillis())
      _ = OAuthTrace(s"found auth record $ar")
      hasAccess <- OptionT.liftF(checkAccess(ar)) if hasAccess
    } yield ar
  }.value

  private def checkAccess(ar: AuthRecord): Future[Boolean] = {
    if (ar.isPrivileged) Future.successful(true)
    else users.forGatewayID(ar.gatewayID).map(_.exists(_.empref.contains(empref)))
  }
} 
开发者ID:UKGovernmentBEIS,项目名称:das-alpha-hmrc-api-mock,代码行数:33,代码来源:AuthorizedAction.scala

示例3: ErrorType

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

import cats.data.{OptionT, StateT, Xor, XorT}
import cats.instances.future._
import scala.concurrent.{ExecutionContext, Future}

sealed abstract class ErrorType
object ErrorTypes {
  case object FooError extends ErrorType
  case object BarError extends ErrorType
  case object BazError extends ErrorType
}

object StateTypes {
  type FutureXor[A] = Future[Xor[ErrorType, A]]
  type FutureXorT[A] = XorT[Future, ErrorType, A]

  type FutureOption[A] = Future[Option[A]]
  type FutureOptionT[A] = OptionT[Future, A]
}

final class StateExamples()(implicit ec: ExecutionContext) {
  import StateTypes._

  // add 2
  val addStep = StateT[FutureXorT, Int, String] { num =>
    val res = num + 2
    XorT(Future.successful(Xor.right((res, s"Result of addition step: $res"))))
  }

  // multiply by 2
  val multiplyStep = StateT[FutureXorT, Int, String] { num =>
    val res = num * 2
    XorT(Future.successful(Xor.right((res, s"Result of multiplication step: $res"))))
  }

  // modulo 2
  val moduloStep = StateT[FutureOptionT, Int, String] { num =>
    val res = num % 2
    OptionT(Future.successful(Option((res, s"Result of modulus step: $res"))))
  }

  def liftOptionT(s: StateT[FutureOptionT, Int, String]): StateT[FutureXorT, Int, String] =
    s.transformF[FutureXorT, String] { (optT: OptionT[Future, (Int, String)]) => XorT(
      optT.map { tup => Xor.right[ErrorType, (Int, String)](tup._1, tup._2) } getOrElse(Xor.left[ErrorType, (Int, String)](ErrorTypes.FooError))
    ) }
} 
开发者ID:peterjanovsky,项目名称:functional-examples,代码行数:48,代码来源:State.scala

示例4: TransformerErrorType

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

import cats.data.{EitherT, OptionT, Xor, XorT}
import cats.instances.future._
import scala.concurrent.{ExecutionContext, Future}

sealed abstract class TransformerErrorType
object TransformerErrorTypes {
  case object FooTransformerError extends TransformerErrorType
  case object BarTransformerError extends TransformerErrorType
  case object BazTransformerError extends TransformerErrorType
}


object TransformerTypes {
  type TransformerErrorXor[A] = TransformerErrorType Xor A
  type TransformerErrorXorOption[A] = OptionT[TransformerErrorXor, A]

  type FutureXor[A] = Future[Xor[TransformerErrorType, A]]
  type FutureXorT[A] = XorT[Future, TransformerErrorType, A]

  type FutureXorOption[A] = OptionT[FutureXor, A]
  type FutureXorTOption[A] = OptionT[FutureXorT, A]
} 
开发者ID:peterjanovsky,项目名称:functional-examples,代码行数:25,代码来源:Transformer.scala

示例5: WriterErrorType

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

import cats.data.{OptionT, WriterT, Xor, XorT}
import cats.instances.future._
import scala.concurrent.{ExecutionContext, Future}

sealed abstract class WriterErrorType
object WriterErrorTypes {
  case object FooWriterError extends WriterErrorType
  case object BarWriterError extends WriterErrorType
  case object BazWriterError extends WriterErrorType
}

object WriterTypes {
  type FutureXor[A] = Future[Xor[WriterErrorType, A]]
  type FutureXorT[A] = XorT[Future, WriterErrorType, A]

  type FutureOption[A] = Future[Option[A]]
  type FutureOptionT[A] = OptionT[Future, A]
}

final class WriterExamples()(implicit ec: ExecutionContext) {
  import WriterTypes._

  // add 2
  def addStep(num: Int) = {
    val res = num + 2
    WriterT[FutureXorT, String, Int](
      XorT(Future.successful(Xor.right((s"Result of addition step: $res", res))))
    )
  }

  // multiply by 2
  def multiplyStep(num: Int) = {
    val res = num * 2
    WriterT[FutureXorT, String, Int](
      XorT(Future.successful(Xor.right((s"Result of multiplication step: $res", res))))
    )
  }

  // modulo 2
  def moduloStep(num: Int) = {
    val res = num % 2
    WriterT[FutureOptionT, String, Int](
      OptionT(Future.successful(Option((s"Result of modulus step: $res", res))))
    )
  }


} 
开发者ID:peterjanovsky,项目名称:functional-examples,代码行数:51,代码来源:Writer.scala

示例6: AccountRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.Account

@Singleton
class AccountRepo @Inject()(
    dao: models.dao.AccountDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[Account]] = db.run(dao.query.result)

  def find(id: Int): OptionT[Future, Account] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(account: Account): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += account)

  def update(account: Account): Future[Boolean] =
    db.run({
      dao
        .query(account.id)
        .map(_.name)
        .update(account.name)
        .map( _ > 0)})

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:38,代码来源:AccountRepo.scala

示例7: ProvinceRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.Province

@Singleton
class ProvinceRepo @Inject()(
    dao: models.dao.ProvinceDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[Province]] = db.run(dao.query.result)

  def find(id: Int): OptionT[Future, Province] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(province: Province): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += province)

  def updateName(id: Int, newName: String): Future[Boolean] =
    db.run(dao.query(id).map(_.name).update(newName).map( _ > 0))

  def updateRegion(id: Int, idRegion: Int): Future[Boolean] =
    db.run(dao.query(id).map(_.idRegion).update(idRegion).map( _ > 0))

  def update(id: Int, newIdRegion: Option[Int], newName: Option[String])
    : Future[Unit] = db.run {
    DBIO.seq(Seq(
      newIdRegion.map(dao.query(id).map(_.idRegion).update(_)),
      newName.map(dao.query(id).map(_.name).update(_)))
    .collect({ case Some(action) => action}):_*)
  }

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:44,代码来源:ProvinceRepo.scala

示例8: MunicipalityRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.Municipality

@Singleton
class MunicipalityRepo @Inject()(
    dao: models.dao.MunicipalityDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[Municipality]] = db.run(dao.query.result)

  def find(id: Int): OptionT[Future, Municipality] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(municipality: Municipality): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += municipality)

  def updateName(id: Int, newName: String): Future[Boolean] =
    db.run(dao.query(id).map(_.name).update(newName).map( _ > 0))

  def updateAreaCode(id: Int, areacode: Int): Future[Boolean] =
    db.run(dao.query(id).map(_.areacode).update(areacode).map( _ > 0))

  def updateProvince(id: Int, idProvince: Int): Future[Boolean] =
    db.run(dao.query(id).map(_.idProvince).update(idProvince).map( _ > 0))

  def update(id: Int, newName: Option[String], newAreaCode: Option[Int], newIdProvince: Option[Int])
    : Future[Unit] = db.run {
    DBIO.seq(Seq(
      newName.map(dao.query(id).map(_.name).update(_)),
      newAreaCode.map(dao.query(id).map(_.areacode).update(_)),
      newIdProvince.map(dao.query(id).map(_.idProvince).update(_)))
    .collect({ case Some(action) => action}):_*)
  }

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)

} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:49,代码来源:MunicipalityRepo.scala

示例9: BusinessTypeRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.BusinessType

@Singleton
class BusinessTypeRepo @Inject()(
    dao: models.dao.BusinessTypeDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[BusinessType]] = db.run(dao.query.result)

  def find(id: Int): OptionT[Future, BusinessType] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(businesstype: BusinessType): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += businesstype)

  def update(businesstype: BusinessType): Future[Boolean] =
    db.run(dao.query(businesstype.id).map(_.name).update(businesstype.name).map( _ > 0))

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:33,代码来源: BusinessTypeRepo.scala

示例10: CountryRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.Country

@Singleton
class CountryRepo @Inject()(
    dao: models.dao.CountryDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[Country]] = db.run(dao.query.result)

  def find(id: Int): OptionT[Future, Country] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(country: Country): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += country)

  def update(country: Country): Future[Boolean] =
    db.run(dao.query(country.id).map(_.name).update(country.name).map( _ > 0))

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:33,代码来源:CountryRepo.scala

示例11: FirmTypeRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.FirmType

@Singleton
class FirmTypeRepo @Inject()(
    dao: models.dao.FirmTypeDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[FirmType]] = db.run(dao.query.result)

  def find(id: Int): OptionT[Future, FirmType] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(firmType: FirmType): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += firmType)

  def update(firmType: FirmType): Future[Boolean] =
    db.run(dao.query(firmType.id).update(firmType).map( _ > 0))

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:33,代码来源:FirmTypeRepo.scala

示例12: AccountClassificationRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.AccountClassification

@Singleton
class AccountClassificationRepo @Inject()(
    dao: models.dao.AccountClassificationDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[AccountClassification]] = db.run(dao.query.result)

  def getByType(isMain: Boolean): Future[Seq[AccountClassification]] =
    db.run(dao.query.filter(_.isMain === isMain).result)

  def find(id: Int): OptionT[Future, AccountClassification] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(accountClassification: AccountClassification): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += accountClassification)

  def update(accountClassification: AccountClassification): Future[Boolean] =
    db.run({
      dao
        .query(accountClassification.id)
        .map(_.name)
        .update(accountClassification.name)
        .map( _ > 0)})

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:41,代码来源:AccountClassificationRepo.scala

示例13: FirmRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.Firm

@Singleton
class FirmRepo @Inject()(
    dao: models.dao.FirmDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[Firm]] = db.run(dao.query.result)

  def getByType(idFirmType: Int): Future[Seq[Firm]] =
    db.run(dao.query.filter(_.idFirmType === idFirmType).result)

  def getByParent(idParent: Int): Future[Seq[Firm]] =
    db.run(dao.query.filter(_.idParent === idParent).result)

  def find(id: Int): OptionT[Future, Firm] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(firm: Firm): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += firm)

  def update(firm: Firm): Future[Boolean] =
    db.run(dao.query(firm.id).update(firm).map( _ > 0))

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:39,代码来源:FirmRepo.scala

示例14: PostingMethodRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT

@Singleton
class PostingMethodRepo @Inject()(
    dao: models.dao.PostingMethodDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(code: String): Future[Boolean] = db.run(dao.query(code).exists.result)

  def get: Future[Seq[String]] = db.run(dao.query.result)

  def find(code: String): OptionT[Future, String] =
    OptionT(db.run(dao.query(code).result.headOption))

  def add(postingMethod: String): Future[Int] =
    db.run((dao.query += postingMethod))

  def delete(code: String): Future[Int] =
    db.run(dao.query(code).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:29,代码来源:PostingMethodRepo.scala

示例15: ContactInfoRepo

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

import javax.inject._
import scala.concurrent.{ ExecutionContext, Future }
import play.api.db.slick.{ DatabaseConfigProvider, HasDatabaseConfigProvider }
import cats.data.OptionT
import models.domain.ContactInfo

@Singleton
class ContactInfoRepo @Inject()(
    dao: models.dao.ContactInfoDAO,
    implicit val ec: ExecutionContext,
    protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[utils.db.PostgresDriver] {
  import driver.api._

  def exists(id: Int): Future[Boolean] = db.run(dao.query(id).exists.result)

  def get: Future[Seq[ContactInfo]] = db.run(dao.query.result)

  def getByFirm(idFirm: Int): Future[Seq[ContactInfo]] =
    db.run(dao.query.filter(_.idFirm === idFirm).result)

  def find(id: Int): OptionT[Future, ContactInfo] =
    OptionT(db.run(dao.query(id).result.headOption))

  def add(contactinfo: ContactInfo): Future[Int] =
    db.run((dao.query returning dao.query.map(_.id)) += contactinfo)

  def update(contactinfo: ContactInfo): Future[Boolean] =
    db.run(dao.query(contactinfo.id).update(contactinfo).map( _ > 0))

  def delete(id: Int): Future[Int] =
    db.run(dao.query(id).delete)
} 
开发者ID:jessica1421,项目名称:HD-Cignal,代码行数:36,代码来源:ContactInfoRepo.scala


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