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

Commit

Permalink
Merge pull request #13 from tkrs/reformat
Browse files Browse the repository at this point in the history
Reformat with align=most
  • Loading branch information
tkrs authored Dec 21, 2019
2 parents 66973c5 + 453aa4a commit 1cbeb3c
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 100 deletions.
3 changes: 1 addition & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
version=2.3.0
align.openParenCallSite = true
align.openParenDefnSite = true
align = most
maxColumn = 120
continuationIndent.defnSite = 2
assumeStandardLibraryStripMargin = true
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/scala/agni/Deserializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ object Deserializer {
// TODO: deserializer varchar

private def read(input: ByteBuffer): ByteBuffer = {
val size = input.getInt()
val size = input.getInt()
val encoded = input.slice()
encoded.limit(size)
input.position(input.position() + size)
Expand All @@ -157,7 +157,7 @@ object Deserializer {
@tailrec def go(size: Int): Either[Throwable, M[K, V]] =
if (size == 0) builder.result().asRight
else {
val encodedKey = read(input)
val encodedKey = read(input)
val encodedValue = read(input)

(K(encodedKey, version), V(encodedValue, version)).mapN {
Expand All @@ -177,8 +177,10 @@ object Deserializer {
}
}

implicit def deserializeCollection[A, C[_]](implicit A: Deserializer[A],
factory: Factory[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 = factory.newBuilder
Expand Down
60 changes: 36 additions & 24 deletions core/src/main/scala/agni/RowSerializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,58 @@ object RowSerializer {
def apply[A](implicit A: RowSerializer[A]): RowSerializer[A] = A

implicit def builtIn[A](implicit A: Serializer[A]): RowSerializer[A] = new RowSerializer[A] {
override def apply(bound: BoundStatement,
i: Int,
a: A,
version: ProtocolVersion): Either[Throwable, BoundStatement] =
override def apply(
bound: BoundStatement,
i: Int,
a: A,
version: ProtocolVersion
): Either[Throwable, BoundStatement] =
A.apply(a, version) >>=
(bs => Either.catchNonFatal(bound.setBytesUnsafe(i, bs)))

override def apply(bound: BoundStatement,
name: String,
a: A,
version: ProtocolVersion): Either[Throwable, BoundStatement] =
override def apply(
bound: BoundStatement,
name: String,
a: A,
version: ProtocolVersion
): Either[Throwable, BoundStatement] =
A.apply(a, version) >>=
(bs => Either.catchNonFatal(bound.setBytesUnsafe(name, bs)))
}

implicit val tupleValue: RowSerializer[TupleValue] = new RowSerializer[TupleValue] {
override def apply(bound: BoundStatement,
i: Int,
a: TupleValue,
version: ProtocolVersion): Either[Throwable, BoundStatement] =
override def apply(
bound: BoundStatement,
i: Int,
a: TupleValue,
version: ProtocolVersion
): Either[Throwable, BoundStatement] =
Either.catchNonFatal(bound.setTupleValue(i, a))

override def apply(bound: BoundStatement,
name: String,
a: TupleValue,
version: ProtocolVersion): Either[Throwable, BoundStatement] =
override def apply(
bound: BoundStatement,
name: String,
a: TupleValue,
version: ProtocolVersion
): Either[Throwable, BoundStatement] =
Either.catchNonFatal(bound.setTupleValue(name, a))
}

implicit val udtValue: RowSerializer[UdtValue] = new RowSerializer[UdtValue] {
override def apply(bound: BoundStatement,
i: Int,
a: UdtValue,
version: ProtocolVersion): Either[Throwable, BoundStatement] =
override def apply(
bound: BoundStatement,
i: Int,
a: UdtValue,
version: ProtocolVersion
): Either[Throwable, BoundStatement] =
Either.catchNonFatal(bound.setUdtValue(i, a))

override def apply(bound: BoundStatement,
name: String,
a: UdtValue,
version: ProtocolVersion): Either[Throwable, BoundStatement] =
override def apply(
bound: BoundStatement,
name: String,
a: UdtValue,
version: ProtocolVersion
): Either[Throwable, BoundStatement] =
Either.catchNonFatal(bound.setUdtValue(name, a))
}
}
10 changes: 6 additions & 4 deletions core/src/main/scala/agni/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ object Serializer {
V: Serializer[V]
): Serializer[M[K, V]] = new Serializer[M[K, V]] {
override def apply(value: M[K, V], version: ProtocolVersion): Either[Throwable, ByteBuffer] = {
@tailrec def go(m: List[(K, V)],
acc: mutable.ArrayBuilder[ByteBuffer],
toAllocate: Int): Either[Throwable, (Array[ByteBuffer], Int)] = m match {
@tailrec def go(
m: List[(K, V)],
acc: mutable.ArrayBuilder[ByteBuffer],
toAllocate: Int
): Either[Throwable, (Array[ByteBuffer], Int)] = m match {
case Nil => (acc.result(), toAllocate).asRight
case (k, v) :: t =>
val kv = (K(k, version), V(v, version)).mapN((_, _))
Expand Down Expand Up @@ -177,7 +179,7 @@ object Serializer {
if (value == null) Left(new NullPointerException)
else {
val items = mutable.ArrayBuilder.make[ByteBuffer]
val it = is(value).iterator
val it = is(value).iterator

@tailrec def go(toAllocate: Int): Either[Throwable, (Array[ByteBuffer], Int)] =
if (!it.hasNext) (items.result(), toAllocate).asRight
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/agni/generic/auto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import agni.{Binder, RowDecoder}
import shapeless.Lazy

object auto {
implicit def autoDerivedBinder[A](implicit A: Lazy[DerivedBinder[A]]): Binder[A] = A.value
implicit def autoDerivedBinder[A](implicit A: Lazy[DerivedBinder[A]]): Binder[A] = A.value
implicit def autoDerivedRowDecoder[A](implicit A: Lazy[DerivedRowDecoder[A]]): RowDecoder[A] = A.value
}
2 changes: 1 addition & 1 deletion core/src/main/scala/agni/generic/semiauto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import agni.{Binder, RowDecoder}
import shapeless.Lazy

object semiauto {
def derivedBinder[A](implicit A: Lazy[DerivedBinder[A]]): Binder[A] = A.value
def derivedBinder[A](implicit A: Lazy[DerivedBinder[A]]): Binder[A] = A.value
def derivedRowDecoder[A](implicit A: Lazy[DerivedRowDecoder[A]]): RowDecoder[A] = A.value
}
2 changes: 1 addition & 1 deletion core/src/test/scala/agni/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.scalatestplus.scalacheck.Checkers
class CodecSpec extends FunSuite with Checkers with Matchers {

implicit val arbString: Arbitrary[String] = Arbitrary(Gen.alphaStr)
implicit val arbUUID: Arbitrary[UUID] = Arbitrary(Gen.uuid)
implicit val arbUUID: Arbitrary[UUID] = Arbitrary(Gen.uuid)
implicit val arbByteBuffer: Arbitrary[ByteBuffer] =
Arbitrary(Gen.alphaStr.map(a => ByteBuffer.wrap(a.getBytes(UTF_8))))
implicit val byte: Arbitrary[Byte] = Arbitrary(Gen.choose[Int](0, 255).map(_.toByte))
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/scala/agni/CqlSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class CqlSpec extends AsyncFunSpec with MockitoSugar with Matchers {

describe("getRows") {
it("should convert to Stream[Row] the value got from AsyncResultSet#currentPage()") {
val session = mock[CqlSession]
val stmt = mock[BoundStatement]
val session = mock[CqlSession]
val stmt = mock[BoundStatement]
val asyncResultSet0 = mock[AsyncResultSet]

when(session.executeAsync(stmt))
Expand Down Expand Up @@ -60,8 +60,8 @@ class CqlSpec extends AsyncFunSpec with MockitoSugar with Matchers {

it("should be stack-safe") {
val session = mock[CqlSession]
val stmt = mock[BoundStatement]
val r = mock[AsyncResultSet]
val stmt = mock[BoundStatement]
val r = mock[AsyncResultSet]

when(session.executeAsync(stmt)).thenReturn(CompletableFuture.completedFuture(r))

Expand Down
52 changes: 27 additions & 25 deletions examples/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ object Main extends IOApp with Matchers {
_ <- remake(session)

_ <- Cql.prepareAsync[IO](session, insertUserQuery).flatTap(a => IO(println(a.getQuery))) >>=
(p => users.traverse(x => insertUser(session, p, x)))
(p => users.traverse(x => insertUser(session, p, x)))

v = session.getContext.getProtocolVersion

xs <- Cql.prepareAsync[IO](session, selectUserQuery).flatTap(a => IO(println(a.getQuery))) >>=
(p => Cql.getRowsAs[IO, Author](session, p.bind()))
(p => Cql.getRowsAs[IO, Author](session, p.bind()))
} yield xs

def insertUser(session: CqlSession, p: PreparedStatement, a: Author) =
Expand All @@ -57,12 +57,12 @@ object Main extends IOApp with Matchers {
LocalDate.of(1932, 12, 15),
"female",
Map(
"The Country Girls" -> 1960,
"Girl with Green Eyes" -> 1962,
"The Country Girls" -> 1960,
"Girl with Green Eyes" -> 1962,
"Girls in Their Married Bliss" -> 1964,
"August is a Wicked Month" -> 1965,
"Casualties of Peace" -> 1966,
"Mother Ireland" -> 1976
"August is a Wicked Month" -> 1965,
"Casualties of Peace" -> 1966,
"Mother Ireland" -> 1976
)
),
Author(
Expand All @@ -72,31 +72,33 @@ object Main extends IOApp with Matchers {
LocalDate.of(1919, 8, 15),
"male",
Map(
"The Collected Stories of Benedict Kiely" -> 2001,
"The Trout in the Turnhole" -> 1996,
"A Letter to Peachtree" -> 1987,
"The Collected Stories of Benedict Kiely" -> 2001,
"The Trout in the Turnhole" -> 1996,
"A Letter to Peachtree" -> 1987,
"The State of Ireland: A Novella and Seventeen Short Stories" -> 1981,
"A Cow in the House" -> 1978,
"A Ball of Malt and Madame Butterfly" -> 1973,
"A Journey to the Seven Streams" -> 1963
"A Cow in the House" -> 1978,
"A Ball of Malt and Madame Butterfly" -> 1973,
"A Journey to the Seven Streams" -> 1963
)
),
Author(UUID.randomUUID(),
"Darren",
"Shan",
LocalDate.of(1972, 7, 2),
"male",
Map(
"Cirque Du Freak" -> 2000,
"The Vampire's Assistant" -> 2000,
"Tunnels of Blood" -> 2000
))
Author(
UUID.randomUUID(),
"Darren",
"Shan",
LocalDate.of(1972, 7, 2),
"male",
Map(
"Cirque Du Freak" -> 2000,
"The Vampire's Assistant" -> 2000,
"Tunnels of Blood" -> 2000
)
)
).sortBy(_.id)

}

object Query {
private[this] val keyspace = "agni_test"
private[this] val keyspace = "agni_test"
private[this] val tableName = "author"

val createKeyspaceQuery =
Expand Down Expand Up @@ -143,6 +145,6 @@ final case class Author(
object Author {
import agni.generic.semiauto._

implicit val bind: Binder[Author] = derivedBinder[Author]
implicit val bind: Binder[Author] = derivedBinder[Author]
implicit val decode: RowDecoder[Author] = derivedRowDecoder[Author]
}
18 changes: 9 additions & 9 deletions project/Boilerplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Boilerplate {
implicit class BlockHelper(val sc: StringContext) extends AnyVal {
def block(args: Any*): String = {
val interpolated = sc.standardInterpolator(treatEscapes, args)
val rawLines = interpolated.split('\n')
val rawLines = interpolated.split('\n')
val trimmedLines = rawLines.map { _.dropWhile(_.isWhitespace) }
trimmedLines.mkString("\n")
}
Expand All @@ -34,16 +34,16 @@ object Boilerplate {
tgtFile
}

val header = ""
val header = ""
val maxArity = 22

class TemplateVals(val arity: Int) {
val synTypes = (0 until arity).map(n => (n + 'A').toChar)
val synVals = (0 until arity).map(n => (n + 'a').toChar)
val synVals = (0 until arity).map(n => (n + 'a').toChar)

val `A..N` = synTypes.mkString(", ")
val `(A..N)` = if (arity == 1) "Tuple1[A]" else synTypes.mkString("(", ", ", ")")
val `(_.._)` = if (arity == 1) "Tuple1(a)" else synVals.map(_ => "_").mkString("(", ", ", ")")
val `A..N` = synTypes.mkString(", ")
val `(A..N)` = if (arity == 1) "Tuple1[A]" else synTypes.mkString("(", ", ", ")")
val `(_.._)` = if (arity == 1) "Tuple1(a)" else synVals.map(_ => "_").mkString("(", ", ", ")")
def `a:F[A]..n:F[N]`(f: String) = (synVals.zip(synTypes)).map { case (v, t) => s"$v: $f[$t]" }.mkString(", ")
}

Expand All @@ -56,9 +56,9 @@ object Boilerplate {
val rawContents = range.map { n =>
content(new TemplateVals(n)).split('\n').filterNot(_.isEmpty)
}
val preBody = rawContents.head.takeWhile(_.startsWith("|")).map(_.tail)
val preBody = rawContents.head.takeWhile(_.startsWith("|")).map(_.tail)
val instances = rawContents.flatMap { _.filter(_.startsWith("-")).map(_.tail) }
val postBody = rawContents.head.dropWhile(_.startsWith("|")).dropWhile(_.startsWith("-")).map(_.tail)
val postBody = rawContents.head.dropWhile(_.startsWith("|")).dropWhile(_.startsWith("-")).map(_.tail)
(headerLines ++ preBody ++ instances ++ postBody).mkString("\n")
}
}
Expand All @@ -82,7 +82,7 @@ object Boilerplate {
def file(root: File) = root / "agni" / "TupleRowDecoder.scala"
def content(tv: TemplateVals): String = {
import tv._
val expr = (synVals zipWithIndex).map { case (v, i) => s"$v.apply(row, $i, ver)" }.mkString("(", ", ", ")")
val expr = (synVals zipWithIndex).map { case (v, i) => s"$v.apply(row, $i, ver)" }.mkString("(", ", ", ")")
val tupled = if (arity == 1) s"$expr.map(Tuple1(_))" else s"$expr.mapN(${`(_.._)`})"
block"""
|package agni
Expand Down
38 changes: 19 additions & 19 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ object Dependencies {

val kindeProjector = "0.11.0"

val datastax = "4.3.0"
val cats = "2.0.0"
val shapeless = "2.3.3"
val datastax = "4.3.0"
val cats = "2.0.0"
val shapeless = "2.3.3"
val scalacheck = "1.14.1"
val scalatest = "3.0.8"
val catbird = "19.9.0"
val monix = "3.1.0"
val scalatest = "3.0.8"
val catbird = "19.9.0"
val monix = "3.1.0"
val catsEffect = "2.0.0"
val mockito = "3.1.0"
val slf4j = "1.7.13"
val mockito = "3.1.0"
val slf4j = "1.7.13"
}

val P = new {
val kindeProjector = "org.typelevel" % "kind-projector" % V.kindeProjector

val datastaxJavaDriver = "com.datastax.oss" % "java-driver-core" % V.datastax
val datastaxJavaDriver = "com.datastax.oss" % "java-driver-core" % V.datastax
val datastaxQueryBuilder = "com.datastax.oss" % "java-driver-query-builder" % V.datastax
val catsCore = "org.typelevel" %% "cats-core" % V.cats
val shapeless = "com.chuusai" %% "shapeless" % V.shapeless
val catbird = "io.catbird" %% "catbird-util" % V.catbird
val monixEval = "io.monix" %% "monix-eval" % V.monix
val monixTail = "io.monix" %% "monix-tail" % V.monix
val catsEffect = "org.typelevel" %% "cats-effect" % V.catsEffect
val scalacheck = "org.scalacheck" %% "scalacheck" % V.scalacheck
val scalatest = "org.scalatest" %% "scalatest" % V.scalatest
val mockito = "org.mockito" % "mockito-core" % V.mockito
val slf4jSimple = "org.slf4j" % "slf4j-simple" % V.slf4j
val catsCore = "org.typelevel" %% "cats-core" % V.cats
val shapeless = "com.chuusai" %% "shapeless" % V.shapeless
val catbird = "io.catbird" %% "catbird-util" % V.catbird
val monixEval = "io.monix" %% "monix-eval" % V.monix
val monixTail = "io.monix" %% "monix-tail" % V.monix
val catsEffect = "org.typelevel" %% "cats-effect" % V.catsEffect
val scalacheck = "org.scalacheck" %% "scalacheck" % V.scalacheck
val scalatest = "org.scalatest" %% "scalatest" % V.scalatest
val mockito = "org.mockito" % "mockito-core" % V.mockito
val slf4jSimple = "org.slf4j" % "slf4j-simple" % V.slf4j
}
}
Loading

0 comments on commit 1cbeb3c

Please sign in to comment.