From b23fb41560d87bd9a006fbc307f626e23be7c0ee Mon Sep 17 00:00:00 2001 From: Nicolas Schlecker <24965872+DevNico@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:07:16 +0100 Subject: [PATCH] wip --- build.sbt | 2 -- .../smithy4play/AutoRoutableController.scala | 6 ++--- .../innfactory/smithy4play/AutoRouter.scala | 12 +++++---- .../smithy4play/AutoRoutingMacro.scala | 27 ------------------- .../middleware/MiddlewareBase.scala | 2 +- .../middleware/ValidateAuthMiddleware.scala | 11 ++++---- .../de/innfactory/smithy4play/package.scala | 10 +------ .../app/controller/TestController.scala | 16 +++++------ .../app/controller/models/TestError.scala | 8 +++--- smithy4playTest/test/TestControllerTest.scala | 6 ++--- smithy4playTest/test/models/TestJson.scala | 5 ++-- 11 files changed, 34 insertions(+), 71 deletions(-) delete mode 100644 smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutingMacro.scala diff --git a/build.sbt b/build.sbt index 51ecf4c5..8698d120 100644 --- a/build.sbt +++ b/build.sbt @@ -43,7 +43,6 @@ lazy val smithy4play = project ), Compile / smithy4sOutputDir := (ThisBuild / baseDirectory).value / "smithy4play" / "target" / "scala-3.3.1" / "src_managed" / "main", name := "smithy4play", - scalacOptions += "-Ymacro-annotations", Compile / compile / wartremoverWarnings ++= Warts.unsafe, libraryDependencies ++= Dependencies.list ) @@ -55,7 +54,6 @@ lazy val smithy4playTest = project sharedSettings, scalaVersion := Dependencies.scalaVersion, name := "smithy4playTest", - scalacOptions += "-Ymacro-annotations", Compile / compile / wartremoverWarnings ++= Warts.unsafe, cleanKeepFiles += (ThisBuild / baseDirectory).value / "smithy4playTest" / "app", cleanFiles += (ThisBuild / baseDirectory).value / "smithy4playTest" / "app" / "testDefinitions" / "test", diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutableController.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutableController.scala index fdf6f394..73e14ec5 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutableController.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutableController.scala @@ -9,9 +9,7 @@ import scala.concurrent.ExecutionContext trait AutoRoutableController { - implicit def transformToRouter[Alg[_[_, _, _, _, _]], F[ - _ - ] <: ContextRoute[_]]( + implicit def transformToRouter[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_]]( impl: FunctorAlgebra[Alg, F] )(implicit service: smithy4s.Service[Alg], @@ -21,5 +19,5 @@ trait AutoRoutableController { new SmithyPlayRouter[Alg, F](impl, service).routes(middlewares) val router: Seq[MiddlewareBase] => Routes - } + diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala index 1ef8c6c2..7de42c1d 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRouter.scala @@ -1,18 +1,20 @@ package de.innfactory.smithy4play import com.typesafe.config.Config -import de.innfactory.smithy4play.middleware.{ MiddlewareBase, MiddlewareRegistryBase, ValidateAuthMiddleware } -import io.github.classgraph.{ ClassGraph, ScanResult } +import de.innfactory.smithy4play.middleware.{MiddlewareBase, MiddlewareRegistryBase, ValidateAuthMiddleware} +import io.github.classgraph.{ClassGraph, ScanResult} import play.api.Application import play.api.mvc.ControllerComponents import play.api.routing.Router.Routes -import java.util.Optional -import javax.inject.{ Inject, Provider, Singleton } +import javax.inject.{Inject, Singleton} +import scala.annotation.{Annotation, StaticAnnotation} import scala.concurrent.ExecutionContext import scala.jdk.CollectionConverters.CollectionHasAsScala import scala.util.Try + + @Singleton class AutoRouter @Inject( ) (validateAuthMiddleware: ValidateAuthMiddleware)(implicit @@ -42,4 +44,4 @@ class AutoRouter @Inject( case c: AutoRoutableController => c.router(middlewares) } -} +} \ No newline at end of file diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutingMacro.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutingMacro.scala deleted file mode 100644 index 541bf294..00000000 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/AutoRoutingMacro.scala +++ /dev/null @@ -1,27 +0,0 @@ -package de.innfactory.smithy4play - -import scala.reflect.macros.whitebox - -object AutoRoutingMacro { - def impl(c: whitebox.Context)(annottees: c.Tree*): c.Tree = { - import c.universe._ - annottees match { - case List( - q"$mods class $className $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parentss { $self => ..$body }" - ) => - q"""$mods class $className $ctorMods(...$paramss) - extends { ..$earlydefns } - with ..$parentss - with de.innfactory.smithy4play.AutoRoutableController - { $self => - override val router: Seq[de.innfactory.smithy4play.middleware.MiddlewareBase] => play.api.routing.Router.Routes = this - ..$body } - """ - case _ => - c.abort( - c.enclosingPosition, - "RegisterClass: An AutoRouter Annotation on this type of Class is not supported." - ) - } - } -} diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/MiddlewareBase.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/MiddlewareBase.scala index 8aadf627..b10a5398 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/MiddlewareBase.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/MiddlewareBase.scala @@ -1,7 +1,7 @@ package de.innfactory.smithy4play.middleware import cats.data.Kleisli -import de.innfactory.smithy4play.{RouteResult, RoutingContext} +import de.innfactory.smithy4play.{EndpointResult, RouteResult, RoutingContext} import play.api.Logger trait MiddlewareBase { diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/ValidateAuthMiddleware.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/ValidateAuthMiddleware.scala index 5edd1ec9..88a776e9 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/ValidateAuthMiddleware.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/middleware/ValidateAuthMiddleware.scala @@ -2,12 +2,11 @@ package de.innfactory.smithy4play.middleware import cats.data.EitherT import de.innfactory.smithy4play -import de.innfactory.smithy4play.{ EndpointResult, RouteResult, RoutingContext, Smithy4PlayError } -import smithy.api.{ Auth, HttpBearerAuth } +import de.innfactory.smithy4play.{EndpointResult, RouteResult, RoutingContext, Smithy4PlayError} +import smithy.api.{Auth, HttpBearerAuth} -import javax.inject.{ Inject, Singleton } -import scala.concurrent.{ ExecutionContext, Future } -import smithy.api.AuthTraitReference.asBijection +import javax.inject.{Inject, Singleton} +import scala.concurrent.{ExecutionContext, Future} @Singleton class ValidateAuthMiddleware @Inject() (implicit executionContext: ExecutionContext @@ -15,7 +14,7 @@ class ValidateAuthMiddleware @Inject() (implicit override protected def skipMiddleware(r: RoutingContext): Boolean = { val serviceAuthHints = - r.serviceHints.get(HttpBearerAuth.tagInstance).flatMap(x => HttpBearerAuth.hints.get(Auth.tag)) + r.serviceHints.get(HttpBearerAuth.tagInstance).map(_ => Auth(Set(smithy.api.AuthTraitReference("smithy.api#httpBearerAuth")))) for { authSet <- r.endpointHints.get(Auth.tag) orElse serviceAuthHints _ <- authSet.value.find(_.value == HttpBearerAuth.id.show) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala index 7b4b84fb..c2846485 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala @@ -1,14 +1,13 @@ package de.innfactory.smithy4play import cats.data.{EitherT, Kleisli} -import de.innfactory.smithy4play.client.SmithyPlayClientEndpointErrorResponse +import de.innfactory.smithy4play.client.{SmithyPlayClientEndpointErrorResponse, SmithyPlayClientEndpointResponse} import org.slf4j import play.api.Logger import play.api.libs.json.{JsValue, Json, OFormat} import play.api.mvc.{Headers, RequestHeader} import smithy4s.http.{CaseInsensitive, HttpEndpoint} -import scala.annotation.{StaticAnnotation, compileTimeOnly} import scala.concurrent.Future import scala.language.experimental.macros @@ -72,13 +71,6 @@ private[smithy4play] def matchRequestPath( ): Option[Map[String, String]] = ep.matches(x.path.replaceFirst("/", "").split("/").filter(_.nonEmpty)) -@compileTimeOnly( - "Macro failed to expand. \"Add: scalacOptions += \"-Ymacro-annotations\"\" to project settings" -) -class AutoRouting extends StaticAnnotation { - def macroTransform(annottees: Any*): Any = macro AutoRoutingMacro.impl -} - private[smithy4play] trait Showable { this: Product => override def toString: String = this.show diff --git a/smithy4playTest/app/controller/TestController.scala b/smithy4playTest/app/controller/TestController.scala index ee60b420..c409e496 100644 --- a/smithy4playTest/app/controller/TestController.scala +++ b/smithy4playTest/app/controller/TestController.scala @@ -1,21 +1,21 @@ package controller -import cats.data.{ EitherT, Kleisli } +import cats.data.{EitherT, Kleisli} import controller.models.TestError -import de.innfactory.smithy4play.{ AutoRouting, ContextRoute, ContextRouteError } +import de.innfactory.smithy4play.{AutoRoutableController, ContextRoute, ContextRouteError} import play.api.mvc.ControllerComponents import smithy4s.ByteArray -import testDefinitions.test._ +import testDefinitions.test.* -import javax.inject.{ Inject, Singleton } -import scala.concurrent.{ ExecutionContext, Future } +import javax.inject.{Inject, Singleton} +import scala.concurrent.{ExecutionContext, Future} @Singleton -@AutoRouting class TestController @Inject() (implicit cc: ControllerComponents, - executionContext: ExecutionContext -) extends TestControllerService[ContextRoute] { + executionContext: ExecutionContext, +) extends TestControllerService[ContextRoute] with AutoRoutableController { + override val router = this override def test(): ContextRoute[SimpleTestResponse] = Kleisli { rc => rc.attributes.get("Not") match { diff --git a/smithy4playTest/app/controller/models/TestError.scala b/smithy4playTest/app/controller/models/TestError.scala index d4c454ec..614f6a3e 100644 --- a/smithy4playTest/app/controller/models/TestError.scala +++ b/smithy4playTest/app/controller/models/TestError.scala @@ -1,13 +1,13 @@ package controller.models -import de.innfactory.smithy4play.{ ContextRouteError, Status } -import play.api.libs.json.{ JsValue, Json } +import de.innfactory.smithy4play.{ContextRouteError, Status} +import play.api.libs.json.{Format, JsValue, Json} case class TestError( message: String, status: Status = Status(Map.empty, 500) ) extends ContextRouteError { - override def toJson: JsValue = Json.toJson(this)(TestError.format) + override def toJson: JsValue = Json.toJson(this) override def addHeaders(headers: Map[String, String]): TestError = this.copy( status = status.copy( headers = status.headers ++ headers @@ -16,5 +16,5 @@ case class TestError( } object TestError { - implicit val format = Json.format[TestError] + given Format[TestError] = Json.format[TestError] } diff --git a/smithy4playTest/test/TestControllerTest.scala b/smithy4playTest/test/TestControllerTest.scala index 4a745315..3dde0fad 100644 --- a/smithy4playTest/test/TestControllerTest.scala +++ b/smithy4playTest/test/TestControllerTest.scala @@ -9,7 +9,7 @@ import org.scalatestplus.play.{ BaseOneAppPerSuite, FakeApplicationFactory, Play import play.api.Application import play.api.Play.materializer import play.api.inject.guice.GuiceApplicationBuilder -import play.api.libs.json.{ Json, OWrites } +import play.api.libs.json.{ Json, Writes } import play.api.mvc.{ AnyContentAsEmpty, Result } import play.api.test.FakeRequest import play.api.test.Helpers._ @@ -99,7 +99,7 @@ class TestControllerTest extends PlaySpec with BaseOneAppPerSuite with FakeAppli val testQuery = "thisIsATestQuery" val testHeader = "thisIsATestHeader" val body = TestRequestBody("thisIsARequestBody") - implicit val format: OWrites[TestRequestBody] = Json.writes[TestRequestBody] + given Writes[TestRequestBody] = Json.writes[TestRequestBody] val future: Future[Result] = route( app, @@ -113,7 +113,7 @@ class TestControllerTest extends PlaySpec with BaseOneAppPerSuite with FakeAppli "route to Query Endpoint but should return error because query is not set" in { val testQuery = "thisIsATestQuery" - implicit val format: OWrites[TestRequestBody] = Json.writes[TestRequestBody] + given Writes[TestRequestBody] = Json.writes[TestRequestBody] val future: Future[Result] = route( app, diff --git a/smithy4playTest/test/models/TestJson.scala b/smithy4playTest/test/models/TestJson.scala index 094b46bd..9c22a7de 100644 --- a/smithy4playTest/test/models/TestJson.scala +++ b/smithy4playTest/test/models/TestJson.scala @@ -1,9 +1,10 @@ package models -import play.api.libs.json.Json +import play.api.libs.json.{Json, Reads, Writes} case class TestJson(message: Option[String]) object TestJson { - implicit val format = Json.format[TestJson] + given Writes[TestJson] = Json.format[TestJson] + given Reads[TestJson] = Json.reads[TestJson] }