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

feat: add language filter to sub attributes #1274

Merged
merged 5 commits into from
Dec 3, 2024
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 @@ -19,17 +19,21 @@ import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_OBJECT
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_TYPE_TERM
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_VALUE_TERM
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_VOCAB_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_CREATED_AT_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_DATASET_ID_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_DATASET_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_ENTITY_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_GEOPROPERTY_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_JSONPROPERTY_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_LANGUAGEPROPERTY_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_LANG_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_MODIFIED_AT_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_NONE_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_OBSERVED_AT_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PROPERTY_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_RELATIONSHIP_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_SYSATTRS_TERMS
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_UNIT_CODE_TERM
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_VOCABPROPERTY_TERM
import com.egm.stellio.shared.util.PROPERTIES_PROPERTY_TERM
import com.egm.stellio.shared.util.QUERY_PARAM_LANG
Expand All @@ -42,6 +46,21 @@ typealias CompactedEntity = Map<String, Any>
typealias CompactedAttributeInstance = Map<String, Any>
typealias CompactedAttributeInstances = List<CompactedAttributeInstance>

val JSONLD_COMPACTED_ATTRIBUTE_CORE_MEMBERS =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSONLD_LANGUAGEMAP_TERM should be in the list (if it is not in the list, it is a trap for future users of your list)

setOf(
JSONLD_TYPE_TERM,
JSONLD_VALUE_TERM,
JSONLD_OBJECT,
JSONLD_JSON_TERM,
JSONLD_VOCAB_TERM,
JSONLD_LANGUAGEMAP_TERM,
NGSILD_UNIT_CODE_TERM,
NGSILD_DATASET_ID_TERM,
NGSILD_CREATED_AT_TERM,
NGSILD_MODIFIED_AT_TERM,
NGSILD_OBSERVED_AT_TERM
)

fun CompactedEntity.getRelationshipsObjects(): Set<URI> =
this.mapValues { entry ->
applyAttributeTransformation(
Expand Down Expand Up @@ -140,27 +159,38 @@ private fun filterLanguageProperty(value: Map<String, Any>, transformationParame
val attributeCompactedType = value[JSONLD_TYPE_TERM]?.let {
AttributeCompactedType.forKey(value[JSONLD_TYPE_TERM] as String)
}
val languageFilteredValue =
if (attributeCompactedType == LANGUAGEPROPERTY) {
val localeRanges = Locale.LanguageRange.parse(languageFilter)
val propertyLocales = (value[JSONLD_LANGUAGEMAP_TERM] as Map<String, Any>).keys.sorted()
val bestLocaleMatch = Locale.filterTags(localeRanges, propertyLocales)
.getOrElse(0) { _ ->
// as the list is sorted, @none is the first in the list if it exists
propertyLocales.first()
}
mapOf(
JSONLD_TYPE_TERM to NGSILD_PROPERTY_TERM,
JSONLD_VALUE_TERM to (value[JSONLD_LANGUAGEMAP_TERM] as Map<String, Any>)[bestLocaleMatch],
NGSILD_LANG_TERM to bestLocaleMatch
)
} else value

return languageFilteredValue.mapValues { entry ->
if (entry.key == NGSILD_ENTITY_TERM)
(entry.value as CompactedEntity).toFilteredLanguageProperties(languageFilter)
else entry.value
}
return if (attributeCompactedType == LANGUAGEPROPERTY) {
val localeRanges = Locale.LanguageRange.parse(languageFilter)
val propertyLocales = (value[JSONLD_LANGUAGEMAP_TERM] as Map<String, Any>).keys.sorted()
val bestLocaleMatch = Locale.filterTags(localeRanges, propertyLocales)
.getOrElse(0) { _ ->
// as the list is sorted, @none is the first in the list if it exists
propertyLocales.first()
}

value.map { entry ->
when {
entry.key == JSONLD_TYPE_TERM ->
JSONLD_TYPE_TERM to NGSILD_PROPERTY_TERM
entry.key == JSONLD_LANGUAGEMAP_TERM ->
JSONLD_VALUE_TERM to (value[JSONLD_LANGUAGEMAP_TERM] as Map<String, Any>)[bestLocaleMatch]
JSONLD_COMPACTED_ATTRIBUTE_CORE_MEMBERS.contains(entry.key) ->
entry.key to entry.value
else ->
entry.key to filterLanguageProperty(entry.value as Map<String, Any>, transformationParameters)
}
}.toMap()
.plus(NGSILD_LANG_TERM to bestLocaleMatch)
} else value.map { entry ->
when {
entry.key == NGSILD_ENTITY_TERM ->
entry.key to (entry.value as CompactedEntity).toFilteredLanguageProperties(languageFilter)
!JSONLD_COMPACTED_ATTRIBUTE_CORE_MEMBERS.contains(entry.key) ->
entry.key to filterLanguageProperty(entry.value as Map<String, Any>, transformationParameters)
else -> entry.key to entry.value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to keep entry as the argument (as you want) you can use entry.toPair()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current is easier to read as it is similar to other branches in the when

}
}.toMap()
}

fun CompactedEntity.toGeoJson(geometryProperty: String): Map<String, Any?> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ object JsonLdUtils {
const val NGSILD_OBSERVED_AT_TERM = "observedAt"
const val NGSILD_OBSERVED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_OBSERVED_AT_TERM"
const val NGSILD_UNIT_CODE_PROPERTY = "https://uri.etsi.org/ngsi-ld/unitCode"
const val NGSILD_UNIT_CODE_TERM = "unitCode"
const val NGSILD_LOCATION_TERM = "location"
const val NGSILD_LOCATION_PROPERTY = "https://uri.etsi.org/ngsi-ld/location"
const val NGSILD_OBSERVATION_SPACE_TERM = "observationSpace"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.egm.stellio.shared.model
import com.egm.stellio.shared.util.JsonUtils.deserializeAsMap
import com.egm.stellio.shared.util.JsonUtils.serializeObject
import com.egm.stellio.shared.util.assertJsonPayloadsAreEqual
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
Expand Down Expand Up @@ -164,4 +166,128 @@ class LanguageFilterTests {

assertJsonPayloadsAreEqual(expectedFilteredRepresentation, serializeObject(filteredRepresentation))
}

@Test
fun `it should filter language properties for sub attributes`() = runTest {
val entity = """
{
"id": "urn:ngsi-ld:Beehive:01",
"type": "Beehive",
"managedBy": {
"type": "Relationship",
"object": "urn:ngsi-ld:Beekeeper:1",
"name": {
"type": "LanguageProperty",
"languageMap": {
"en": "beekeeper",
"fr": "apiculteur"
}
}
}
}
""".trimIndent().deserializeAsMap()

val filteredEntity = entity.toFilteredLanguageProperties("en")

assertJsonPayloadsAreEqual(
"""
{
"id": "urn:ngsi-ld:Beehive:01",
"type": "Beehive",
"managedBy": {
"type": "Relationship",
"object": "urn:ngsi-ld:Beekeeper:1",
"name": {
"type": "Property",
"value": "beekeeper",
"lang": "en"
}
}
}
""".trimIndent(),
serializeObject(filteredEntity)
)
}

@Test
fun `it should filter language properties for an attribute and its sub attributes`() = runTest {
val entity = """
{
"id": "urn:ngsi-ld:Beehive:01",
"type": "Beehive",
"name": {
"type": "LanguageProperty",
"languageMap": {
"en": "beekeeper",
"fr": "apiculteur"
},
"subAttribute": {
"type": "LanguageProperty",
"languageMap": {
"en": "English",
"fr": "Français"
}
}
}
}
""".trimIndent().deserializeAsMap()

val filteredEntity = entity.toFilteredLanguageProperties("en")

assertJsonPayloadsAreEqual(
"""
{
"id": "urn:ngsi-ld:Beehive:01",
"type": "Beehive",
"name": {
"type": "Property",
"value": "beekeeper",
"lang": "en",
"subAttribute": {
"type": "Property",
"value": "English",
"lang": "en"
}
}
}
""".trimIndent(),
serializeObject(filteredEntity)
)
}

@Test
fun `it should filter language properties having other core attribute members`() = runTest {
val entity = """
{
"id": "urn:ngsi-ld:Beehive:01",
"type": "Beehive",
"name": {
"type": "LanguageProperty",
"languageMap": {
"en": "beekeeper",
"fr": "apiculteur"
},
"observedAt": "2024-11-30T00:00:00Z"
}
}
""".trimIndent().deserializeAsMap()

val filteredEntity = entity.toFilteredLanguageProperties("en")

assertJsonPayloadsAreEqual(
"""
{
"id": "urn:ngsi-ld:Beehive:01",
"type": "Beehive",
"name": {
"type": "Property",
"value": "beekeeper",
"lang": "en",
"observedAt": "2024-11-30T00:00:00Z"
}
}
""".trimIndent(),
serializeObject(filteredEntity)
)
}
}
Loading