-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from all commits
3d2ff51
199d844
7c5ff11
3dfa4e6
0403c57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -42,6 +46,21 @@ typealias CompactedEntity = Map<String, Any> | |
typealias CompactedAttributeInstance = Map<String, Any> | ||
typealias CompactedAttributeInstances = List<CompactedAttributeInstance> | ||
|
||
val JSONLD_COMPACTED_ATTRIBUTE_CORE_MEMBERS = | ||
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( | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?> { | ||
|
There was a problem hiding this comment.
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)