Created
November 18, 2025 16:17
-
-
Save fmaxx/a0d27200c93ee9b40af15c68e89ead33 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.aura.android | |
| /** | |
| * Created by Maxim Firsov on 18.11.2025. | |
| * [email protected] | |
| */ | |
| data class User( | |
| val id: Int, | |
| val name: String, | |
| val age: Int, | |
| val email: String? = null, | |
| val city: String? = null, | |
| ) { | |
| operator fun get(key: String): Any? { | |
| return when (key) { | |
| "id" -> id | |
| "name" -> name | |
| "age" -> age | |
| "email" -> email | |
| "city" -> city | |
| else -> null | |
| } | |
| } | |
| } | |
| val users = listOf( | |
| User(id = 1, name = "John Doe", age = 21, email = "[email protected]", city = "Berlin"), | |
| User(id = 2, name = "Jane Smith", age = 22, email = "[email protected]", city = "Paris"), | |
| User(id = 3, name = "Alice Johnson", age = 21, email = "[email protected]", city = "Berlin"), | |
| User(id = 4, name = "Bob Jones", age = 21, email = "[email protected]", city = "Bern"), | |
| User(id = 5, name = "Eva Brown", age = 21, email = "[email protected]", city = null), | |
| User(id = 6, name = "Noah Wilson", age = 21, email = "[email protected]", city = "Berlin"), | |
| ) | |
| fun testQuery() { | |
| val result = users.query( | |
| actions = listOf( | |
| where("name", "John Doe"), | |
| where("name", "Bob Jones"), | |
| where("name", "Noah Wilson") | |
| ), | |
| groupBy = groupBy("city") | |
| ) | |
| } | |
| fun where(key: String, value: Any): QueryAction<User> = WhereQueryAction(key, value) | |
| fun groupBy(key: String): QueryAction<User> = GroupBy(key) | |
| fun interface QueryAction<T> { | |
| fun execute(input: List<T>): List<T> | |
| } | |
| class WhereQueryAction(val key: String, val value: Any) : QueryAction<User> { | |
| override fun execute(input: List<User>): List<User> { | |
| return input.filter { | |
| it[key] == value | |
| } | |
| } | |
| } | |
| class GroupBy(val key: String) : QueryAction<User> { | |
| override fun execute(input: List<User>): List<User> { | |
| return input.groupBy { | |
| when (key) { | |
| "id" -> it.id | |
| "name" -> it.name | |
| "age" -> it.age | |
| "email" -> it.email | |
| "city" -> it.city | |
| else -> null | |
| } | |
| }.values.flatten() | |
| } | |
| } | |
| fun <T> List<T>.query( | |
| actions: List<QueryAction<T>>, | |
| groupBy: QueryAction<T>, | |
| ): List<T> { | |
| return actions | |
| .map { | |
| it.execute(this) | |
| } | |
| .flatten() | |
| .toSet() | |
| .toList() | |
| .run { | |
| groupBy.execute(this) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment