Skip to content

Commit

Permalink
Intermediate step (FetchSchema)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski committed Mar 8, 2024
1 parent 819a501 commit 48e2f0f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ch.epfl.bluebrain.nexus.delta.wiring

import cats.effect.{Clock, IO}
import ch.epfl.bluebrain.nexus.delta.Main.pluginsMaxPriority
import ch.epfl.bluebrain.nexus.delta.config.AppConfig
import ch.epfl.bluebrain.nexus.delta.kernel.utils.{ClasspathResourceLoader, UUIDF}
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.JsonLdApi
Expand All @@ -22,10 +21,11 @@ import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.ApiMappings
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.{ResolverContextResolution, Resolvers}
import ch.epfl.bluebrain.nexus.delta.sdk.resources.FetchResource
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas.{ScopedSchemaDefinition, ScopedSchemaLog}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.{Schema, SchemaEvent}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.{SchemaImports, Schemas, SchemasImpl, ValidateSchema}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas._
import ch.epfl.bluebrain.nexus.delta.sdk.sse.SseEncoder
import ch.epfl.bluebrain.nexus.delta.sourcing.Transactors
import ch.epfl.bluebrain.nexus.delta.sourcing.{ScopedEventLog, Transactors}
import izumi.distage.model.definition.{Id, ModuleDef}

/**
Expand All @@ -40,26 +40,28 @@ object SchemasModule extends ModuleDef {

}

make[ScopedSchemaDefinition].from { (validateSchema: ValidateSchema, clock: Clock[IO]) =>
Schemas.definition(validateSchema, clock)
}

make[ScopedSchemaLog].from { (scopedDefinition: ScopedSchemaDefinition, config: SchemasConfig, xas: Transactors) =>
ScopedEventLog(scopedDefinition, config.eventLog, xas)
}

make[Schemas].from {
(
scopedLog: ScopedSchemaLog,
fetchContext: FetchContext,
schemaImports: SchemaImports,
api: JsonLdApi,
validate: ValidateSchema,
resolverContextResolution: ResolverContextResolution,
config: AppConfig,
xas: Transactors,
clock: Clock[IO],
uuidF: UUIDF
) =>
SchemasImpl(
scopedLog,
fetchContext,
schemaImports,
resolverContextResolution,
validate,
config.schemas,
xas,
clock
resolverContextResolution
)(api, uuidF)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import ch.epfl.bluebrain.nexus.delta.sdk.permissions.Permissions.schemas
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContextDummy
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.ApiMappings
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.ResolverContextResolution
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.{SchemaImports, SchemasConfig, SchemasImpl, ValidateSchema}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.{SchemaImports, Schemas, SchemasConfig, SchemasImpl, ValidateSchema}
import ch.epfl.bluebrain.nexus.delta.sdk.utils.BaseRouteSpec
import ch.epfl.bluebrain.nexus.delta.sourcing.ScopedEventLog
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.{Anonymous, Authenticated, Group, Subject, User}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import ch.epfl.bluebrain.nexus.testkit.ce.IOFromMap
Expand Down Expand Up @@ -78,12 +79,15 @@ class SchemasRoutesSpec extends BaseRouteSpec with IOFromMap with CatsIOValues {

private val config = SchemasConfig(eventLogConfig)

private val schemaDef = Schemas.definition(ValidateSchema.apply, clock)
private val schemaLog = ScopedEventLog(schemaDef, config.eventLog, xas)

private lazy val routes =
Route.seal(
SchemasRoutes(
identities,
aclCheck,
SchemasImpl(fetchContext, schemaImports, resolverContextResolution, ValidateSchema.apply, config, xas, clock),
SchemasImpl(schemaLog, fetchContext, schemaImports, resolverContextResolution),
groupDirectives,
IndexingAction.noop
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ch.epfl.bluebrain.nexus.delta.sdk.schemas

import cats.effect.IO
import cats.implicits.catsSyntaxApplicativeError
import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri
import ch.epfl.bluebrain.nexus.delta.sdk.model.Fetch.FetchF
import ch.epfl.bluebrain.nexus.delta.sdk.model.IdSegmentRef
import ch.epfl.bluebrain.nexus.delta.sdk.model.IdSegmentRef.{Latest, Revision, Tag}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas.ScopedSchemaLog
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.SchemaRejection.{RevisionNotFound, SchemaNotFound, TagNotFound}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.{Schema, SchemaRejection, SchemaState}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{ProjectRef, ResourceRef}

trait FetchSchema {

/** Fetch the referenced resource in the given project */
def fetch(ref: ResourceRef, project: ProjectRef): FetchF[Schema]

def stateOrNotFound(id: IdSegmentRef, iri: Iri, ref: ProjectRef): IO[SchemaState]

}

object FetchSchema {

def apply(log: ScopedSchemaLog): FetchSchema = {

def notFound(iri: Iri, ref: ProjectRef) = SchemaNotFound(iri, ref)

new FetchSchema {
override def fetch(ref: ResourceRef, project: ProjectRef): FetchF[Schema] = {
stateOrNotFound(IdSegmentRef(ref), ref.iri, project)
.attemptNarrow[SchemaRejection]
.map(_.toOption)
.map(_.map(_.toResource))
}

override def stateOrNotFound(id: IdSegmentRef, iri: Iri, ref: ProjectRef): IO[SchemaState] =
id match {
case Latest(_) => log.stateOr(ref, iri, notFound(iri, ref))
case Revision(_, rev) => log.stateOr(ref, iri, rev, notFound(iri, ref), RevisionNotFound)
case Tag(_, tag) => log.stateOr(ref, iri, tag, notFound(iri, ref), TagNotFound(tag))
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ch.epfl.bluebrain.nexus.delta.sourcing.ScopedEntityDefinition.Tagger
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.model._
import ch.epfl.bluebrain.nexus.delta.sourcing.{ScopedEntityDefinition, StateMachine}
import ch.epfl.bluebrain.nexus.delta.sourcing.{ScopedEntityDefinition, ScopedEventLog, StateMachine}
import io.circe.Json

/**
Expand Down Expand Up @@ -375,13 +375,16 @@ object Schemas {
}
}

type ScopedSchemaDefinition = ScopedEntityDefinition[Iri, SchemaState, SchemaCommand, SchemaEvent, SchemaRejection]
type ScopedSchemaLog = ScopedEventLog[Iri, SchemaState, SchemaCommand, SchemaEvent, SchemaRejection]

/**
* Entity definition for [[Schemas]]
*/
def definition(
validate: ValidateSchema,
clock: Clock[IO]
): ScopedEntityDefinition[Iri, SchemaState, SchemaCommand, SchemaEvent, SchemaRejection] =
): ScopedSchemaDefinition =
ScopedEntityDefinition(
entityType,
StateMachine(None, evaluate(validate, clock), next),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch.epfl.bluebrain.nexus.delta.sdk.schemas

import cats.effect.{Clock, IO}
import cats.effect.IO
import cats.syntax.all._
import ch.epfl.bluebrain.nexus.delta.kernel.kamon.KamonMetricComponent
import ch.epfl.bluebrain.nexus.delta.kernel.utils.UUIDF
Expand All @@ -16,7 +16,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.model.IdSegmentRef.{Latest, Revision, T
import ch.epfl.bluebrain.nexus.delta.sdk.model._
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.ResolverContextResolution
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas.{entityType, expandIri}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas.{entityType, expandIri, ScopedSchemaLog}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.SchemasImpl.SchemasLog
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.SchemaCommand._
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.SchemaRejection.{RevisionNotFound, SchemaNotFound, TagNotFound}
Expand Down Expand Up @@ -173,13 +173,10 @@ object SchemasImpl {
* Constructs a [[Schemas]] instance.
*/
final def apply(
scopedLog: ScopedSchemaLog,
fetchContext: FetchContext,
schemaImports: SchemaImports,
contextResolution: ResolverContextResolution,
validate: ValidateSchema,
config: SchemasConfig,
xas: Transactors,
clock: Clock[IO]
contextResolution: ResolverContextResolution
)(implicit
api: JsonLdApi,
uuidF: UUIDF
Expand All @@ -191,7 +188,7 @@ object SchemasImpl {
uuidF
)
new SchemasImpl(
ScopedEventLog(Schemas.definition(validate, clock), config.eventLog, xas),
scopedLog,
fetchContext,
schemaImports,
parser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ class ResourcesImplSpec
)

private val resourceDef = Resources.definition(ValidateResource(resourceResolution), detectChanges, clock)
private val scopedLog = ScopedEventLog(resourceDef, eventLogConfig, xas)
private val resourceLog = ScopedEventLog(resourceDef, eventLogConfig, xas)

private lazy val resources: Resources = ResourcesImpl(
scopedLog,
resourceLog,
fetchContext,
resolverContextResolution
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.{ContextValue, RemoteCon
import ch.epfl.bluebrain.nexus.delta.rdf.shacl.ShaclShapesGraph
import ch.epfl.bluebrain.nexus.delta.sdk.ConfigFixtures
import ch.epfl.bluebrain.nexus.delta.sdk.generators.{ProjectGen, SchemaGen}
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.JsonLdRejection.UnexpectedId
import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.Caller
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.JsonLdRejection.UnexpectedId
import ch.epfl.bluebrain.nexus.delta.sdk.model.{IdSegmentRef, Tags}
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContextDummy
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.ApiMappings
Expand All @@ -20,6 +20,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.ResolverContextResolution
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.Schema
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.SchemaRejection._
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import ch.epfl.bluebrain.nexus.delta.sourcing.ScopedEventLog
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{Identity, Label, ProjectRef}
Expand Down Expand Up @@ -75,8 +76,11 @@ class SchemasImplSuite extends NexusSuite with Doobie.Fixture with ConfigFixture
private val fetchContext = FetchContextDummy(Map(project.ref -> project.context), Set(projectDeprecated.ref))
private val config = SchemasConfig(eventLogConfig)

private val schemaDef = Schemas.definition(ValidateSchema.apply, clock)
private val schemaLog = ScopedEventLog(schemaDef, config.eventLog, xas)

private lazy val schemas: Schemas =
SchemasImpl(fetchContext, schemaImports, resolverContextResolution, ValidateSchema.apply, config, xas, clock)
SchemasImpl(schemaLog, fetchContext, schemaImports, resolverContextResolution)

private def schemaSourceWithId(id: Iri) = {
source deepMerge json"""{"@id": "$id"}"""
Expand Down

0 comments on commit 48e2f0f

Please sign in to comment.