Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scala 3: compile and test marshalling, JMH and docs subprojects #4126

Merged
merged 4 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion akka-http-bench-jmh/src/main/scala/akka/BenchRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object BenchRunner {
val opts = new CommandLineOptions(args2: _*)
val results = new Runner(opts).run()

val report = results.asScala.map { result: RunResult =>
val report = results.asScala.map { (result: RunResult) =>
val bench = result.getParams.getBenchmark
val params = result.getParams.getParamsKeys.asScala.map(key => s"$key=${result.getParams.getParam(key)}").mkString("_")
val score = result.getAggregatedResult.getPrimaryResult.getScore.round
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package akka.http.scaladsl.server.util
/**
* Constructor for instances of type `R` which can be created from a tuple of type `T`.
*/
@FunctionalInterface
trait ConstructFromTuple[T, R] extends (T => R)

object ConstructFromTuple extends ConstructFromTupleInstances
19 changes: 11 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,10 @@ lazy val httpJmhBench = project("akka-http-bench-jmh")
.enablePlugins(JmhPlugin)
.enablePlugins(NoPublish) // don't release benchs
.disablePlugins(MimaPlugin)
.enablePlugins(NoScala3) // FIXME

lazy val httpMarshallersScala = project("akka-http-marshallers-scala")
.settings(commonSettings)
.enablePlugins(NoPublish, NoScala3 /*FIXME */ /*, AggregatePRValidation*/)
.enablePlugins(NoPublish /*, AggregatePRValidation*/)
.disablePlugins(MimaPlugin)
.aggregate(httpSprayJson, httpXml)

Expand All @@ -311,7 +310,7 @@ lazy val httpSprayJson =

lazy val httpMarshallersJava = project("akka-http-marshallers-java")
.settings(commonSettings)
.enablePlugins(NoPublish, NoScala3 /*FIXME */ /*, AggregatePRValidation*/)
.enablePlugins(NoPublish /*, AggregatePRValidation*/)
.disablePlugins(MimaPlugin)
.aggregate(httpJackson)

Expand Down Expand Up @@ -412,7 +411,6 @@ lazy val httpScalafixTests =

lazy val docs = project("docs")
.enablePlugins(AkkaParadoxPlugin, NoPublish, PublishRsyncPlugin)
.enablePlugins(NoScala3) // FIXME
.disablePlugins(MimaPlugin)
.addAkkaModuleDependency("akka-stream", "provided", AkkaDependency.docs)
.addAkkaModuleDependency("akka-actor-typed", "provided", AkkaDependency.docs)
Expand All @@ -430,12 +428,17 @@ lazy val docs = project("docs")
scalacOptions ++= Seq(
// Make sure we don't accidentally keep documenting deprecated calls
"-Xfatal-warnings",
// In docs adding an unused variable can be helpful, for example
// to show its type
"-Xlint:-unused",
// Does not appear to lead to problems
"-Wconf:msg=The outer reference in this type test cannot be checked at run time:s",
),
scalacOptions ++= (
if (scalaVersion.value.startsWith("3")) Seq.empty
else Seq(
// In docs adding an unused variable can be helpful, for example
// to show its type
"-Xlint:-unused"
)
),
scalacOptions --= Seq(
// Code after ??? can be considered 'dead', but still useful for docs
"-Ywarn-dead-code",
Expand Down Expand Up @@ -501,7 +504,7 @@ lazy val compatibilityTests = Project("akka-http-compatibility-tests", file("akk
)

lazy val billOfMaterials = Project("bill-of-materials", file("akka-http-bill-of-materials"))
.enablePlugins(BillOfMaterialsPlugin, NoScala3 /* FIXME */)
.enablePlugins(BillOfMaterialsPlugin)
.disablePlugins(MimaPlugin)
.settings(
name := "akka-http-bom",
Expand Down
5 changes: 3 additions & 2 deletions docs/src/test/scala/docs/http/scaladsl/Http2ClientApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.typesafe.config.ConfigFactory

import scala.annotation.nowarn
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.Promise

Expand All @@ -33,8 +34,8 @@ object Http2ClientApp extends App {
"""
).withFallback(ConfigFactory.defaultApplication())

implicit val system = ActorSystem("Http2ClientApp", config)
implicit val ec = system.dispatcher
implicit val system: ActorSystem = ActorSystem("Http2ClientApp", config)
implicit val ec: ExecutionContext = system.dispatcher

// #response-future-association
val dispatch = singleRequest(Http().connectionTo("doc.akka.io").http2())
Expand Down
36 changes: 21 additions & 15 deletions docs/src/test/scala/docs/http/scaladsl/HttpClientExampleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp
import akka.stream.scaladsl.{ FileIO, Framing }
import akka.util.ByteString

implicit val system = ActorSystem()
implicit val system: ActorSystem = ActorSystem()

val response: HttpResponse = ???

Expand All @@ -43,15 +43,16 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp

"manual-entity-consume-example-2" in compileOnlySpec {
//#manual-entity-consume-example-2
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.duration._

import akka.actor.ActorSystem
import akka.http.scaladsl.model._
import akka.util.ByteString

implicit val system = ActorSystem()
implicit val dispatcher = system.dispatcher
implicit val system: ActorSystem = ActorSystem()
implicit val dispatcher: ExecutionContext = system.dispatcher

case class ExamplePerson(name: String)
def parse(line: ByteString): ExamplePerson = ???
Expand All @@ -78,6 +79,7 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp

"manual-entity-consume-example-3" in compileOnlySpec {
//#manual-entity-consume-example-3
import scala.concurrent.ExecutionContext
import scala.concurrent.Future

import akka.NotUsed
Expand All @@ -87,13 +89,13 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp
import akka.util.ByteString
import akka.stream.scaladsl.{ Flow, Sink, Source }

implicit val system = ActorSystem()
implicit val dispatcher = system.dispatcher
implicit val system: ActorSystem = ActorSystem()
implicit val dispatcher: ExecutionContext = system.dispatcher

case class ExamplePerson(name: String)

def parse(line: ByteString): Option[ExamplePerson] =
line.utf8String.split(" ").headOption.map(ExamplePerson)
line.utf8String.split(" ").headOption.map(ExamplePerson.apply)

val requests: Source[HttpRequest, NotUsed] = Source
.fromIterator(() =>
Expand Down Expand Up @@ -126,12 +128,14 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp

"manual-entity-discard-example-1" in compileOnlySpec {
//#manual-entity-discard-example-1
import scala.concurrent.ExecutionContext

import akka.actor.ActorSystem
import akka.http.scaladsl.model.HttpMessage.DiscardedEntity
import akka.http.scaladsl.model._

implicit val system = ActorSystem()
implicit val dispatcher = system.dispatcher
implicit val system: ActorSystem = ActorSystem()
implicit val dispatcher: ExecutionContext = system.dispatcher

val response1: HttpResponse = ??? // obtained from an HTTP call (see examples below)

Expand All @@ -141,15 +145,16 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp
//#manual-entity-discard-example-1
}
"manual-entity-discard-example-2" in compileOnlySpec {
import scala.concurrent.ExecutionContext
import scala.concurrent.Future

import akka.Done
import akka.actor.ActorSystem
import akka.http.scaladsl.model._
import akka.stream.scaladsl.Sink

implicit val system = ActorSystem()
implicit val dispatcher = system.dispatcher
implicit val system: ActorSystem = ActorSystem()
implicit val dispatcher: ExecutionContext = system.dispatcher

//#manual-entity-discard-example-2
val response1: HttpResponse = ??? // obtained from an HTTP call (see examples below)
Expand All @@ -171,7 +176,7 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp

import akka.stream.{ OverflowStrategy, QueueOfferResult }

implicit val system = ActorSystem()
implicit val system: ActorSystem = ActorSystem()
import system.dispatcher // to get an implicit ExecutionContext into scope

val QueueSize = 10
Expand Down Expand Up @@ -218,7 +223,7 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp
import akka.http.scaladsl.model.Multipart.FormData
import akka.http.scaladsl.marshalling.Marshal

implicit val system = ActorSystem()
implicit val system: ActorSystem = ActorSystem()
import system.dispatcher // to get an implicit ExecutionContext into scope

case class FileToUpload(name: String, location: Path)
Expand Down Expand Up @@ -303,17 +308,18 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

case class Pet(name: String)
implicit val petFormat = jsonFormat1(Pet)
implicit val petFormat: RootJsonFormat[Pet] = jsonFormat1(Pet.apply)

val pet: Future[Pet] = Unmarshal(response).to[Pet]
//#unmarshal-response-body
}

"single-request-in-actor-example" in compileOnlySpec {
//#single-request-in-actor-example
import akka.actor.{ Actor, ActorLogging }
import akka.actor.{ Actor, ActorLogging, ActorSystem }
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.util.ByteString
Expand All @@ -324,7 +330,7 @@ class HttpClientExampleSpec extends AnyWordSpec with Matchers with CompileOnlySp
import akka.pattern.pipe
import context.dispatcher

implicit val system = context.system
implicit val system: ActorSystem = context.system
val http = Http(system)

override def preStart() = {
Expand Down
25 changes: 14 additions & 11 deletions docs/src/test/scala/docs/http/scaladsl/HttpServerExampleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ package docs.http.scaladsl
import akka.event.LoggingAdapter
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.{ Directive, Route }
import akka.http.scaladsl.server.directives.FormFieldDirectives.FieldSpec
import akka.http.scaladsl.server.util.ConstructFromTuple
import akka.testkit.TestActors

import scala.annotation.nowarn
import docs.CompileOnlySpec

Expand Down Expand Up @@ -262,8 +265,7 @@ class HttpServerExampleSpec extends AnyWordSpec with Matchers
pathEnd {
concat(
put {
// form extraction from multipart or www-url-encoded forms
formFields("email", "total".as[Money]).as(Order) { order =>
formFields("email", "total".as[Money]).as(Order.apply _) { (order: Order) =>
complete {
// complete with serialized Future result
(myDbActor ? Update(order)).mapTo[TransactionResult]
Expand All @@ -285,7 +287,7 @@ class HttpServerExampleSpec extends AnyWordSpec with Matchers
get {
// parameters to case class extraction
parameters("size".as[Int], "color".optional, "dangerous".withDefault("no"))
.as(OrderItem) { orderItem =>
.as(OrderItem.apply _) { (orderItem: OrderItem) =>
// ... route using case class instance created from
// required and optional query parameters
complete("") // #hide
Expand All @@ -309,20 +311,21 @@ class HttpServerExampleSpec extends AnyWordSpec with Matchers
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

implicit val system = ActorSystem()
implicit val system: ActorSystem = ActorSystem()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
implicit val executionContext: ExecutionContext = system.dispatcher

final case class Bid(userId: String, bid: Int)

// these are from spray-json
implicit val bidFormat = jsonFormat2(Bid)
implicit val bidFormat: RootJsonFormat[Bid] = jsonFormat2(Bid.apply)

val route =
path("bid") {
put {
entity(as[Bid]) { bid =>
entity(as[Bid]) { (bid: Bid) =>
// incoming entity is fully consumed and converted into a Bid
complete("The bid was: " + bid)
}
Expand Down Expand Up @@ -371,7 +374,7 @@ class HttpServerExampleSpec extends AnyWordSpec with Matchers
val route =
(put & path("lines")) {
withoutSizeLimit {
extractRequest { r: HttpRequest =>
extractRequest { (r: HttpRequest) =>
val finishedWriting = r.discardEntityBytes().future

// we only want to respond once the incoming data has been handled:
Expand Down Expand Up @@ -422,11 +425,11 @@ class HttpServerExampleSpec extends AnyWordSpec with Matchers
import spray.json.DefaultJsonProtocol._
import spray.json._

implicit val system = ActorSystem()
implicit val system: ActorSystem = ActorSystem()

//#dynamic-routing-example
case class MockDefinition(path: String, requests: Seq[JsValue], responses: Seq[JsValue])
implicit val format = jsonFormat3(MockDefinition)
implicit val format: RootJsonFormat[MockDefinition] = jsonFormat3(MockDefinition.apply)

@volatile var state = Map.empty[String, Map[JsValue, JsValue]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.util.Timeout
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }
Expand Down Expand Up @@ -44,8 +45,8 @@ object HttpServerWithActorInteraction {
}

// these are from spray-json
implicit val bidFormat = jsonFormat2(Auction.Bid)
implicit val bidsFormat = jsonFormat1(Auction.Bids)
implicit val bidFormat: RootJsonFormat[Auction.Bid] = jsonFormat2(Auction.Bid.apply)
implicit val bidsFormat: RootJsonFormat[Auction.Bids] = jsonFormat1(Auction.Bids.apply)

def main(args: Array[String]): Unit = {
implicit val system: ActorSystem[Auction.Message] = ActorSystem(Auction.apply, "auction")
Expand All @@ -69,7 +70,7 @@ object HttpServerWithActorInteraction {
implicit val timeout: Timeout = 5.seconds

// query the actor for the current auction state
val bids: Future[Bids] = (auction ? GetBids).mapTo[Bids]
val bids: Future[Bids] = (auction ? (replyTo => GetBids(replyTo))).mapTo[Bids]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, a bit unfortunate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a typed actor I'd expected being able to do val bids: Future[Bids] = action ? (GetBids(_)) (or ask instead of ? which makes the param list parenthesis's more obvious), without needing the mapTo at all.

complete(bids)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ object HttpServerWithActorsSample {
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.DefaultJsonProtocol
import spray.json.DeserializationException
import spray.json.JsonFormat
import spray.json.JsString
import spray.json.JsValue
import spray.json.RootJsonFormat
Expand All @@ -75,7 +76,7 @@ object HttpServerWithActorsSample {
}
}

implicit val jobFormat = jsonFormat4(Job)
implicit val jobFormat: RootJsonFormat[Job] = jsonFormat4(Job.apply)
}
//#akka-typed-json

Expand Down
Loading