Skip to content

Commit

Permalink
Implement rough hacks for 2.13 (#204)
Browse files Browse the repository at this point in the history
* Implement rough hacks for 2.13
* Remove unneeded SyncVar import
* Reduce scope of changes for cross-compilation
* Remove redundant imports
* Add file for compatibility with 2.11
* Add comments explaining decisions made for 2.13
  • Loading branch information
changlinli authored and gvolpe committed Jun 27, 2019
1 parent c8cd3e9 commit a95fd55
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 74 deletions.
98 changes: 67 additions & 31 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name := """fs2-rabbit-root"""

organization in ThisBuild := "dev.profunktor"

crossScalaVersions in ThisBuild := Seq("2.11.12", "2.12.8")
crossScalaVersions in ThisBuild := Seq("2.11.12", "2.12.8", "2.13.0")

// makes `tut` fail :( -> https://github.com/tpolecat/tut/issues/255
//scalaVersion in ThisBuild := "2.12.8" // needed for metals
Expand All @@ -19,23 +19,40 @@ promptTheme := PromptTheme(List(
text(_ => "fs2-rabbit", fg(15)).padRight(" λ ")
))



// We use String as our input type because `scalaVersion.value` cannot be called
// in a lot of places in a build.sbt file where it would be convenient to do so
// and so we have to thread it through at the last moment instead and
// scalaVersion.value is a String.
def determineVersionSpecificDeps(scalaVersionStr: String) = CrossVersion.partialVersion(scalaVersionStr) match {
case Some((2, 13)) => Scala213Dependencies
case Some((2, 12)) => Scala212Dependencies
case Some((2, 11)) => Scala211Dependencies
// Fallback to 2.12 libraries as they're currently the most well-supported
case _ => Scala212Dependencies
}

val commonSettings = Seq(
organizationName := "ProfunKtor",
startYear := Some(2017),
licenses += ("Apache-2.0", new URL("https://www.apache.org/licenses/LICENSE-2.0.txt")),
homepage := Some(url("https://fs2-rabbit.profunktor.dev/")),
headerLicense := Some(HeaderLicense.ALv2("2017-2019", "ProfunKtor")),
libraryDependencies ++= Seq(
compilerPlugin(Libraries.kindProjector),
compilerPlugin(Libraries.betterMonadicFor),
Libraries.amqpClient,
Libraries.catsEffect,
Libraries.fs2Core,
Libraries.scalaTest % "test",
Libraries.scalaCheck % "test"
),
scalacOptions ++= determineVersionSpecificDeps(scalaVersion.value).scalacOptions,
libraryDependencies ++= {
val library = determineVersionSpecificDeps(scalaVersion.value)
Seq(
compilerPlugin(library.kindProjector),
compilerPlugin(library.betterMonadicFor),
library.amqpClient,
library.catsEffect,
library.fs2Core,
library.scalaTest % "test",
library.scalaCheck % "test"
)
},
resolvers += "Apache public" at "https://repository.apache.org/content/groups/public/",
scalacOptions ++= Seq("-Xmax-classfile-name", "100"),
scalafmtOnCompile := true,
publishTo := {
val sonatype = "https://oss.sonatype.org/"
Expand All @@ -57,22 +74,41 @@ val commonSettings = Seq(
</developers>
)

val CoreDependencies: Seq[ModuleID] = Seq(
Libraries.logback % "test"
)

val JsonDependencies: Seq[ModuleID] = Seq(
Libraries.circeCore,
Libraries.circeGeneric,
Libraries.circeParser
)

val ExamplesDependencies: Seq[ModuleID] = Seq(
Libraries.monix,
Libraries.zioCore,
Libraries.zioCats,
Libraries.logback % "runtime"
)
def CoreDependencies(scalaVersionStr: String): Seq[ModuleID] = {
val library = determineVersionSpecificDeps(scalaVersionStr)
Seq(
library.logback % "test"
)
}

def JsonDependencies(scalaVersionStr: String): Seq[ModuleID] = {
val library = determineVersionSpecificDeps(scalaVersionStr)
Seq(
library.circeCore,
library.circeGeneric,
library.circeParser
)
}

def ExamplesDependencies(scalaVersionStr: String): Seq[ModuleID] = {
determineVersionSpecificDeps(scalaVersionStr) match {
case library: Scala213Dependencies.type => Seq(library.logback % "runtime")
case library: Scala212Dependencies.type =>
Seq(
library.logback % "runtime",
library.monix,
library.zioCore,
library.zioCats
)
case library: Scala211Dependencies.type =>
Seq(
library.logback % "runtime",
library.monix,
library.zioCore,
library.zioCats
)
}
}

lazy val noPublish = Seq(
publish := {},
Expand All @@ -87,20 +123,20 @@ lazy val `fs2-rabbit-root` = project.in(file("."))

lazy val `fs2-rabbit` = project.in(file("core"))
.settings(commonSettings: _*)
.settings(libraryDependencies ++= CoreDependencies)
.settings(libraryDependencies ++= CoreDependencies(scalaVersion.value))
.settings(parallelExecution in Test := false)
.enablePlugins(AutomateHeaderPlugin)

lazy val `fs2-rabbit-circe` = project.in(file("json-circe"))
.settings(commonSettings: _*)
.settings(libraryDependencies ++= JsonDependencies)
.settings(libraryDependencies ++= JsonDependencies(scalaVersion.value))
.settings(parallelExecution in Test := false)
.enablePlugins(AutomateHeaderPlugin)
.dependsOn(`fs2-rabbit`)

lazy val `fs2-rabbit-test-support` = project.in(file("test-support"))
.settings(commonSettings: _*)
.settings(libraryDependencies += Libraries.scalaTest)
.settings(libraryDependencies += determineVersionSpecificDeps(scalaVersion.value).scalaTest)
.enablePlugins(AutomateHeaderPlugin)
.dependsOn(`fs2-rabbit`)

Expand All @@ -113,7 +149,7 @@ lazy val tests = project.in(file("tests"))

lazy val examples = project.in(file("examples"))
.settings(commonSettings: _*)
.settings(libraryDependencies ++= ExamplesDependencies)
.settings(libraryDependencies ++= ExamplesDependencies(scalaVersion.value))
.settings(noPublish)
.enablePlugins(AutomateHeaderPlugin)
.dependsOn(`fs2-rabbit`, `fs2-rabbit-circe`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017-2019 ProfunKtor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.profunktor.fs2rabbit

import scala.collection.convert.{DecorateAsJava, DecorateAsScala}

// This exists purely for compatibility between Scala 2.13 and 2.12 since the
// Java conversions have been moved into a different package between the two,
// allowing us to have a single, consistent import everywhere else in this
// codebase across both 2.13 and 2.12.
object javaConversion extends DecorateAsJava with DecorateAsScala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017-2019 ProfunKtor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.profunktor.fs2rabbit

import scala.collection.convert.{DecorateAsJava, DecorateAsScala}

// This exists purely for compatibility between Scala 2.13 and 2.12 since the
// Java conversions have been moved into a different package between the two,
// allowing us to have a single, consistent import everywhere else in this
// codebase across both 2.13 and 2.12.
object javaConversion extends DecorateAsJava with DecorateAsScala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017-2019 ProfunKtor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.profunktor.fs2rabbit

import scala.collection.convert.{AsJavaExtensions, AsScalaExtensions}

// This exists purely for compatibility between Scala 2.13 and 2.12 since the
// Java conversions have been moved into a different package between the two,
// allowing us to have a single, consistent import everywhere else in this
// codebase across both 2.13 and 2.12.
object javaConversion extends AsJavaExtensions with AsScalaExtensions
5 changes: 1 addition & 4 deletions core/src/main/scala/dev/profunktor/fs2rabbit/arguments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package dev.profunktor.fs2rabbit

import scala.annotation.implicitNotFound
import scala.language.implicitConversions
import scala.collection.JavaConverters._
import dev.profunktor.fs2rabbit.javaConversion._

object arguments {

Expand Down Expand Up @@ -58,8 +57,6 @@ object arguments {
def toJavaType(a: A) = f(a)
}

import scala.collection.JavaConverters._

implicit val stringInstance: SafeArgument[String] = instance(identity)
implicit val bigDecimalInstance: SafeArgument[BigDecimal] = instance(_.bigDecimal)
implicit val intInstance: SafeArgument[Int] = instance(Int.box)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ object EnvelopeDecoder {
def longHeader[F[_]: ApplicativeError[?[_], Throwable]](name: String): EnvelopeDecoder[F, Long] =
headerPF[F, Long](name) { case LongVal(a) => a }

def arrayHeader[F[_]: ApplicativeError[?[_], Throwable]](name: String): EnvelopeDecoder[F, Seq[Any]] =
headerPF[F, Seq[Any]](name) { case ArrayVal(a) => a }
def arrayHeader[F[_]: ApplicativeError[?[_], Throwable]](name: String): EnvelopeDecoder[F, collection.Seq[Any]] =
headerPF[F, collection.Seq[Any]](name) { case ArrayVal(a) => a }

def optStringHeader[F[_]: ApplicativeError[?[_], Throwable]](name: String): EnvelopeDecoder[F, Option[String]] =
optHeaderPF[F, String](name) { case StringVal(a) => a }
Expand All @@ -66,8 +66,9 @@ object EnvelopeDecoder {
def optLongHeader[F[_]: ApplicativeError[?[_], Throwable]](name: String): EnvelopeDecoder[F, Option[Long]] =
optHeaderPF[F, Long](name) { case LongVal(a) => a }

def optArrayHeader[F[_]: ApplicativeError[?[_], Throwable]](name: String): EnvelopeDecoder[F, Option[Seq[Any]]] =
optHeaderPF[F, Seq[Any]](name) { case ArrayVal(a) => a }
def optArrayHeader[F[_]: ApplicativeError[?[_], Throwable]](
name: String): EnvelopeDecoder[F, Option[collection.Seq[Any]]] =
optHeaderPF[F, collection.Seq[Any]](name) { case ArrayVal(a) => a }

private def headerPF[F[_], A](name: String)(pf: PartialFunction[AmqpHeaderVal, A])(
implicit F: ApplicativeError[F, Throwable]): EnvelopeDecoder[F, A] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package dev.profunktor.fs2rabbit.interpreter
import cats.data.NonEmptyList
import cats.effect.{Resource, Sync}
import cats.implicits._
import com.rabbitmq.client.{Address, ConnectionFactory}
import dev.profunktor.fs2rabbit.algebra.Connection
import dev.profunktor.fs2rabbit.config.Fs2RabbitConfig
import dev.profunktor.fs2rabbit.effects.Log
import dev.profunktor.fs2rabbit.javaConversion._
import dev.profunktor.fs2rabbit.model.{AMQPChannel, AMQPConnection, RabbitChannel, RabbitConnection}
import com.rabbitmq.client.{Address, ConnectionFactory}
import javax.net.ssl.SSLContext

import scala.collection.JavaConverters._

class ConnectionEffect[F[_]: Log: Sync](
factory: ConnectionFactory,
addresses: NonEmptyList[Address]
Expand Down
15 changes: 8 additions & 7 deletions core/src/main/scala/dev/profunktor/fs2rabbit/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import cats.implicits._
import dev.profunktor.fs2rabbit.arguments.Arguments
import dev.profunktor.fs2rabbit.effects.{EnvelopeDecoder, MessageEncoder}
import dev.profunktor.fs2rabbit.model.AmqpHeaderVal._
import dev.profunktor.fs2rabbit.javaConversion._
import com.rabbitmq.client.impl.LongStringHelper
import com.rabbitmq.client.{AMQP, Channel, Connection, LongString}
import fs2.Stream

import scala.collection.JavaConverters._

object model {

type StreamAckerConsumer[F[_], A] = (AckResult => F[Unit], Stream[F, AmqpEnvelope[A]])
Expand Down Expand Up @@ -92,10 +91,10 @@ object model {
}

object AmqpHeaderVal {
final case class IntVal(value: Int) extends AmqpHeaderVal
final case class LongVal(value: Long) extends AmqpHeaderVal
final case class StringVal(value: String) extends AmqpHeaderVal
final case class ArrayVal(v: Seq[Any]) extends AmqpHeaderVal
final case class IntVal(value: Int) extends AmqpHeaderVal
final case class LongVal(value: Long) extends AmqpHeaderVal
final case class StringVal(value: String) extends AmqpHeaderVal
final case class ArrayVal(v: collection.Seq[Any]) extends AmqpHeaderVal

def from(value: AnyRef): AmqpHeaderVal = value match {
case ls: LongString => StringVal(new String(ls.getBytes, "UTF-8"))
Expand Down Expand Up @@ -161,7 +160,9 @@ object model {
.expiration(props.expiration.orNull)
.replyTo(props.replyTo.orNull)
.clusterId(props.clusterId.orNull)
.headers(props.headers.mapValues[AnyRef](_.impure).asJava)
// Note we don't use mapValues here to maintain compatibility between
// Scala 2.12 and 2.13
.headers(props.headers.map { case (key, value) => (key, value.impure) }.asJava)
.build()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package dev.profunktor.fs2rabbit.examples
import dev.profunktor.fs2rabbit.config.Fs2RabbitConfig
import dev.profunktor.fs2rabbit.interpreter.Fs2Rabbit

import scalaz.zio._
import scalaz.zio.interop.catz._
import scalaz.zio.interop.catz.implicits._
import zio._
import zio.interop.catz._
import zio.interop.catz.implicits._
import dev.profunktor.fs2rabbit.resiliency.ResilientStream

object ZIOAutoAckConsumer extends CatsApp {
Expand Down
Loading

0 comments on commit a95fd55

Please sign in to comment.