Skip to content

Commit ace6ccf

Browse files
committed
feat(modelql): findAll that accepts multiple keys as a flux
1 parent a6a5c50 commit ace6ccf

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

modelql-core/src/commonMain/kotlin/org/modelix/modelql/core/ConstantSourceStep.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,15 @@ fun String?.asMono() = createConstantSourceStep(this)
166166
fun Set<String?>.asMono() = createConstantSourceStep(this)
167167

168168
inline fun <reified T> nullMono(): IMonoStep<T?> = ConstantSourceStep<T?>(null, typeOf<T?>())
169+
170+
fun fluxOf(vararg elements: String): IFluxStep<String> = when (elements.size) {
171+
0 -> nullMono<String>().filterNotNull().asFlux()
172+
1 -> elements.first().asMono().asFlux()
173+
else -> {
174+
var result = elements[0].asMono().plus(elements[1].asMono())
175+
for (e in elements.drop(2)) {
176+
result = result.plus(e.asMono())
177+
}
178+
result
179+
}
180+
}

modelql-core/src/commonMain/kotlin/org/modelix/modelql/core/MemoizingStep.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ fun <In, K, Out : Any> IMonoStep<In>.find(elements: (IMonoStep<In>) -> IFluxStep
9696
fun <In, K, Out : Any> IMonoStep<In>.findAll(elements: (IMonoStep<In>) -> IFluxStep<Out>, keySelector: (IMonoStep<Out>) -> IProducingStep<K>, key: IMonoStep<K>): IFluxStep<Out> {
9797
return memoize { elements(it).flatMap { keySelector(it).allowEmpty().zip(it) }.toMultimap() }.get(key).filterNotNull().toFlux()
9898
}
99+
100+
fun <In, K, Out : Any> IMonoStep<In>.findAll(elements: (IMonoStep<In>) -> IFluxStep<Out>, keySelector: (IMonoStep<Out>) -> IProducingStep<K>, keys: IFluxStep<K>): IFluxStep<Out> {
101+
val map: IMonoStep<Map<K, List<Out>>> = memoize { elements(it).flatMap { keySelector(it).allowEmpty().zip(it) }.toMultimap() }
102+
return map.zip(keys.allowEmpty()).flatMap { it.first.get(it.second).filterNotNull().toFlux() }
103+
}

modelql-core/src/commonTest/kotlin/org/modelix/modelql/core/ModelQLTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,28 @@ class ModelQLTest {
368368
assertEquals(listOf("iPhone 9", "iPhone X", "Samsung Universe 9", "OPPOF19", "Huawei P30"), result)
369369
}
370370

371+
@Test
372+
fun testFindAllMultipleKeys() = runTestWithTimeout {
373+
val result: List<String> = remoteProductDatabaseQuery { db ->
374+
db.findAll({ it.products }, { it.category }, fluxOf("smartphones", "laptops")).map { it.title }.toList()
375+
}
376+
assertEquals(
377+
listOf(
378+
"iPhone 9",
379+
"iPhone X",
380+
"Samsung Universe 9",
381+
"OPPOF19",
382+
"Huawei P30",
383+
"MacBook Pro",
384+
"Samsung Galaxy Book",
385+
"Microsoft Surface Laptop 4",
386+
"Infinix INBOOK",
387+
"HP Pavilion 15-DK1056WM",
388+
),
389+
result,
390+
)
391+
}
392+
371393
@Test
372394
fun assertNotEmpty_throws_IllegalArgumentException() = runTestWithTimeout {
373395
assertFailsWith(IllegalArgumentException::class) {

0 commit comments

Comments
 (0)