-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fdccbe
commit 3101909
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
3 changes: 3 additions & 0 deletions
3
modules/interop/cats/js/src/main/scala/coursier/interop/PlatformCatsImplicits.scala
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
package coursier.interop | |||
|
|||
abstract class PlatformCatsImplicits |
42 changes: 42 additions & 0 deletions
42
modules/interop/cats/jvm/src/main/scala/coursier/interop/PlatformCatsImplicits.scala
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
package coursier.interop | |||
|
|||
import java.util.concurrent.ExecutorService | |||
|
|||
import _root_.cats.effect.IO | |||
import _root_.cats.instances.vector._ | |||
import _root_.cats.syntax.apply._ | |||
import coursier.util.Schedulable | |||
|
|||
import scala.concurrent.{ExecutionContext, ExecutionContextExecutorService} | |||
|
|||
abstract class PlatformCatsImplicits { | |||
|
|||
// wish cs wasn't needed here (parSequence in gather needs it) | |||
implicit def catsIOSchedulable(implicit cs: _root_.cats.effect.ContextShift[IO]): Schedulable[IO] = | |||
new Schedulable[IO] { | |||
def point[A](a: A) = | |||
IO.pure(a) | |||
def delay[A](a: => A) = | |||
IO(a) | |||
def handle[A](a: IO[A])(f: PartialFunction[Throwable, A]) = | |||
a.handleErrorWith { e => | |||
f.lift(e).fold[IO[A]](IO.raiseError(e))(IO.pure) | |||
} | |||
def schedule[A](pool: ExecutorService)(f: => A) = { | |||
|
|||
val ec0 = pool match { | |||
case eces: ExecutionContextExecutorService => eces | |||
case _ => ExecutionContext.fromExecutorService(pool) // FIXME Is this instantiation costly? Cache it? | |||
} | |||
|
|||
IO.shift(ec0) *> IO(f) | |||
} | |||
|
|||
def gather[A](elems: Seq[IO[A]]) = | |||
_root_.cats.Parallel.parSequence(elems.toVector).map(_.toSeq) | |||
|
|||
def bind[A, B](elem: IO[A])(f: A => IO[B]) = | |||
elem.flatMap(f) | |||
} | |||
|
|||
} |
55 changes: 55 additions & 0 deletions
55
modules/interop/cats/jvm/src/test/scala/coursier/interop/CatsTests.scala
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,55 @@ | |||
package coursier.interop | |||
|
|||
import _root_.cats.effect.IO | |||
import coursier.{Module, moduleNameString, organizationString} | |||
import coursier.interop.cats._ | |||
import coursier.test.compatibility.executionContext | |||
import coursier.test.{TestRunner, compatibility} | |||
import coursier.test.util.ToFuture | |||
import utest._ | |||
|
|||
import scala.concurrent.{ExecutionContext, Future} | |||
|
|||
object CatsTests extends TestSuite { | |||
|
|||
private implicit val cs = _root_.cats.effect.IO.contextShift(executionContext) | |||
|
|||
// few basic tests from CentralTests, to ensure everything is wired correctly with cats.effect.IO | |||
|
|||
private implicit val ioToFuture: ToFuture[IO] = | |||
new ToFuture[IO] { | |||
def toFuture[T](ec: ExecutionContext, f: IO[T]) = | |||
Future(()) | |||
.flatMap(_ => f.unsafeToFuture()) | |||
} | |||
|
|||
private lazy val runner = new TestRunner( | |||
artifact = compatibility.artifact[IO] | |||
) | |||
|
|||
val tests = Tests { | |||
|
|||
'spark - { | |||
* - runner.resolutionCheck( | |||
Module(org"org.apache.spark", name"spark-core_2.11"), | |||
"1.3.1", | |||
profiles = Some(Set("hadoop-2.2")) | |||
) | |||
|
|||
'scala210 - runner.resolutionCheck( | |||
Module(org"org.apache.spark", name"spark-core_2.10"), | |||
"2.1.1", | |||
profiles = Some(Set("hadoop-2.6", "scala-2.10", "!scala-2.11")) | |||
) | |||
} | |||
|
|||
'argonautShapeless - { | |||
runner.resolutionCheck( | |||
Module(org"com.github.alexarchambault", name"argonaut-shapeless_6.1_2.11"), | |||
"0.2.0" | |||
) | |||
} | |||
|
|||
} | |||
|
|||
} |
27 changes: 27 additions & 0 deletions
27
modules/interop/cats/shared/src/main/scala/coursier/interop/cats.scala
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
package coursier.interop | |||
|
|||
import _root_.cats.instances.vector._ | |||
import coursier.util.{Gather, Monad} | |||
|
|||
object cats extends LowPriorityCatsImplicits { | |||
|
|||
implicit def coursierMonadFromCats[F[_]](implicit M: _root_.cats.Monad[F]): Monad[F] = | |||
new Monad[F] { | |||
def point[A](a: A) = M.pure(a) | |||
def bind[A, B](elem: F[A])(f: A => F[B]) = M.flatMap(elem)(f) | |||
} | |||
|
|||
} | |||
|
|||
abstract class LowPriorityCatsImplicits extends PlatformCatsImplicits { | |||
|
|||
implicit def coursierGatherFromCats[F[_], F0[_]](implicit N: _root_.cats.Monad[F], cs: _root_.cats.Parallel[F, F0]): Gather[F] = | |||
new Gather[F] { | |||
def point[A](a: A) = N.pure(a) | |||
def bind[A, B](elem: F[A])(f: A => F[B]) = N.flatMap(elem)(f) | |||
def gather[A](elems: Seq[F[A]]) = { | |||
N.map(_root_.cats.Parallel.parSequence(elems.toVector))(_.toSeq) | |||
} | |||
} | |||
|
|||
} |
This file contains 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