Skip to content

Commit

Permalink
fix: nested block depth / more functional way
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Nov 30, 2024
1 parent e0a98cc commit dc3fa78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
1 change: 0 additions & 1 deletion shared/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ID>LongMethod:QueryUtils.kt$private fun transformQQueryToSqlJsonPath( mainAttributePath: List&lt;ExpandedTerm&gt;, trailingAttributePath: List&lt;ExpandedTerm&gt;, operator: String, value: String )</ID>
<ID>LongParameterList:ApiResponses.kt$( body: String, count: Int, resourceUrl: String, paginationQuery: PaginationQuery, requestParams: MultiValueMap&lt;String, String&gt;, mediaType: MediaType, contexts: List&lt;String&gt; )</ID>
<ID>LongParameterList:ApiResponses.kt$( entities: Any, count: Int, resourceUrl: String, paginationQuery: PaginationQuery, requestParams: MultiValueMap&lt;String, String&gt;, mediaType: MediaType, contexts: List&lt;String&gt; )</ID>
<ID>NestedBlockDepth:CompactedEntity.kt$private fun filterLanguageProperty(value: Map&lt;String, Any&gt;, transformationParameters: Map&lt;String, String&gt;?): Any</ID>
<ID>SpreadOperator:EntityEvent.kt$EntityEvent$( *[ JsonSubTypes.Type(value = EntityCreateEvent::class), JsonSubTypes.Type(value = EntityReplaceEvent::class), JsonSubTypes.Type(value = EntityDeleteEvent::class), JsonSubTypes.Type(value = AttributeAppendEvent::class), JsonSubTypes.Type(value = AttributeReplaceEvent::class), JsonSubTypes.Type(value = AttributeUpdateEvent::class), JsonSubTypes.Type(value = AttributeDeleteEvent::class), JsonSubTypes.Type(value = AttributeDeleteAllInstancesEvent::class) ] )</ID>
<ID>TooManyFunctions:JsonLdUtils.kt$JsonLdUtils</ID>
</CurrentIssues>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ val JSONLD_COMPACTED_ATTRIBUTE_CORE_MEMBERS =
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,
JSONLD_LANGUAGEMAP_TERM
NGSILD_OBSERVED_AT_TERM
)

fun CompactedEntity.getRelationshipsObjects(): Set<URI> =
Expand Down Expand Up @@ -160,42 +160,37 @@ private fun filterLanguageProperty(value: Map<String, Any>, transformationParame
AttributeCompactedType.forKey(value[JSONLD_TYPE_TERM] as String)
}

if (attributeCompactedType == LANGUAGEPROPERTY) {
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()
}
val filteredMainAttribute = mutableMapOf(
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
)
value.forEach { (key, value) ->
if (JSONLD_COMPACTED_ATTRIBUTE_CORE_MEMBERS.contains(key)) {
if (key != JSONLD_LANGUAGEMAP_TERM && key != JSONLD_TYPE_TERM) filteredMainAttribute[key] = value
} else {
filteredMainAttribute[key] = filterLanguageProperty(
value as Map<String, Any>,
transformationParameters
)
}
}

return filteredMainAttribute
} else {
return value.map { entry ->
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 == 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)
else -> entry.key to entry.value
}
}.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
}
}.toMap()
}

fun CompactedEntity.toGeoJson(geometryProperty: String): Map<String, Any?> {
Expand Down

0 comments on commit dc3fa78

Please sign in to comment.