Skip to content

Commit

Permalink
Merge pull request #229 from modelix/feature/zip-destructing
Browse files Browse the repository at this point in the history
feat(modelql): support destructing zip output
  • Loading branch information
languitar authored Sep 4, 2023
2 parents 8c2880d + 382c262 commit 1d14313
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ val <T> IMonoStep<IZip8Output<*, *, *, *, *, *, *, *, T>>.eighth: IMonoStep<T>
get() = ZipElementAccessStep<T>(7).also { connect(it) }
val <T> IMonoStep<IZip9Output<*, *, *, *, *, *, *, *, *, T>>.ninth: IMonoStep<T>
get() = ZipElementAccessStep<T>(8).also { connect(it) }

operator fun <T> IMonoStep<IZip1Output<*, T>>.component1() = first
operator fun <T> IMonoStep<IZip2Output<*, *, T>>.component2() = second
operator fun <T> IMonoStep<IZip3Output<*, *, *, T>>.component3() = third
operator fun <T> IMonoStep<IZip4Output<*, *, *, *, T>>.component4() = forth
operator fun <T> IMonoStep<IZip5Output<*, *, *, *, *, T>>.component5() = fifth
operator fun <T> IMonoStep<IZip6Output<*, *, *, *, *, *, T>>.component6() = sixth
operator fun <T> IMonoStep<IZip7Output<*, *, *, *, *, *, *, T>>.component7() = seventh
operator fun <T> IMonoStep<IZip8Output<*, *, *, *, *, *, *, *, T>>.component8() = eighth
operator fun <T> IMonoStep<IZip9Output<*, *, *, *, *, *, *, *, *, T>>.component9() = ninth
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ interface IZip7Output<out Common, out E1, out E2, out E3, out E4, out E5, out E6
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 }
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 }

operator fun <T> IZip1Output<*, T>.component1() = first
operator fun <T> IZip2Output<*, *, T>.component2() = second
operator fun <T> IZip3Output<*, *, *, T>.component3() = third
operator fun <T> IZip4Output<*, *, *, *, T>.component4() = forth
operator fun <T> IZip5Output<*, *, *, *, *, T>.component5() = fifth
operator fun <T> IZip6Output<*, *, *, *, *, *, T>.component6() = sixth
operator fun <T> IZip7Output<*, *, *, *, *, *, *, T>.component7() = seventh
operator fun <T> IZip8Output<*, *, *, *, *, *, *, *, T>.component8() = eighth
operator fun <T> IZip9Output<*, *, *, *, *, *, *, *, *, T>.component9() = ninth

@Serializable
@SerialName("modelix.modelql.zip.output")
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> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ class ModelQLTest {
assertEquals((1..30).map { listOf(it, "abc") }, result)
}

@Test
fun zipDestructingMap() = runTestWithTimeout {
val result = remoteProductDatabaseQuery { db ->
db.products.filter { it.title.equalTo("iPhone 9") }.map {
it.title.zip(it.category).map { (_, category) ->
category
}
}.first()
}

assertEquals("smartphones", result)
}

@Test
fun zipDestructingMapLocal() = runTestWithTimeout {
val result = remoteProductDatabaseQuery { db ->
db.products.filter { it.title.equalTo("iPhone 9") }.map {
it.title.zip(it.category).mapLocal { (title, category) ->
"$title: $category"
}
}.first()
}

assertEquals("iPhone 9: smartphones", result)
}

@Test
fun testMapLocal2_unusedInput() = runTestWithTimeout {
val result = remoteProductDatabaseQuery { db ->
Expand Down

0 comments on commit 1d14313

Please sign in to comment.