-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EPMDJ-3093 implemented filtering and sorting core functionality
- Loading branch information
Showing
7 changed files
with
158 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.epam.drill.admin.endpoints.plugin | ||
|
||
import com.epam.drill.admin.plugin.* | ||
import kotlinx.serialization.* | ||
import kotlin.reflect.* | ||
import kotlin.reflect.full.* | ||
|
||
|
||
@Serializable | ||
data class SearchStatement(val fieldName: String, val value: String) | ||
|
||
@Serializable | ||
data class SortStatement(val fieldName: String, val order: String) | ||
|
||
internal fun FrontMessage.processWithSubscription(subscription: Subscription?): Any = | ||
when (subscription) { | ||
is AgentSubscription -> | ||
applyFilter(subscription.searchStatement) | ||
.applySort(subscription.sortStatement) | ||
else -> this | ||
} | ||
|
||
|
||
private fun FrontMessage.applyFilter(statement: SearchStatement?): FrontMessage = statement?.let { | ||
runCatching { | ||
@Suppress("UNCHECKED_CAST") | ||
(this as? Iterable<Any>)?.filter { fMessage -> | ||
val propertyValue = fMessage::class.getProperty(statement.fieldName).call(fMessage) | ||
statement.value in propertyValue.toString() | ||
} | ||
}.getOrNull() | ||
} ?: this | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
internal fun FrontMessage.applySort(statement: SortStatement?): Any = statement?.let { | ||
runCatching { | ||
val selector: (Any) -> Comparable<Any> = { | ||
it::class.getProperty(statement.fieldName).call(it) as Comparable<Any> | ||
} | ||
val data = this as Iterable<Any> | ||
if (statement.order == "ASC") data.sortedBy(selector) else data.sortedByDescending(selector) | ||
}.getOrNull() | ||
} ?: this | ||
|
||
private fun KClass<out Any>.getProperty(name: String) = | ||
memberProperties.first { it.name == name } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters