-
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.
- Loading branch information
Simon Dumas
committed
Feb 20, 2024
1 parent
02471bf
commit 781e7f3
Showing
8 changed files
with
119 additions
and
59 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
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
35 changes: 7 additions & 28 deletions
35
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 |
---|---|---|
@@ -1,42 +1,21 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.marshalling | ||
|
||
import cats.effect.IO | ||
import cats.syntax.all._ | ||
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.rdf.Vocabulary.contexts | ||
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, ResourceF} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._ | ||
import io.circe.Json | ||
import io.circe.syntax.EncoderOps | ||
|
||
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[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) | ||
def apply(resourceF: ResourceF[_], source: Json)(implicit baseUri: BaseUri): Json = { | ||
val sourceWithoutMetadata = source.removeMetadataKeys | ||
val metadataJson = resourceF.void.asJson | ||
metadataJson.deepMerge(sourceWithoutMetadata).addContext(contexts.metadata) | ||
} | ||
|
||
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