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

Commit d04ba01

Browse files
committed
Add scala 3 support for circe
Add missing comma
1 parent 6ea80f7 commit d04ba01

File tree

5 files changed

+188
-36
lines changed

5 files changed

+188
-36
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2015 Heiko Seeberger
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.heikoseeberger.akkahttpcirce
18+
19+
import akka.actor.ActorSystem
20+
import akka.http.scaladsl.marshalling.Marshal
21+
import akka.http.scaladsl.model._
22+
import akka.http.scaladsl.unmarshalling.Unmarshal
23+
import akka.stream.scaladsl.{ Sink, Source }
24+
import io.circe.Encoder
25+
import org.scalatest.concurrent.ScalaFutures
26+
import org.scalatest.matchers.should.Matchers
27+
import org.scalatest.wordspec.AsyncWordSpec
28+
import org.scalatest.{ BeforeAndAfterAll, EitherValues }
29+
30+
import scala.concurrent.Await
31+
import scala.concurrent.duration.DurationInt
32+
33+
final class CirceSupportSpec2
34+
extends AsyncWordSpec
35+
with Matchers
36+
with BeforeAndAfterAll
37+
with ScalaFutures
38+
with EitherValues {
39+
40+
import CirceSupportSpec._
41+
42+
private implicit val system: ActorSystem = ActorSystem()
43+
44+
/**
45+
* Specs common to both [[FailFastCirceSupport]] and [[ErrorAccumulatingCirceSupport]]
46+
*/
47+
private def commonCirceSupport(support: BaseCirceSupport) = {
48+
import io.circe.generic.auto._
49+
import support._
50+
51+
"enable streamed marshalling and unmarshalling for json arrays" in {
52+
val foos = (0 to 100).map(i => Foo(s"bar-$i")).toList
53+
54+
// Don't know why, the encoder is not resolving alongside the marshaller
55+
// this only happens if we use the implicits from BaseCirceSupport
56+
// so, tried to create it before and guess what? it worked.
57+
// not sure if this is a bug, but, the error is this:
58+
// diverging implicit expansion for type io.circe.Encoder[A]
59+
// [error] starting with lazy value encodeZoneOffset in object Encoder
60+
implicit val e = implicitly[Encoder[Foo]]
61+
62+
Marshal(Source(foos))
63+
.to[ResponseEntity]
64+
.flatMap(entity => Unmarshal(entity).to[SourceOf[Foo]])
65+
.flatMap(_.runWith(Sink.seq))
66+
.map(_ shouldBe foos)
67+
}
68+
69+
}
70+
71+
"FailFastCirceSupport" should {
72+
behave like commonCirceSupport(FailFastCirceSupport)
73+
}
74+
75+
"ErrorAccumulatingCirceSupport" should {
76+
behave like commonCirceSupport(ErrorAccumulatingCirceSupport)
77+
78+
}
79+
80+
override protected def afterAll(): Unit = {
81+
Await.ready(system.terminate(), 42.seconds)
82+
super.afterAll()
83+
}
84+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2015 Heiko Seeberger
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.heikoseeberger.akkahttpcirce
18+
19+
import akka.actor.ActorSystem
20+
import akka.http.scaladsl.marshalling.Marshal
21+
import akka.http.scaladsl.model._
22+
import akka.http.scaladsl.unmarshalling.Unmarshal
23+
import akka.stream.scaladsl.{ Sink, Source }
24+
import org.scalatest.concurrent.ScalaFutures
25+
import org.scalatest.matchers.should.Matchers
26+
import org.scalatest.wordspec.AsyncWordSpec
27+
import org.scalatest.{ BeforeAndAfterAll, EitherValues }
28+
29+
import scala.concurrent.Await
30+
import scala.concurrent.duration.DurationInt
31+
32+
final class CirceSupportSpec2
33+
extends AsyncWordSpec
34+
with Matchers
35+
with BeforeAndAfterAll
36+
with ScalaFutures
37+
with EitherValues {
38+
39+
import CirceSupportSpec._
40+
41+
private implicit val system: ActorSystem = ActorSystem()
42+
43+
/**
44+
* Specs common to both [[FailFastCirceSupport]] and [[ErrorAccumulatingCirceSupport]]
45+
*/
46+
private def commonCirceSupport(support: BaseCirceSupport) = {
47+
import io.circe.generic.auto._
48+
import support._
49+
50+
"enable streamed marshalling and unmarshalling for json arrays" in {
51+
val foos = (0 to 100).map(i => Foo(s"bar-$i")).toList
52+
53+
Marshal(Source(foos))
54+
.to[ResponseEntity]
55+
.flatMap(entity => Unmarshal(entity).to[SourceOf[Foo]])
56+
.flatMap(_.runWith(Sink.seq))
57+
.map(_ shouldBe foos)
58+
}
59+
60+
}
61+
62+
"FailFastCirceSupport" should {
63+
behave like commonCirceSupport(FailFastCirceSupport)
64+
}
65+
66+
"ErrorAccumulatingCirceSupport" should {
67+
behave like commonCirceSupport(ErrorAccumulatingCirceSupport)
68+
69+
}
70+
71+
override protected def afterAll(): Unit = {
72+
Await.ready(system.terminate(), 42.seconds)
73+
super.afterAll()
74+
}
75+
}

akka-http-circe/src/test/scala/de/heikoseeberger/akkahttpcirce/CirceSupportSpec.scala

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,18 @@ package de.heikoseeberger.akkahttpcirce
1818

1919
import akka.actor.ActorSystem
2020
import akka.http.scaladsl.marshalling.Marshal
21-
import akka.http.scaladsl.model.{
22-
ContentTypeRange,
23-
HttpCharsets,
24-
HttpEntity,
25-
MediaType,
26-
RequestEntity,
27-
ResponseEntity
28-
}
2921
import akka.http.scaladsl.model.ContentTypes.{ `application/json`, `text/plain(UTF-8)` }
30-
import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller }
22+
import akka.http.scaladsl.model._
3123
import akka.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException
32-
import akka.stream.scaladsl.{ Sink, Source }
24+
import akka.http.scaladsl.unmarshalling.{ Unmarshal, Unmarshaller }
3325
import cats.data.{ NonEmptyList, ValidatedNel }
34-
import io.circe.{ DecodingFailure, Encoder, ParsingFailure, Printer }
3526
import io.circe.CursorOp.DownField
36-
import org.scalatest.{ BeforeAndAfterAll, EitherValues }
27+
import io.circe.{ DecodingFailure, ParsingFailure, Printer }
3728
import org.scalatest.concurrent.ScalaFutures
3829
import org.scalatest.matchers.should.Matchers
3930
import org.scalatest.wordspec.AsyncWordSpec
31+
import org.scalatest.{ BeforeAndAfterAll, EitherValues }
32+
4033
import scala.concurrent.Await
4134
import scala.concurrent.duration.DurationInt
4235

@@ -79,24 +72,6 @@ final class CirceSupportSpec
7972
.map(_ shouldBe foo)
8073
}
8174

82-
"enable streamed marshalling and unmarshalling for json arrays" in {
83-
val foos = (0 to 100).map(i => Foo(s"bar-$i")).toList
84-
85-
// Don't know why, the encoder is not resolving alongside the marshaller
86-
// this only happens if we use the implicits from BaseCirceSupport
87-
// so, tried to create it before and guess what? it worked.
88-
// not sure if this is a bug, but, the error is this:
89-
// diverging implicit expansion for type io.circe.Encoder[A]
90-
// [error] starting with lazy value encodeZoneOffset in object Encoder
91-
implicit val e = implicitly[Encoder[Foo]]
92-
93-
Marshal(Source(foos))
94-
.to[ResponseEntity]
95-
.flatMap(entity => Unmarshal(entity).to[SourceOf[Foo]])
96-
.flatMap(_.runWith(Sink.seq))
97-
.map(_ shouldBe foos)
98-
}
99-
10075
"provide proper error messages for requirement errors" in {
10176
val entity = HttpEntity(`application/json`, """{ "bar": "baz" }""")
10277
Unmarshal(entity)

akka-http-circe/src/test/scala/de/heikoseeberger/akkahttpcirce/ExampleApp.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object ExampleApp {
3131
private final case class Foo(bar: String)
3232

3333
def main(args: Array[String]): Unit = {
34-
implicit val system = ActorSystem()
34+
implicit val system: ActorSystem = ActorSystem()
3535

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

@@ -54,7 +54,7 @@ object ExampleApp {
5454
}
5555
} ~ pathPrefix("stream") {
5656
post {
57-
entity(as[SourceOf[Foo]]) { fooSource: SourceOf[Foo] =>
57+
entity(as[SourceOf[Foo]]) { (fooSource: SourceOf[Foo]) =>
5858
import sys._
5959

6060
Marshal(Source.single(Foo("a"))).to[RequestEntity]

build.sbt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Build settings
33
// *****************************************************************************
44

5+
lazy val scalaReleaseVersion = SettingKey[Int]("scalaReleaseVersion")
56
inThisBuild(
67
Seq(
78
organization := "de.heikoseeberger",
@@ -35,7 +36,13 @@ inThisBuild(
3536
"-target:jvm-1.8"
3637
),
3738
scalafmtOnCompile := true,
38-
dynverSeparator := "_" // the default `+` is not compatible with docker tags,
39+
dynverSeparator := "_", // the default `+` is not compatible with docker tags,
40+
scalaReleaseVersion := {
41+
lazy val v = scalaVersion.value
42+
CrossVersion.partialVersion(v).map(_._1.toInt).getOrElse {
43+
throw new RuntimeException(s"could not get Scala release version from $v")
44+
}
45+
}
3946
)
4047
)
4148

@@ -86,16 +93,27 @@ lazy val `akka-http-argonaut` =
8693
lazy val `akka-http-circe` =
8794
project
8895
.enablePlugins(AutomateHeaderPlugin)
89-
.settings(commonSettings)
96+
.settings(commonSettings, withScala3)
9097
.settings(
9198
libraryDependencies ++= Seq(
9299
library.akkaHttp,
93100
library.circe,
94101
library.circeParser,
95102
library.akkaStream % Provided,
96103
library.circeGeneric % Test,
97-
library.scalaTest % Test,
98-
)
104+
library.scalaTest % Test,
105+
),
106+
Test / unmanagedSourceDirectories ++= {
107+
if (scalaReleaseVersion.value > 2) {
108+
Seq(
109+
(LocalRootProject / baseDirectory).value / "src" / "test" / "scala-3"
110+
)
111+
} else {
112+
Seq(
113+
(LocalRootProject / baseDirectory).value / "src" / "test" / "scala-2",
114+
)
115+
}
116+
}
99117
)
100118

101119
lazy val `akka-http-jackson` =

0 commit comments

Comments
 (0)