• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala OptionT类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: RegionRepo

//设置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.Region

@Singleton
class RegionRepo @Inject()(
    dao: models.dao.RegionDAO,
    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[Region]] = db.run(dao.query.result)

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

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

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

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

  def update(id: Int, newIdCountry: Option[Int], newName: Option[String])
    : Future[Unit] = db.run {
    DBIO.seq(Seq(
      newIdCountry.map(dao.query(id).map(_.idCountry).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,代码来源:RegionRepo.scala


示例17: ProprietorRepo

//设置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.Proprietor

@Singleton
class ProprietorRepo @Inject()(
    dao: models.dao.ProprietorDAO,
    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[Proprietor]] = db.run(dao.query.result)

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

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

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

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

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


示例18: MixedController

//设置package包名称以及导入依赖的类
package lunatech.controllers
import cats.data.OptionT
import com.twitter.util.Future
import lunatech.models.Person
import lunatech.utils.TwitterFutureHelper._

object MixedController extends DataSource {
  override def getPerson(name: String): Future[Option[Person]] = ???

  override def getPersons(name: String): Future[Option[Seq[Person]]] = {
    val sqlPersons: Future[Option[Seq[Person]]]      = SqlController getPersons name
    val inMemoryPersons: Future[Option[Seq[Person]]] = InMemoryController getPersons name
    for {
      sqlPerson      <- OptionT(sqlPersons)
      inMemoryPerson <- OptionT(inMemoryPersons)
    } yield sqlPerson.toList ::: inMemoryPerson.toList
  }.value

  override def createPerson(person: Person): Future[Option[Person]] = ???
} 
开发者ID:iPlessmann,项目名称:FinchTutorial,代码行数:21,代码来源:MixedController.scala


示例19: MTUsagePattern

//设置package包名称以及导入依赖的类
package io.hnfmr.chapter5

import cats.data.Writer

object MTUsagePattern extends App {

  type Logged[A] = Writer[List[String], A]

  def parseNumber(str: String): Logged[Option[Int]] =
    util.Try(str.toInt).toOption match {
      case Some(num) => Writer(List(s"Read $str"), Some(num))
      case None => Writer(List(s"Failed on $str"), None)
    }

  def addNumbers(
                  a: String
                , b: String
                , c: String
                ): Logged[Option[Int]] = {
    import cats.data.OptionT
    import cats.instances.list._

    val result = for {
      a <- OptionT(parseNumber(a))
      b <- OptionT(parseNumber(b))
      c <- OptionT(parseNumber(c))
    } yield a + b + c

    result.value
  }


  val result1 = addNumbers("1", "2", "3")
  val result2 = addNumbers("1", "a", "3")

  println(result1)
  println(result2)
} 
开发者ID:hnfmr,项目名称:advanced-scala,代码行数:39,代码来源:MTUsagePattern.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala sqrt类代码示例发布时间:2022-05-23
下一篇:
Scala Toast类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap