This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cats.data._ | |
| type FutureOption[A] = OptionT[Future, A] | |
| type FutureOptionEither[A] = EitherT[FutureOption, Throwable, A] | |
| def computeApi = { | |
| val futureEitherOption = for { | |
| someValue <- FutureOptionEither(fetchAp2(1)) | |
| someOtherValue <- FutureOptionEither(fetchApi3(someValue)) | |
| } yield () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def computeSequentially = for { | |
| api1 <- Future { ... } | |
| api2 <- Future { ... } | |
| api3 <- Future { ... } | |
| } yield( /* do something */ ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def computeSequentially = { | |
| val api1Future = Future {...} | |
| val api2Future = Future {...} | |
| val api3Future = Future {...} | |
| api1Future.flatMap {api1 => | |
| api2Future.flatMap { api2 => | |
| api3Future.map{api3 => | |
| //do something here | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fetchAPi2(someValue: Int): Future[Option[Int]] | |
| def fetchApi3(someOtherValue: Int): Either[Throwable, Result] | |
| def computeApi = { | |
| fetchApi2(1).flatMap{someValueMaybe => | |
| someValueMaybe.map{ | |
| fetchApi3(someOtherValue) match { | |
| case Left(throwable) => ??? | |
| case Right(result) => ??? | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getAPI[K,V](key:K): Future[V] = get(key).getOrElse{ | |
| for { | |
| bool <- Future(compareAndSet(key, null, "in-progress")) | |
| value <- fetchAPIA(key) | |
| bool1 <- Future(compareAndSet(key, "in-progress", value)) | |
| } yield (value) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getAPI[K,V](key:K): Future[V] = get(key).getOrElse{ | |
| fetchAPIA(key).flatMap{value => | |
| Future(compareAndSet(key, null, value)).map{bool => | |
| getApi(key) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // third party API | |
| def fetchAPIA[K,V](key:K): Future[V] | |
| def getAPI[K, V](key: K) : Future[V] = ??? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Cache[K, V] { | |
| def get(key: K): Option[V] | |
| def compareAndSet(key:Key, prevValue: V, currValue: V): Boolean | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Invoice(invoiceId: String, orderCode:String, metadata:Metadata, status: Status, isReceived: Received) | |
| val invoiceSchema = Schema[Invoice].recordOf{field => | |
| (field("invoiceId", _.invoiceId), | |
| field("orderCode", _.orderCode), | |
| field("metadata", _.metadata)(metadataSchema), | |
| field("status", _.status)(statusSchema), | |
| field(isReceived, _.isReceived)(receivedSchema) | |
| ).map{(invoiceId:String, orderCode:String, metadata:Metadata, status: Status, isReceived:Received) => | |
| Invoice(invoiceId = invoiceId, orderCode = orderCode, metadata = metadata, status = status, isReceived = isReceived) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val updateSchema = Schema.oneOf[Update]{alt => | |
| val updateRefundSchema = Schema[UdpateRefund].record{field => | |
| (field.opt("additionalData", _.additionalData), | |
| field.opt(status, _.status)).mapN{(additionalData: Option[String], status: Option[String]) => UpdateRefund(additionalData = additionalData, status = status) | |
| } | |
| }.tag("updateStatus") | |
| val updateNewPurchaseSchema = Schema[UpdateNewPurchase].record{field => | |
| (field.opt("additionalData", _.additionalData) | |
| field.opt("amount", _.amount) |