-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add annotated source as a format for multi-fetch and archives (#4246)
Co-authored-by: Simon Dumas <simon.dumas@epfl.ch>
- Loading branch information
Showing
13 changed files
with
243 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
delta/app/src/test/resources/multi-fetch/annotated-source-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"format": "annotated-source", | ||
"resources": [ | ||
{ | ||
"@id": "https://bluebrain.github.io/nexus/vocabulary/success", | ||
"project": "org/proj1", | ||
"value": { | ||
"@context": "https://bluebrain.github.io/nexus/contexts/metadata.json", | ||
"@id": "https://bluebrain.github.io/nexus/vocabulary/success", | ||
"@type": "https://bluebrain.github.io/nexus/vocabulary/Custom", | ||
"bool": false, | ||
"name": "Alex", | ||
"number": 24, | ||
"_constrainedBy": "https://bluebrain.github.io/nexus/schemas/unconstrained.json", | ||
"_createdAt": "1970-01-01T00:00:00Z", | ||
"_createdBy": "http://localhost/v1/anonymous", | ||
"_deprecated": false, | ||
"_incoming": "http://localhost/v1/resources/org/proj1/_/https:%2F%2Fbluebrain.github.io%2Fnexus%2Fvocabulary%2Fsuccess/incoming", | ||
"_outgoing": "http://localhost/v1/resources/org/proj1/_/https:%2F%2Fbluebrain.github.io%2Fnexus%2Fvocabulary%2Fsuccess/outgoing", | ||
"_project": "http://localhost/v1/projects/org/proj1", | ||
"_rev": 1, | ||
"_schemaProject": "http://localhost/v1/projects/org/proj1", | ||
"_self": "http://localhost/v1/resources/org/proj1/_/https:%2F%2Fbluebrain.github.io%2Fnexus%2Fvocabulary%2Fsuccess", | ||
"_updatedAt": "1970-01-01T00:00:00Z", | ||
"_updatedBy": "http://localhost/v1/anonymous" | ||
} | ||
}, | ||
{ | ||
"@id": "https://bluebrain.github.io/nexus/vocabulary/not-found", | ||
"error": { | ||
"@type": "NotFound", | ||
"reason": "The resource 'https://bluebrain.github.io/nexus/vocabulary/not-found' was not found in project 'org/proj1'." | ||
}, | ||
"project": "org/proj1" | ||
}, | ||
{ | ||
"@id": "https://bluebrain.github.io/nexus/vocabulary/unauthorized", | ||
"error": { | ||
"@type": "AuthorizationFailed", | ||
"reason": "The supplied authentication is not authorized to access this resource." | ||
}, | ||
"project": "org/proj2" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/marshalling/AnnotatedSource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.marshalling | ||
|
||
import cats.syntax.all._ | ||
import ch.epfl.bluebrain.nexus.delta.rdf.RdfError | ||
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.{JsonLdApi, JsonLdJavaApi} | ||
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.RemoteContextResolution | ||
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder | ||
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, ResourceF} | ||
import io.circe.Json | ||
import monix.bio.IO | ||
|
||
object AnnotatedSource { | ||
|
||
implicit private val api: JsonLdApi = JsonLdJavaApi.lenient | ||
|
||
/** | ||
* Merges the source with the metadata of [[ResourceF]] | ||
*/ | ||
def apply(resourceF: ResourceF[_], source: Json)(implicit | ||
baseUri: BaseUri, | ||
cr: RemoteContextResolution | ||
): IO[RdfError, Json] = | ||
metadataJson(resourceF) | ||
.map(mergeOriginalPayloadWithMetadata(source, _)) | ||
|
||
private def metadataJson(resource: ResourceF[_])(implicit baseUri: BaseUri, cr: RemoteContextResolution) = { | ||
implicit val resourceFJsonLdEncoder: JsonLdEncoder[ResourceF[Unit]] = ResourceF.defaultResourceFAJsonLdEncoder | ||
resourceFJsonLdEncoder | ||
.compact(resource.void) | ||
.map(_.json) | ||
} | ||
|
||
private def mergeOriginalPayloadWithMetadata(payload: Json, metadata: Json): Json = { | ||
getId(payload) | ||
.foldLeft(payload.deepMerge(metadata))(setId) | ||
} | ||
|
||
private def getId(payload: Json): Option[String] = payload.hcursor.get[String]("@id").toOption | ||
|
||
private def setId(payload: Json, id: String): Json = | ||
payload.hcursor.downField("@id").set(Json.fromString(id)).top.getOrElse(payload) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.