Skip to content

Commit

Permalink
small refactoring and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Nov 30, 2023
1 parent 6790ca2 commit 389ff40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ typealias JsonLdNgsiLdEntity = Pair<JsonLdEntity, NgsiLdEntity>
fun List<JsonLdNgsiLdEntity>.extractNgsiLdEntities(): List<NgsiLdEntity> = this.map { it.second }
fun JsonLdNgsiLdEntity.entityId(): URI = this.second.id

// a temporary data class to hold the result of deserializing, expanding and transforming to NGSI-LD entities
// the entities received in a batch operation
data class BatchEntityPreparation(
val success: List<JsonLdNgsiLdEntity> = emptyList(),
val errors: List<Pair<String, APIException>> = emptyList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.egm.stellio.search.web

import arrow.core.Either
import arrow.core.Option
import arrow.core.left
import arrow.core.*
import arrow.core.raise.either
import arrow.core.right
import com.egm.stellio.search.authorization.AuthorizationService
import com.egm.stellio.search.service.EntityEventService
import com.egm.stellio.search.service.EntityOperationService
Expand Down Expand Up @@ -309,21 +306,18 @@ class EntityOperationHandler(
contentType: MediaType?
): BatchEntityPreparation =
payload.map {
if (contentType == JSON_LD_MEDIA_TYPE)
expandJsonLdEntityF(it).mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) }
else
expandJsonLdEntityF(it, listOf(context ?: NGSILD_CORE_CONTEXT))
.mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) }
}.map {
when (it) {
is Either.Left -> it.value.left()
is Either.Right -> {
when (val result = it.value.toNgsiLdEntity()) {
is Either.Left -> Pair(it.value.id, result.value).left()
is Either.Right -> Pair(it.value, result.value).right()
}
val jsonLdExpansionResult =
if (contentType == JSON_LD_MEDIA_TYPE)
expandJsonLdEntityF(it)
else
expandJsonLdEntityF(it, listOf(context ?: NGSILD_CORE_CONTEXT))
jsonLdExpansionResult
.mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) }
.flatMap { jsonLdEntity ->
jsonLdEntity.toNgsiLdEntity()
.mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) }
.map { ngsiLdEntity -> Pair(jsonLdEntity, ngsiLdEntity) }
}
}
}.fold(BatchEntityPreparation()) { acc, entry ->
when (entry) {
is Either.Left -> acc.copy(errors = acc.errors.plus(entry.value))
Expand Down

0 comments on commit 389ff40

Please sign in to comment.