Skip to content

Commit 382c262

Browse files
committed
feat(modelql): support destructing zip output
With these additions, zip output can be used with kotlin destructing declarations: ```kotlin foo.zip(bar).map { (theFoo, theBar) -> ... } foo.zip(bar).mapLocal { (theFoo, theBar) -> ... } ```
1 parent 8c2880d commit 382c262

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ val <T> IMonoStep<IZip8Output<*, *, *, *, *, *, *, *, T>>.eighth: IMonoStep<T>
6464
get() = ZipElementAccessStep<T>(7).also { connect(it) }
6565
val <T> IMonoStep<IZip9Output<*, *, *, *, *, *, *, *, *, T>>.ninth: IMonoStep<T>
6666
get() = ZipElementAccessStep<T>(8).also { connect(it) }
67+
68+
operator fun <T> IMonoStep<IZip1Output<*, T>>.component1() = first
69+
operator fun <T> IMonoStep<IZip2Output<*, *, T>>.component2() = second
70+
operator fun <T> IMonoStep<IZip3Output<*, *, *, T>>.component3() = third
71+
operator fun <T> IMonoStep<IZip4Output<*, *, *, *, T>>.component4() = forth
72+
operator fun <T> IMonoStep<IZip5Output<*, *, *, *, *, T>>.component5() = fifth
73+
operator fun <T> IMonoStep<IZip6Output<*, *, *, *, *, *, T>>.component6() = sixth
74+
operator fun <T> IMonoStep<IZip7Output<*, *, *, *, *, *, *, T>>.component7() = seventh
75+
operator fun <T> IMonoStep<IZip8Output<*, *, *, *, *, *, *, *, T>>.component8() = eighth
76+
operator fun <T> IMonoStep<IZip9Output<*, *, *, *, *, *, *, *, *, T>>.component9() = ninth

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ interface IZip7Output<out Common, out E1, out E2, out E3, out E4, out E5, out E6
303303
interface IZip8Output<out Common, out E1, out E2, out E3, out E4, out E5, out E6, out E7, out E8> : IZip7Output<Common, E1, E2, E3, E4, E5, E6, E7> { val eighth: E8 }
304304
interface IZip9Output<out Common, out E1, out E2, out E3, out E4, out E5, out E6, out E7, out E8, out E9> : IZip8Output<Common, E1, E2, E3, E4, E5, E6, E7, E8> { val ninth: E9 }
305305

306+
operator fun <T> IZip1Output<*, T>.component1() = first
307+
operator fun <T> IZip2Output<*, *, T>.component2() = second
308+
operator fun <T> IZip3Output<*, *, *, T>.component3() = third
309+
operator fun <T> IZip4Output<*, *, *, *, T>.component4() = forth
310+
operator fun <T> IZip5Output<*, *, *, *, *, T>.component5() = fifth
311+
operator fun <T> IZip6Output<*, *, *, *, *, *, T>.component6() = sixth
312+
operator fun <T> IZip7Output<*, *, *, *, *, *, *, T>.component7() = seventh
313+
operator fun <T> IZip8Output<*, *, *, *, *, *, *, *, T>.component8() = eighth
314+
operator fun <T> IZip9Output<*, *, *, *, *, *, *, *, *, T>.component9() = ninth
315+
306316
@Serializable
307317
@SerialName("modelix.modelql.zip.output")
308318
data class ZipOutput<out Common, out E1, out E2, out E3, out E4, out E5, out E6, out E7, out E8, out E9>(override val values: List<Common>) : IZip9Output<Common, E1, E2, E3, E4, E5, E6, E7, E8, E9> {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ class ModelQLTest {
222222
assertEquals((1..30).map { listOf(it, "abc") }, result)
223223
}
224224

225+
@Test
226+
fun zipDestructingMap() = runTestWithTimeout {
227+
val result = remoteProductDatabaseQuery { db ->
228+
db.products.filter { it.title.equalTo("iPhone 9") }.map {
229+
it.title.zip(it.category).map { (_, category) ->
230+
category
231+
}
232+
}.first()
233+
}
234+
235+
assertEquals("smartphones", result)
236+
}
237+
238+
@Test
239+
fun zipDestructingMapLocal() = runTestWithTimeout {
240+
val result = remoteProductDatabaseQuery { db ->
241+
db.products.filter { it.title.equalTo("iPhone 9") }.map {
242+
it.title.zip(it.category).mapLocal { (title, category) ->
243+
"$title: $category"
244+
}
245+
}.first()
246+
}
247+
248+
assertEquals("iPhone 9: smartphones", result)
249+
}
250+
225251
@Test
226252
fun testMapLocal2_unusedInput() = runTestWithTimeout {
227253
val result = remoteProductDatabaseQuery { db ->

0 commit comments

Comments
 (0)