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

Commit

Permalink
Add scala 3 support (#754)
Browse files Browse the repository at this point in the history
Thanks a lot!
  • Loading branch information
DieBauer authored Nov 21, 2022
1 parent b9d29ea commit 291e97d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller }
import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException
import akka.stream.scaladsl.{ Sink, Source }
import argonaut.Argonaut._
import argonaut.CodecJson
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec

import scala.concurrent.Await
import scala.concurrent.duration.DurationInt

Expand All @@ -41,8 +43,9 @@ final class ArgonautSupportSpec extends AsyncWordSpec with Matchers with BeforeA

import ArgonautSupportSpec._

private implicit val system = ActorSystem()
private implicit def fooCodec = casecodec1(Foo.apply, Foo.unapply)("bar")
private implicit val system: ActorSystem = ActorSystem()
private implicit def fooCodec: CodecJson[Foo] =
casecodec1(Foo.apply, (f: Foo) => Option(f.bar))("bar")

"ArgonautSupport" should {
"enable marshalling and unmarshalling objects for generic derivation" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ object ExampleApp {

final object Foo {
implicit val fooCodec: CodecJson[Foo] =
casecodec1(Foo.apply, Foo.unapply)("bar")
casecodec1(Foo.apply, (f: Foo) => Option(f.bar))("bar")
}
final case class Foo(bar: String)

def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val system: ActorSystem = ActorSystem()

Http().newServerAt("127.0.0.1", 8000).bindFlow(route)

Expand All @@ -61,7 +61,7 @@ object ExampleApp {
}
} ~ pathPrefix("stream") {
post {
entity(as[SourceOf[Foo]]) { fooSource: SourceOf[Foo] =>
entity(as[SourceOf[Foo]]) { (fooSource: SourceOf[Foo]) =>
import sys._

Marshal(Source.single(Foo("a"))).to[RequestEntity]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.scaladsl.Source
import org.json4s.jackson.Serialization
import org.json4s.{ DefaultFormats, jackson }

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.io.StdIn
Expand All @@ -32,7 +34,7 @@ object ExampleApp {
final case class Foo(bar: String)

def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val system: ActorSystem = ActorSystem()

Http().newServerAt("127.0.0.1", 8000).bindFlow(route)

Expand All @@ -44,8 +46,9 @@ object ExampleApp {
import Directives._
import Json4sSupport._

implicit val serialization = jackson.Serialization // or native.Serialization
implicit val formats = DefaultFormats
implicit val serialization: Serialization.type =
jackson.Serialization // or native.Serialization
implicit val formats: DefaultFormats.type = DefaultFormats

pathSingleSlash {
post {
Expand All @@ -57,7 +60,7 @@ object ExampleApp {
}
} ~ pathPrefix("stream") {
post {
entity(as[SourceOf[Foo]]) { fooSource: SourceOf[Foo] =>
entity(as[SourceOf[Foo]]) { (fooSource: SourceOf[Foo]) =>
complete(fooSource.throttle(1, 2.seconds))
}
} ~ get {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import akka.http.scaladsl.model.ContentTypes.{ `application/json`, `text/plain(U
import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller }
import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException
import akka.stream.scaladsl.{ Sink, Source }
import org.json4s.jackson.Serialization
import org.json4s.{ DefaultFormats, jackson, native }
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec

import scala.concurrent.Await
import scala.concurrent.duration.DurationInt

Expand All @@ -41,22 +43,22 @@ final class Json4sSupportSpec extends AsyncWordSpec with Matchers with BeforeAnd
import Json4sSupport._
import Json4sSupportSpec._

private implicit val system = ActorSystem()
private implicit val formats = DefaultFormats
private implicit val system: ActorSystem = ActorSystem()
private implicit val formats: DefaultFormats.type = DefaultFormats

private val foo = Foo("bar")

"Json4sSupport" should {
"enable marshalling and unmarshalling objects for `DefaultFormats` and `jackson.Serialization`" in {
implicit val serialization = jackson.Serialization
implicit val serialization: Serialization.type = jackson.Serialization
Marshal(foo)
.to[RequestEntity]
.flatMap(Unmarshal(_).to[Foo])
.map(_ shouldBe foo)
}

"enable streamed marshalling and unmarshalling for json arrays" in {
implicit val serialization = jackson.Serialization
implicit val serialization: Serialization.type = jackson.Serialization

val foos = (0 to 100).map(i => Foo(s"bar-$i")).toList

Expand All @@ -72,15 +74,15 @@ final class Json4sSupportSpec extends AsyncWordSpec with Matchers with BeforeAnd
}

"enable marshalling and unmarshalling objects for `DefaultFormats` and `native.Serialization`" in {
implicit val serialization = native.Serialization
implicit val serialization: native.Serialization.type = native.Serialization
Marshal(foo)
.to[RequestEntity]
.flatMap(Unmarshal(_).to[Foo])
.map(_ shouldBe foo)
}

"provide proper error messages for requirement errors" in {
implicit val serialization = native.Serialization
implicit val serialization: native.Serialization.type = native.Serialization
val entity =
HttpEntity(MediaTypes.`application/json`, """{ "bar": "baz" }""")
Unmarshal(entity)
Expand All @@ -90,17 +92,17 @@ final class Json4sSupportSpec extends AsyncWordSpec with Matchers with BeforeAnd
}

"fail with NoContentException when unmarshalling empty entities" in {
implicit val serialization = native.Serialization
val entity = HttpEntity.empty(`application/json`)
implicit val serialization: native.Serialization.type = native.Serialization
val entity = HttpEntity.empty(`application/json`)
Unmarshal(entity)
.to[Foo]
.failed
.map(_ shouldBe Unmarshaller.NoContentException)
}

"fail with UnsupportedContentTypeException when Content-Type is not `application/json`" in {
implicit val serialization = native.Serialization
val entity = HttpEntity("""{ "bar": "bar" }""")
implicit val serialization: native.Serialization.type = native.Serialization
val entity = HttpEntity("""{ "bar": "bar" }""")
Unmarshal(entity)
.to[Foo]
.failed
Expand All @@ -110,8 +112,8 @@ final class Json4sSupportSpec extends AsyncWordSpec with Matchers with BeforeAnd
}

"allow unmarshalling with passed in Content-Types" in {
implicit val serialization = native.Serialization
val foo = Foo("bar")
implicit val serialization: native.Serialization.type = native.Serialization
val foo = Foo("bar")
val `application/json-home` =
MediaType.applicationWithFixedCharset("json-home", HttpCharsets.`UTF-8`, "json-home")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object ExampleApp {
}
} ~ pathPrefix("stream") {
post {
entity(as[SourceOf[Foo]]) { fooSource: SourceOf[Foo] =>
entity(as[SourceOf[Foo]]) { (fooSource: SourceOf[Foo]) =>
complete(fooSource.throttle(1, 2.seconds))
}
} ~ get {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object ExampleApp {
}
} ~ pathPrefix("stream") {
post {
entity(as[SourceOf[Foo]]) { fooSource: SourceOf[Foo] =>
entity(as[SourceOf[Foo]]) { (fooSource: SourceOf[Foo]) =>
import sys._

Marshal(Source.single(Foo("a"))).to[RequestEntity]
Expand Down
30 changes: 17 additions & 13 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ inThisBuild(
url("https://github.com/hseeberger")
)
),
scalaVersion := "2.13.8",
crossScalaVersions := Seq(scalaVersion.value, "2.12.15"),
scalaVersion := "2.13.10",
crossScalaVersions := Seq(scalaVersion.value, "2.12.17"),
scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
Expand All @@ -39,6 +39,10 @@ inThisBuild(
)
)

val withScala3 = Seq(
crossScalaVersions += "3.2.1",
)

// *****************************************************************************
// Projects
// *****************************************************************************
Expand Down Expand Up @@ -69,7 +73,7 @@ lazy val `akka-http-json` =
lazy val `akka-http-argonaut` =
project
.enablePlugins(AutomateHeaderPlugin)
.settings(commonSettings)
.settings(commonSettings, withScala3)
.settings(
libraryDependencies ++= Seq(
library.akkaHttp,
Expand Down Expand Up @@ -112,7 +116,7 @@ lazy val `akka-http-jackson` =
lazy val `akka-http-json4s` =
project
.enablePlugins(AutomateHeaderPlugin)
.settings(commonSettings)
.settings(commonSettings, withScala3)
.settings(
libraryDependencies ++= Seq(
library.akkaHttp,
Expand All @@ -127,7 +131,7 @@ lazy val `akka-http-json4s` =
lazy val `akka-http-jsoniter-scala` =
project
.enablePlugins(AutomateHeaderPlugin)
.settings(commonSettings)
.settings(commonSettings, withScala3)
.settings(
libraryDependencies ++= Seq(
library.akkaHttp,
Expand Down Expand Up @@ -193,7 +197,7 @@ lazy val `akka-http-avro4s` =
lazy val `akka-http-zio-json` =
project
.enablePlugins(AutomateHeaderPlugin)
.settings(commonSettings)
.settings(commonSettings, withScala3)
.settings(
libraryDependencies ++= Seq(
library.akkaHttp,
Expand Down Expand Up @@ -223,23 +227,23 @@ lazy val commonSettings =
lazy val library =
new {
object Version {
val akka = "2.6.18"
val akkaHttp = "10.2.8"
val akka = "2.6.20"
val akkaHttp = "10.2.10"
val argonaut = "6.3.8"
val avro4s = "4.0.12"
val circe = "0.14.1"
val jacksonModuleScala = "2.13.1"
val jsoniterScala = "2.10.3"
val json4s = "4.0.4"
val json4s = "4.0.6"
val jsoniterScala = "2.17.9"
val ninny = "0.7.0"
val play = "2.9.2"
val scalaTest = "3.2.11"
val upickle = "1.5.0"
val zioJson = "0.3.0-RC9"
val zioJson = "0.3.0"
}
// format: off
val akkaHttp = "com.typesafe.akka" %% "akka-http" % Version.akkaHttp
val akkaHttpJacksonJava = "com.typesafe.akka" %% "akka-http-jackson" % Version.akkaHttp
val akkaHttp = ("com.typesafe.akka" %% "akka-http" % Version.akkaHttp).cross(CrossVersion.for3Use2_13)
val akkaHttpJacksonJava = ("com.typesafe.akka" %% "akka-http-jackson" % Version.akkaHttp).cross(CrossVersion.for3Use2_13)
val akkaStream = "com.typesafe.akka" %% "akka-stream" % Version.akka
val argonaut = "io.argonaut" %% "argonaut" % Version.argonaut
val avro4sJson = "com.sksamuel.avro4s" %% "avro4s-json" % Version.avro4s
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.6.2
sbt.version = 1.8.0

0 comments on commit 291e97d

Please sign in to comment.