Skip to content

Commit

Permalink
Drop tag in serialized file if null
Browse files Browse the repository at this point in the history
  • Loading branch information
dantb committed Oct 16, 2023
1 parent aa808b4 commit 1b48c05
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.JsonLdContext.keywords
import ch.epfl.bluebrain.nexus.delta.sdk.implicits._
import ch.epfl.bluebrain.nexus.delta.plugins.storage.instances._
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.StoragesConfig.StorageTypeConfig
import ch.epfl.bluebrain.nexus.delta.sdk.circe.JsonObjOps
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.IriEncoder
import ch.epfl.bluebrain.nexus.delta.sdk.model.BaseUri
import ch.epfl.bluebrain.nexus.delta.sdk.model.metrics.EventMetric._
Expand All @@ -23,7 +24,7 @@ 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.{EntityType, Label, ProjectRef, ResourceRef}
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.{deriveConfiguredCodec, deriveConfiguredEncoder}
import io.circe.generic.extras.semiauto.{deriveConfiguredCodec, deriveConfiguredDecoder, deriveConfiguredEncoder}
import io.circe.generic.semiauto.deriveEncoder
import io.circe.syntax._
import io.circe.{Codec, Decoder, Encoder, Json}
Expand Down Expand Up @@ -266,7 +267,8 @@ object FileEvent {
deriveConfiguredCodec[Digest]
implicit val fileAttributesCodec: Codec.AsObject[FileAttributes] =
deriveConfiguredCodec[FileAttributes]
implicit val coder: Codec.AsObject[FileEvent] = deriveConfiguredCodec[FileEvent]
implicit val enc: Encoder.AsObject[FileEvent] = deriveConfiguredEncoder[FileEvent].mapJsonObject(_.dropNulls)
implicit val codec: Codec.AsObject[FileEvent] = Codec.AsObject.from(deriveConfiguredDecoder, enc)
Serializer()
}

Expand Down Expand Up @@ -335,6 +337,7 @@ object FileEvent {
}
deriveConfiguredEncoder[FileEvent]
.encodeObject(event)
.dropNulls
.remove("storage")
.remove("storageType")
.addIfExists("_storage", storageJsonOpt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"project" : "myorg/myproj",
"storage" : "https://bluebrain.github.io/nexus/vocabulary/disk-storage?rev=1",
"storageType" : "DiskStorage",
"tag" : null,
"attributes" : {
"origin" : "Client",
"uuid" : "8049ba90-7cc6-4de5-93a1-802c04200dcc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"https://bluebrain.github.io/nexus/contexts/files.json"
],
"@type": "FileCreated",
"tag": null,
"_attributes": {
"_bytes": 12,
"_digest": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ch.epfl.bluebrain.nexus.delta.sdk
import cats.Order
import cats.data.NonEmptyMap
import io.circe.syntax.EncoderOps
import io.circe.{Decoder, Encoder}
import io.circe.{Decoder, Encoder, JsonObject}

package object circe {

Expand All @@ -21,4 +21,10 @@ package object circe {
}

}

implicit class JsonObjOps(j: JsonObject) {
def dropNulls: JsonObject = dropNullValues(j)
}

def dropNullValues(j: JsonObject): JsonObject = j.filter { case (_, v) => !v.isNull }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.{contexts, nxv}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.ContextValue
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.JsonLdContext.keywords
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.{CompactedJsonLd, ExpandedJsonLd}
import ch.epfl.bluebrain.nexus.delta.sdk.circe.{dropNullValues, JsonObjOps}
import ch.epfl.bluebrain.nexus.delta.sdk.instances._
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.IriEncoder
import ch.epfl.bluebrain.nexus.delta.sdk.model.BaseUri
Expand Down Expand Up @@ -340,14 +341,12 @@ object ResourceEvent {
implicit val projectRefEncoder: Encoder[ProjectRef] = IriEncoder.jsonEncoder[ProjectRef]
Encoder.encodeJsonObject.contramapObject { event =>
val obj = deriveConfiguredEncoder[ResourceEvent].encodeObject(event)
dropNullValues(obj)
obj.dropNulls
.remove("compacted")
.remove("expanded")
.remove("remoteContexts")
.add(keywords.context, context.value)
}
}
}

private def dropNullValues(j: JsonObject): JsonObject = j.filter { case (_, v) => !v.isNull }
}

0 comments on commit 1b48c05

Please sign in to comment.