Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Build for Scala 2.13
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrs committed Dec 20, 2019
1 parent 0ab5aff commit 33c7d46
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 56 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sudo: true

scala:
- 2.12.10
- 2.13.1

jdk:
- openjdk11
Expand All @@ -25,13 +26,13 @@ install:

script:
- |
if [[ "$TRAVIS_JDK_VERSION" = "openjdk11" && "$TRAVIS_SCALA_VERSION" = 2.12.* ]]; then
if [[ "$TRAVIS_JDK_VERSION" = "openjdk11" && "$TRAVIS_SCALA_VERSION" = 2.13.* ]]; then
sbt ++$TRAVIS_SCALA_VERSION clean coverage compile test:compile test coverageReport coverageOff && codecov
else
sbt ++$TRAVIS_SCALA_VERSION clean compile test:compile test
fi
- |
if [[ "$TRAVIS_JDK_VERSION" = "openjdk8" && $(cat version.sbt) =~ "-SNAPSHOT" ]]; then
if [[ "$TRAVIS_JDK_VERSION" = "openjdk11" && $(cat version.sbt) =~ "-SNAPSHOT" ]]; then
export PGP_SECRET_RING_PATH="$(mktemp -d)/secring.asc"
echo "${PGP_SECRET}" | base64 --decode > "${PGP_SECRET_RING_PATH}"
if [ "$TRAVIS_BRANCH" = "master" ]; then
Expand Down
34 changes: 26 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ lazy val allSettings = Seq.concat(

lazy val buildSettings = Seq(
organization := "com.github.yanana",
scalaVersion := "2.12.10",
crossScalaVersions := Seq("2.12.10"),
scalaVersion := "2.13.1",
crossScalaVersions := Seq("2.12.10", "2.13.1"),
libraryDependencies += compilerPlugin(("org.typelevel" % "kind-projector" % "0.11.0").cross(CrossVersion.full))
)

Expand All @@ -49,7 +49,12 @@ lazy val testDeps = Seq(
) map (_ % "test")

lazy val baseSettings = Seq(
scalacOptions ++= compilerOptions,
scalacOptions ++= compilerOptions ++ {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n >= 13 => Nil
case _ => Seq("-Xfuture","-Ypartial-unification", "-Yno-adapted-args")
}
},
scalacOptions in (Compile, console) := compilerOptions,
scalacOptions in (Compile, test) := compilerOptions,
libraryDependencies ++= (coreDeps ++ testDeps).map(_.withSources),
Expand All @@ -58,7 +63,7 @@ lazy val baseSettings = Seq(
Resolver.sonatypeRepo("snapshots")
),
fork in Test := true,
scalacOptions in (Compile, console) ~= (_ filterNot (_ == "-Ywarn-unused-import"))
scalacOptions in (Compile, console) ~= (_ filterNot (_ == "-Ywarn-unused:_"))
)

lazy val publishSettings = Seq(
Expand Down Expand Up @@ -103,8 +108,24 @@ lazy val noPublishSettings = Seq(
publishArtifact := false
)

lazy val crossVersionSharedSources: Seq[Setting[_]] =
Seq(Compile, Test).map { sc =>
(sc / unmanagedSourceDirectories) ++= {
(sc / unmanagedSourceDirectories).value.flatMap { dir =>
if (dir.getName != "scala") Seq(dir)
else
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n >= 13 => Seq(file(dir.getPath + "_2.13+"))
case _ => Seq(file(dir.getPath + "_2.12-"))
}
}
}
}


lazy val core = project.in(file("core"))
.settings(allSettings)
.settings(crossVersionSharedSources)
.settings(
sourceGenerators in Compile += (sourceManaged in Compile).map(Boilerplate.gen).taskValue
)
Expand Down Expand Up @@ -199,15 +220,12 @@ lazy val compilerOptions = Seq(
"-encoding", "UTF-8",
"-unchecked",
"-feature",
"-Ypartial-unification",
"-language:existentials",
"-language:higherKinds",
"-language:implicitConversions",
"-language:postfixOps",
"-Yno-adapted-args",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Ywarn-unused-import",
"-Xfuture",
"-Ywarn-unused:_",
"-Xlint"
)
31 changes: 2 additions & 29 deletions core/src/main/scala/agni/Cql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package agni
import java.util.concurrent.CompletionStage

import agni.util.Par
import agni.internal.Pageable
import cats.{ Apply, MonadError }
import cats.instances.stream._
import cats.syntax.functor._
import cats.syntax.traverse._
import cats.syntax.applicative._
import cats.syntax.flatMap._
import cats.syntax.option._
import com.datastax.oss.driver.api.core.CqlSession
import com.datastax.oss.driver.api.core.cql._

import scala.collection.JavaConverters._

object Cql {
object Cql extends Pageable {

def prepareAsync[F[_]](session: CqlSession, stmt: String)(
implicit
Expand Down Expand Up @@ -51,28 +48,4 @@ object Cql {
case _ =>
none.pure[F]
}

def getRows[F[_]: MonadError[*[*], Throwable]](session: CqlSession, stmt: BoundStatement)(
implicit
par: Par.Aux[CompletionStage, F]
): F[Stream[Row]] = {
def go(result: F[AsyncResultSet]): F[Stream[Row]] =
result >>= { i =>
val page = i.currentPage().asScala.toStream
if (!i.hasMorePages) page.pure[F]
else go(par.parallel(i.fetchNextPage())).map(page ++ _)
}

go(executeAsync(session, stmt))
}

def getRowsAs[F[_], A](session: CqlSession, stmt: BoundStatement)(
implicit
F: MonadError[F, Throwable],
decodeA: RowDecoder[A],
parF: Par.Aux[CompletionStage, F]
): F[Stream[A]] = {
val ver = session.getContext().getProtocolVersion()
getRows[F](session, stmt) >>= (_.traverse(row => F.fromEither(decodeA(row, ver))))
}
}
10 changes: 5 additions & 5 deletions core/src/main/scala/agni/Deserializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.nio.ByteBuffer
import java.time.{ Instant, LocalDate }
import java.util.UUID

import agni.internal.ScalaVersionSpecifics._
import cats.instances.either._
import cats.syntax.apply._
import cats.syntax.either._
Expand All @@ -13,7 +14,6 @@ import com.datastax.oss.driver.api.core.`type`.codec.TypeCodecs
import com.datastax.oss.driver.api.core.data.CqlDuration

import scala.annotation.tailrec
import scala.collection.generic.CanBuildFrom

trait Deserializer[A] {
self =>
Expand Down Expand Up @@ -144,10 +144,10 @@ object Deserializer {
implicit
K: Deserializer[K],
V: Deserializer[V],
cbf: CanBuildFrom[Nothing, (K, V), M[K, V]]
factory: Factory[(K, V), M[K, V]]
): Deserializer[M[K, V]] = new Deserializer[M[K, V]] {
override def apply(raw: ByteBuffer, version: ProtocolVersion): Either[Throwable, M[K, V]] = {
val builder = cbf.apply
val builder = factory.newBuilder
if (raw == null || !raw.hasRemaining)
builder.result().asRight[Throwable]
else {
Expand Down Expand Up @@ -176,10 +176,10 @@ object Deserializer {
}
}

implicit def deserializeCollection[A, C[_]](implicit A: Deserializer[A], cbf: CanBuildFrom[Nothing, A, C[A]]): Deserializer[C[A]] =
implicit def deserializeCollection[A, C[_]](implicit A: Deserializer[A], factory: Factory[A, C[A]]): Deserializer[C[A]] =
new Deserializer[C[A]] {
override def apply(raw: ByteBuffer, version: ProtocolVersion): Either[Throwable, C[A]] = {
val builder = cbf.apply()
val builder = factory.newBuilder
if (raw == null || !raw.hasRemaining)
builder.result().asRight
else {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/agni/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ object Serializer {

}

val bbs = mutable.ArrayBuilder.make[ByteBuffer]()
val bbs = mutable.ArrayBuilder.make[ByteBuffer]
go(value.toList, bbs, 4).flatMap {
case (bbs, toAllocate) => Either.catchNonFatal {
val result = ByteBuffer.allocate(toAllocate)
Expand All @@ -172,7 +172,7 @@ object Serializer {
new Serializer[C[A0]] {
override def apply(value: C[A0], version: ProtocolVersion): Either[Throwable, ByteBuffer] = {
if (value == null) Left(new NullPointerException) else {
val items = mutable.ArrayBuilder.make[ByteBuffer]()
val items = mutable.ArrayBuilder.make[ByteBuffer]
val it = is.conversion(value).toIterator

@tailrec def go(toAllocate: Int): Either[Throwable, (Array[ByteBuffer], Int)] = {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/agni/generic/DerivedRowDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object DerivedRowDecoder extends DerivedRowDecoder1
trait DerivedRowDecoder1 {

implicit val decodeHNil: DerivedRowDecoder[HNil] =
(_, version) => Right(HNil)
(_, _) => Right(HNil)

implicit def decodeLabelledHList[K <: Symbol, H, T <: HList](
implicit
Expand Down
43 changes: 43 additions & 0 deletions core/src/main/scala_2.12-/agni/internal/Pageable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package agni.internal

import java.util.concurrent.CompletionStage

import agni.{ Cql, RowDecoder }
import agni.util.Par
import cats.MonadError
import cats.instances.stream._
import cats.syntax.functor._
import cats.syntax.traverse._
import cats.syntax.applicative._
import cats.syntax.flatMap._
import com.datastax.oss.driver.api.core.CqlSession
import com.datastax.oss.driver.api.core.cql._

import scala.collection.JavaConverters._

trait Pageable { self: Cql.type =>

def getRows[F[_]: MonadError[*[*], Throwable]](session: CqlSession, stmt: BoundStatement)(
implicit
par: Par.Aux[CompletionStage, F]
): F[Stream[Row]] = {
def go(result: F[AsyncResultSet]): F[Stream[Row]] =
result >>= { i =>
val page = i.currentPage().asScala.toStream
if (!i.hasMorePages) page.pure[F]
else go(par.parallel(i.fetchNextPage())).map(page ++ _)
}

go(executeAsync(session, stmt))
}

def getRowsAs[F[_], A](session: CqlSession, stmt: BoundStatement)(
implicit
F: MonadError[F, Throwable],
decodeA: RowDecoder[A],
parF: Par.Aux[CompletionStage, F]
): F[Stream[A]] = {
val ver = session.getContext().getProtocolVersion()
getRows[F](session, stmt) >>= (_.traverse(row => F.fromEither(decodeA(row, ver))))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package agni.internal

import scala.collection.generic.CanBuildFrom
import scala.collection.mutable

private[agni] object ScalaVersionSpecifics {
private[agni] type Factory[-E, +T] = CanBuildFrom[Nothing, E, T]

implicit private[agni] class FactoryOps[F, E, T](val bf: Factory[E, T]) extends AnyVal {
def newBuilder: mutable.Builder[E, T] = bf.apply()
}
}
43 changes: 43 additions & 0 deletions core/src/main/scala_2.13+/agni/internal/Pageable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package agni.internal

import java.util.concurrent.CompletionStage

import agni.{ Cql, RowDecoder }
import agni.util.Par
import cats.MonadError
import cats.instances.lazyList._
import cats.syntax.functor._
import cats.syntax.traverse._
import cats.syntax.applicative._
import cats.syntax.flatMap._
import com.datastax.oss.driver.api.core.CqlSession
import com.datastax.oss.driver.api.core.cql._

import scala.jdk.CollectionConverters._

trait Pageable { self: Cql.type =>

def getRows[F[_]: MonadError[*[*], Throwable]](session: CqlSession, stmt: BoundStatement)(
implicit
par: Par.Aux[CompletionStage, F]
): F[LazyList[Row]] = {
def go(result: F[AsyncResultSet]): F[LazyList[Row]] =
result >>= { i =>
val page = LazyList.concat(i.currentPage().asScala)
if (!i.hasMorePages) page.pure[F]
else go(par.parallel(i.fetchNextPage())).map(page ++ _)
}

go(executeAsync(session, stmt))
}

def getRowsAs[F[_], A](session: CqlSession, stmt: BoundStatement)(
implicit
F: MonadError[F, Throwable],
decodeA: RowDecoder[A],
parF: Par.Aux[CompletionStage, F]
): F[LazyList[A]] = {
val ver = session.getContext().getProtocolVersion()
getRows[F](session, stmt) >>= (_.traverse(row => F.fromEither(decodeA(row, ver))))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package agni.internal

private[agni] object ScalaVersionSpecifics {
private[agni] type Factory[-E, +T] = scala.collection.Factory[E, T]
}
8 changes: 4 additions & 4 deletions examples/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import org.scalatest.Matchers
object Main extends IOApp with Matchers {
import Query._

def run(args: List[String]): IO[ExitCode] =
def run(args: List[String]) =
IO(connect()).bracket(action)(c => IO(c.close())).flatTap(xs => IO(xs.foreach(println))) >>=
(xs => IO(xs.sortBy(_.id) === users).ifM(IO(ExitCode.Success), IO(ExitCode.Error)))

def connect(): CqlSession =
CqlSession.builder().build()

def remake(session: CqlSession): IO[Unit] = for {
def remake(session: CqlSession) = for {
_ <- Cql.prepareAsync[IO](session, createKeyspaceQuery) >>= (p => Cql.executeAsync[IO](session, p.bind()))
_ <- Cql.prepareAsync[IO](session, dropTableQuery) >>= (p => Cql.executeAsync[IO](session, p.bind()))
_ <- Cql.prepareAsync[IO](session, createTableQuery) >>= (p => Cql.executeAsync[IO](session, p.bind()))
} yield ()

def action(session: CqlSession): IO[Stream[Author]] = for {
def action(session: CqlSession) = for {
_ <- remake(session)

_ <- Cql.prepareAsync[IO](session, insertUserQuery).flatTap(a => IO(println(a.getQuery))) >>=
Expand All @@ -38,7 +38,7 @@ object Main extends IOApp with Matchers {
(p => Cql.getRowsAs[IO, Author](session, p.bind()))
} yield xs

def insertUser(session: CqlSession, p: PreparedStatement, a: Author): IO[Unit] =
def insertUser(session: CqlSession, p: PreparedStatement, a: Author) =
IO.fromEither(Binder[Author].apply(p.bind(), session.getContext.getProtocolVersion, a)).flatTap(a => IO(println(a.getUuid(0)))) >>=
(b => Cql.executeAsync[IO](session, b) >>
IO(println("inserted users")))
Expand Down
9 changes: 4 additions & 5 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.3")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.8.1")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.1")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.9")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.9.3")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.12")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.7")

0 comments on commit 33c7d46

Please sign in to comment.