From 5d709350f8ce4a98d634d348e59f1e95ed108e66 Mon Sep 17 00:00:00 2001 From: Daniel Bell Date: Tue, 17 Oct 2023 10:13:22 +0200 Subject: [PATCH] Migrate ResourcesTrialRoutes to Cats Effect --- .../delta/routes/ResourcesTrialRoutes.scala | 32 ++++++++++++------- .../delta/wiring/ResourcesTrialModule.scala | 3 -- .../routes/ResourcesTrialRoutesSpec.scala | 11 ++++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutes.scala b/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutes.scala index bfd6e47f16..82666088f4 100644 --- a/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutes.scala +++ b/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutes.scala @@ -2,6 +2,7 @@ package ch.epfl.bluebrain.nexus.delta.routes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route +import cats.effect.IO import cats.syntax.all._ import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._ import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.schemas @@ -11,8 +12,8 @@ import ch.epfl.bluebrain.nexus.delta.routes.ResourcesTrialRoutes.SchemaInput._ import ch.epfl.bluebrain.nexus.delta.routes.ResourcesTrialRoutes.{GenerateSchema, GenerationInput} import ch.epfl.bluebrain.nexus.delta.sdk.SchemaResource import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck +import ch.epfl.bluebrain.nexus.delta.sdk.ce.DeltaDirectives._ import ch.epfl.bluebrain.nexus.delta.sdk.circe.CirceUnmarshalling -import ch.epfl.bluebrain.nexus.delta.sdk.directives.DeltaDirectives._ import ch.epfl.bluebrain.nexus.delta.sdk.directives.{AuthDirectives, DeltaSchemeDirectives} import ch.epfl.bluebrain.nexus.delta.sdk.identities.Identities import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.Caller @@ -29,8 +30,6 @@ import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef import io.circe.generic.extras.Configuration import io.circe.generic.extras.semiauto.deriveConfiguredDecoder import io.circe.{Decoder, Json} -import monix.bio.IO -import monix.execution.Scheduler import scala.annotation.nowarn @@ -45,7 +44,6 @@ final class ResourcesTrialRoutes( schemeDirectives: DeltaSchemeDirectives )(implicit baseUri: BaseUri, - s: Scheduler, cr: RemoteContextResolution, ordering: JsonKeyOrdering, decodingOption: DecodingOption @@ -67,7 +65,10 @@ final class ResourcesTrialRoutes( authorizeFor(project, Write).apply { val schemaOpt = underscoreToOption(schema) emit( - resourcesTrial.validate(id, project, schemaOpt).leftWiden[ResourceRejection] + resourcesTrial + .validate(id, project, schemaOpt) + .toCatsIO + .attemptNarrow[ResourceRejection] ) } } @@ -92,19 +93,29 @@ final class ResourcesTrialRoutes( private def generate(project: ProjectRef, input: GenerationInput)(implicit caller: Caller) = input.schema match { case ExistingSchema(schemaId) => - emit(resourcesTrial.generate(project, schemaId, input.resource).flatMap(_.asJson)) + emit( + resourcesTrial + .generate(project, schemaId, input.resource) + .toCatsIO + .flatMap(_.asJson) + ) case NewSchema(schemaSource) => emit( - generateSchema(project, schemaSource, caller).flatMap { schema => - resourcesTrial.generate(project, schema, input.resource).flatMap(_.asJson) - } + generateSchema(project, schemaSource, caller) + .flatMap { schema => + resourcesTrial + .generate(project, schema, input.resource) + .toCatsIO + .flatMap(_.asJson.toCatsIO) + } + .attemptNarrow[SchemaRejection] ) } } object ResourcesTrialRoutes { - type GenerateSchema = (ProjectRef, Json, Caller) => IO[SchemaRejection, SchemaResource] + type GenerateSchema = (ProjectRef, Json, Caller) => IO[SchemaResource] sealed private[routes] trait SchemaInput extends Product @@ -146,7 +157,6 @@ object ResourcesTrialRoutes { schemeDirectives: DeltaSchemeDirectives )(implicit baseUri: BaseUri, - s: Scheduler, cr: RemoteContextResolution, ordering: JsonKeyOrdering, decodingOption: DecodingOption diff --git a/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/wiring/ResourcesTrialModule.scala b/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/wiring/ResourcesTrialModule.scala index 7135e8d258..7194f25fb7 100644 --- a/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/wiring/ResourcesTrialModule.scala +++ b/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/wiring/ResourcesTrialModule.scala @@ -21,7 +21,6 @@ import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas import distage.ModuleDef import izumi.distage.model.definition.Id import monix.bio.UIO -import monix.execution.Scheduler /** * Resources trial wiring @@ -54,7 +53,6 @@ object ResourcesTrialModule extends ModuleDef { resourcesTrial: ResourcesTrial, schemeDirectives: DeltaSchemeDirectives, baseUri: BaseUri, - s: Scheduler, cr: RemoteContextResolution @Id("aggregate"), ordering: JsonKeyOrdering, config: ResourcesConfig @@ -67,7 +65,6 @@ object ResourcesTrialModule extends ModuleDef { schemeDirectives )( baseUri, - s, cr, ordering, config.decodingOption diff --git a/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutesSpec.scala b/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutesSpec.scala index 7661a1279f..3f89a559f2 100644 --- a/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutesSpec.scala +++ b/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutesSpec.scala @@ -3,6 +3,7 @@ package ch.epfl.bluebrain.nexus.delta.routes import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.server.Route +import cats.effect.IO import ch.epfl.bluebrain.nexus.delta.kernel.utils.UrlUtils import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.{contexts, nxv, schemas} import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.JsonLdContext.keywords @@ -28,7 +29,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.utils.BaseRouteSpec import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.{Anonymous, Authenticated, Group} import ch.epfl.bluebrain.nexus.delta.sourcing.model.{ProjectRef, ResourceRef} import ch.epfl.bluebrain.nexus.delta.sourcing.model.ResourceRef.Revision -import monix.bio.{IO, UIO} +import monix.bio.{IO => BIO, UIO} import java.time.Instant @@ -94,7 +95,7 @@ class ResourcesTrialRoutesSpec extends BaseRouteSpec with ResourceInstanceFixtur override def validate(id: IdSegmentRef, project: ProjectRef, schemaOpt: Option[IdSegment])(implicit caller: Caller - ): IO[ResourceRejection, ValidationResult] = + ): BIO[ResourceRejection, ValidationResult] = (id.value, schemaOpt) match { // Returns a validated result for myId when no schema is provided case (StringSegment("myId") | IriSegment(`myId`), None) => @@ -102,13 +103,13 @@ class ResourcesTrialRoutesSpec extends BaseRouteSpec with ResourceInstanceFixtur // Returns no validation result for myId for `schemas.resources` case (StringSegment("myId") | IriSegment(`myId`), Some(IriSegment(schemas.resources))) => UIO.pure(NoValidation(projectRef)) - case (IriSegment(iri), None) => IO.raiseError(ResourceNotFound(iri, project, None)) - case _ => IO.terminate(new IllegalStateException("Should not happen !")) + case (IriSegment(iri), None) => BIO.raiseError(ResourceNotFound(iri, project, None)) + case _ => BIO.terminate(new IllegalStateException("Should not happen !")) } } private val generateSchema: GenerateSchema = { - case (_, `schemaSource`, _) => UIO.pure(SchemaGen.resourceFor(schema1)) + case (_, `schemaSource`, _) => IO.pure(SchemaGen.resourceFor(schema1)) case _ => IO.raiseError(SchemaShaclEngineRejection(nxv + "invalid", "Invalid schema")) }