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

Refactor ResourceRef expansion #4450

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.model.IdSegment
import ch.epfl.bluebrain.nexus.delta.sdk.model.search.SortList
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.{ApiMappings, ProjectBase}
import ch.epfl.bluebrain.nexus.delta.sdk.resources.Resources
import ch.epfl.bluebrain.nexus.delta.sourcing.Scope
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{Label, ProjectRef, ResourceRef}

Expand Down Expand Up @@ -62,7 +63,7 @@ object DefaultSearchRequest {
.onRead(ref)
.toCatsIO
.flatMap { context =>
expandResourceRef(schema, context.apiMappings, context.base)
IO.fromEither(expandResourceRef(schema, context.apiMappings, context.base))
}
.map { schemaRef =>
ProjectSearch(ref, params.withSchema(schemaRef), pagination, sort: SortList)
Expand Down Expand Up @@ -90,7 +91,7 @@ object DefaultSearchRequest {
object OrgSearch {
def apply(label: Label, params: ResourcesSearchParams, pagination: Pagination, sort: SortList, schema: IdSegment)(
fetchContext: FetchContext[ElasticSearchQueryError]
): IO[OrgSearch] =
): Either[Throwable, OrgSearch] =
expandResourceRef(schema, fetchContext).map { resourceRef =>
OrgSearch(label, params.withSchema(resourceRef), pagination, sort)
}
Expand All @@ -111,7 +112,7 @@ object DefaultSearchRequest {
object RootSearch {
def apply(params: ResourcesSearchParams, pagination: Pagination, sort: SortList, schema: IdSegment)(
fetchContext: FetchContext[ElasticSearchQueryError]
): IO[RootSearch] =
): Either[Throwable, RootSearch] =
expandResourceRef(schema, fetchContext).map { resourceRef =>
RootSearch(params.withSchema(resourceRef), pagination, sort)
}
Expand All @@ -124,16 +125,13 @@ object DefaultSearchRequest {
private def expandResourceRef(
segment: IdSegment,
fetchContext: FetchContext[ElasticSearchQueryError]
): IO[ResourceRef] =
): Either[Throwable, ResourceRef] =
expandResourceRef(segment, fetchContext.defaultApiMappings, ProjectBase(iri""))

private def expandResourceRef(
segment: IdSegment,
mappings: ApiMappings,
base: ProjectBase
): IO[ResourceRef] =
IO.fromOption(segment.toIri(mappings, base).map(ResourceRef(_)))(
InvalidResourceId(segment.asString)
)
): Either[Throwable, ResourceRef] = Resources.expandResourceRef(segment, mappings, base, InvalidResourceId)

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ class ElasticSearchQueryRoutes(
pathEndOrSingleSlash {
val request = DefaultSearchRequest.RootSearch(params, page, sort, resourceSchema)(fetchContext)
concat(
aggregate(request),
list(request)
aggregate(IO.fromEither(request)),
list(IO.fromEither(request))
)
},
// List all resources of type resourceSegment inside an organization
(label & pathEndOrSingleSlash) { org =>
val request = DefaultSearchRequest.OrgSearch(org, params, page, sort, resourceSchema)(fetchContext)
concat(
aggregate(request),
list(request)
aggregate(IO.fromEither(request)),
list(IO.fromEither(request))
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.Caller
import ch.epfl.bluebrain.nexus.delta.sdk.instances._
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.ExpandIri
import ch.epfl.bluebrain.nexus.delta.sdk.model._
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.{ApiMappings, ProjectContext}
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.{ApiMappings, ProjectBase, ProjectContext}
import ch.epfl.bluebrain.nexus.delta.sdk.resources.model.ResourceCommand._
import ch.epfl.bluebrain.nexus.delta.sdk.resources.model.ResourceEvent._
import ch.epfl.bluebrain.nexus.delta.sdk.resources.model.ResourceRejection.{IncorrectRev, InvalidResourceId, ResourceAlreadyExists, ResourceFetchRejection, ResourceIsDeprecated, ResourceNotFound, RevisionNotFound, TagNotFound, UnexpectedResourceSchema}
Expand Down Expand Up @@ -263,25 +263,25 @@ object Resources {
/**
* Expands the segment to a [[ResourceRef]]
*/
def expandResourceRef(segment: IdSegment, context: ProjectContext): IO[ResourceRef] =
expandResourceRef(segment, context, InvalidResourceId)

def expandResourceRef(segment: IdSegment, context: ProjectContext, notFound: String => Throwable): IO[ResourceRef] =
IO.fromOption(segment.toIri(context.apiMappings, context.base).map(ResourceRef(_)))(
notFound(segment.asString)
)
def expandResourceRef(segment: IdSegment, context: ProjectContext): Either[Throwable, ResourceRef] =
expandResourceRef(segment, context.apiMappings, context.base, InvalidResourceId)

/**
* Expands the segment to a [[ResourceRef]] if defined
*/
def expandResourceRef(
segmentOpt: Option[IdSegment],
context: ProjectContext
): IO[Option[ResourceRef]] =
segmentOpt match {
case None => IO.none
case Some(schema) => expandResourceRef(schema, context).map(Some.apply)
}
): Either[Throwable, Option[ResourceRef]] =
segmentOpt.flatTraverse(expandResourceRef(_, context).map(_.some))

def expandResourceRef(
segment: IdSegment,
mappings: ApiMappings,
base: ProjectBase,
notFound: String => Throwable
): Either[Throwable, ResourceRef] =
segment.toIri(mappings, base).map(ResourceRef(_)).toRight(notFound(segment.asString))

private[delta] def next(state: Option[ResourceState], event: ResourceEvent): Option[ResourceState] = {
// format: off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class ResourcesImpl private (
)(implicit caller: Caller): IO[DataResource] = {
for {
projectContext <- fetchContext.onCreate(projectRef).toCatsIO
schemeRef <- expandResourceRef(schema, projectContext)
schemeRef <- IO.fromEither(expandResourceRef(schema, projectContext))
jsonld <- sourceParser(projectRef, projectContext, source).toCatsIO
res <- eval(CreateResource(jsonld.iri, projectRef, schemeRef, source, jsonld, caller, tag))
} yield res
Expand All @@ -59,7 +59,7 @@ final class ResourcesImpl private (
)(implicit caller: Caller): IO[DataResource] = {
for {
(iri, projectContext) <- expandWithContext(fetchContext.onCreate, projectRef, id)
schemeRef <- expandResourceRef(schema, projectContext)
schemeRef <- IO.fromEither(expandResourceRef(schema, projectContext))
jsonld <- sourceParser(projectRef, projectContext, iri, source).toCatsIO
res <- eval(CreateResource(iri, projectRef, schemeRef, source, jsonld, caller, tag))
} yield res
Expand All @@ -75,7 +75,7 @@ final class ResourcesImpl private (
)(implicit caller: Caller): IO[DataResource] = {
for {
(iri, projectContext) <- expandWithContext(fetchContext.onModify, projectRef, id)
schemeRefOpt <- expandResourceRef(schemaOpt, projectContext)
schemeRefOpt <- IO.fromEither(expandResourceRef(schemaOpt, projectContext))
jsonld <- sourceParser(projectRef, projectContext, iri, source).toCatsIO
res <- eval(UpdateResource(iri, projectRef, schemeRefOpt, source, jsonld, rev, caller, tag))
} yield res
Expand All @@ -88,7 +88,7 @@ final class ResourcesImpl private (
)(implicit caller: Caller): IO[DataResource] = {
for {
(iri, projectContext) <- expandWithContext(fetchContext.onModify, projectRef, id)
schemaRef <- expandResourceRef(schema, projectContext)
schemaRef <- IO.fromEither(expandResourceRef(schema, projectContext))
resource <- log.stateOr(projectRef, iri, ResourceNotFound(iri, projectRef)).toCatsIO
res <- if (schemaRef.iri == resource.schema.iri) fetch(id, projectRef, schema.some)
else eval(UpdateResourceSchema(iri, projectRef, schemaRef, resource.expanded, resource.rev, caller))
Expand All @@ -102,7 +102,7 @@ final class ResourcesImpl private (
)(implicit caller: Caller): IO[DataResource] = {
for {
(iri, projectContext) <- expandWithContext(fetchContext.onModify, projectRef, id)
schemaRefOpt <- expandResourceRef(schemaOpt, projectContext)
schemaRefOpt <- IO.fromEither(expandResourceRef(schemaOpt, projectContext))
resource <- log.stateOr(projectRef, iri, ResourceNotFound(iri, projectRef)).toCatsIO
jsonld <- sourceParser(projectRef, projectContext, iri, resource.source).toCatsIO
res <- eval(RefreshResource(iri, projectRef, schemaRefOpt, jsonld, resource.rev, caller))
Expand All @@ -119,7 +119,7 @@ final class ResourcesImpl private (
)(implicit caller: Subject): IO[DataResource] =
(for {
(iri, projectContext) <- expandWithContext(fetchContext.onModify, projectRef, id)
schemeRefOpt <- expandResourceRef(schemaOpt, projectContext)
schemeRefOpt <- IO.fromEither(expandResourceRef(schemaOpt, projectContext))
res <- eval(TagResource(iri, projectRef, schemeRefOpt, tagRev, tag, rev, caller))
} yield res).span("tagResource")

Expand All @@ -132,7 +132,7 @@ final class ResourcesImpl private (
)(implicit caller: Subject): IO[DataResource] =
(for {
(iri, projectContext) <- expandWithContext(fetchContext.onModify, projectRef, id)
schemeRefOpt <- expandResourceRef(schemaOpt, projectContext)
schemeRefOpt <- IO.fromEither(expandResourceRef(schemaOpt, projectContext))
res <- eval(DeleteResourceTag(iri, projectRef, schemeRefOpt, tag, rev, caller))
} yield res).span("deleteResourceTag")

Expand All @@ -144,7 +144,7 @@ final class ResourcesImpl private (
)(implicit caller: Subject): IO[DataResource] =
(for {
(iri, projectContext) <- expandWithContext(fetchContext.onModify, projectRef, id)
schemeRefOpt <- expandResourceRef(schemaOpt, projectContext)
schemeRefOpt <- IO.fromEither(expandResourceRef(schemaOpt, projectContext))
res <- eval(DeprecateResource(iri, projectRef, schemeRefOpt, rev, caller))
} yield res).span("deprecateResource")

Expand All @@ -155,7 +155,7 @@ final class ResourcesImpl private (
): IO[ResourceState] = {
for {
(iri, pc) <- expandWithContext(fetchContext.onRead, projectRef, id.value)
schemaRefOpt <- expandResourceRef(schemaOpt, pc)
schemaRefOpt <- IO.fromEither(expandResourceRef(schemaOpt, pc))
state <- stateOrNotFound(id, iri, projectRef)
_ <- IO.raiseWhen(schemaRefOpt.exists(_.iri != state.schema.iri))(notFound(iri, projectRef))
} yield state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object ResourcesTrial {
): IO[ResourceGenerationResult] = {
for {
projectContext <- fetchContext.onRead(project).toCatsIO
schemaRef <- Resources.expandResourceRef(schema, projectContext)
schemaRef <- IO.fromEither(Resources.expandResourceRef(schema, projectContext))
jsonld <- sourceParser(project, projectContext, source.value).toCatsIO
validation <- validateResource(jsonld.iri, jsonld.expanded, schemaRef, project, caller)
result <- toResourceF(project, jsonld, source, validation)
Expand All @@ -112,7 +112,7 @@ object ResourcesTrial {
): IO[ValidationResult] = {
for {
projectContext <- fetchContext.onRead(project).toCatsIO
schemaRefOpt <- expandResourceRef(schemaOpt, projectContext)
schemaRefOpt <- IO.fromEither(expandResourceRef(schemaOpt, projectContext))
resource <- fetchResource(id, project)
report <- validateResource(
resource.id,
Expand Down