Skip to content

Commit 9ba5d0b

Browse files
authored
Merge pull request #261 from robstoll/feature/toVarArg
also provide toVararg for Iterable<T> and Sequence<T>
2 parents ea96186 + 4102ad2 commit 9ba5d0b

File tree

4 files changed

+266
-103
lines changed

4 files changed

+266
-103
lines changed

gradle/code-generation/src/main/kotlin/code-generation.generate.gradle.kts

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
7373
val tupleToList = createStringBuilder(packageName)
7474
val tupleToSequence = createStringBuilder(packageName)
7575
val tupleFlatten = createStringBuilder(packageName)
76-
.append("import kotlin.jvm.JvmName")
76+
.append("import kotlin.jvm.JvmName\n\n")
7777

7878
val toVararg = createStringBuilder(packageName)
79-
.append("import kotlin.jvm.JvmName")
79+
.append("import kotlin.jvm.JvmName\n\n")
8080

8181
(2..numOfArgs).forEach { upperNumber ->
8282
val numbers = (1..upperNumber).toList()
@@ -327,21 +327,22 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
327327
}
328328
}
329329

330-
toVararg.append(
331-
"""
332-
|/**
333-
| * Splits this [Array] into the first element and the rest as `Array<out T>`.
334-
| *
335-
| * This way you can pass it to a function which expects `x: T, vararg otherX: T`.
336-
| *
337-
| * @since 3.1.0
338-
| */
339-
|inline fun <reified T> Array<out T>.toVararg(): Pair<T, Array<out T>> =
340-
| first() to drop(1).toList().toTypedArray()
341-
|
342-
""".trimMargin()
343-
).appendLine()
344-
330+
listOf("Array", "Iterable", "Sequence").forEach { receiver ->
331+
toVararg.append(
332+
"""
333+
|/**
334+
| * Splits this [$receiver] into the first element and the rest as `Array<out T>`.
335+
| *
336+
| * This way you can pass it to a function which expects `x: T, vararg otherX: T`.
337+
| *
338+
| * @since 3.1.0
339+
| */
340+
|inline fun <reified T> $receiver<${if (receiver == "Array") "out " else ""}T>.toVararg(): Pair<T, Array<out T>> =
341+
| first() to drop(1).toList().toTypedArray()
342+
|
343+
""".trimMargin()
344+
).appendLine()
345+
}
345346

346347
primitiveTypes.forEach { (type, arrayType) ->
347348
listOf("Iterable", "Array").forEach { receiver ->
@@ -353,8 +354,7 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
353354
| * This way you can pass it to a function which expects `x: $type, vararg otherX: $type`.
354355
| *
355356
| * @since 3.1.0
356-
| */
357-
|@JvmName("toVararg$type")
357+
| */${if (receiver != "Array") "\n@JvmName(\"toVararg$type\")" else ""}
358358
|fun $receiver<$type>.toVararg(): Pair<$type, $arrayType> =
359359
| first() to drop(1).to$arrayType()
360360
|
@@ -381,13 +381,11 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
381381
| *
382382
| * @since 3.1.0
383383
| */
384-
|@JvmName("toVararg$type")
385384
|fun $arrayType.toVararg(): Pair<$type, $arrayType> =
386385
| first() to drop(1).to$arrayType()
387386
|
388387
""".trimMargin()
389388
).appendLine()
390-
391389
}
392390

393391

@@ -455,6 +453,7 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
455453

456454
val toVarargTest = createStringBuilder(packageName)
457455
.appendTest("ToVarargTest")
456+
458457
toVarargTest.append(
459458
"""
460459
| fun expectString(s: String, vararg others: String) {}
@@ -467,21 +466,32 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
467466
| fun expectFloat(first: Float, vararg others: Float) {}
468467
| fun expectDouble(first: Double, vararg others: Double) {}
469468
|
470-
| @Test
471-
| fun toVararg_array() {
472-
| val arr = arrayOf("a", "b")
473-
| val pair = arr.toVararg()
474-
|
475-
| val (first, rest) = pair
476-
| expectString(first, *rest)
477-
|
478-
| expect(pair) {
479-
| this.first.toEqual("a")
480-
| second.asList().toContainExactly("b")
481-
| }
482-
| }
483469
""".trimMargin()
484-
).appendLine().appendLine()
470+
).appendLine()
471+
472+
listOf(
473+
"Array" to "arrayOf",
474+
"Iterable" to "listOf",
475+
"Sequence" to "sequenceOf"
476+
).forEach { (receiver, factory) ->
477+
toVarargTest.append(
478+
"""
479+
| @Test
480+
| fun toVararg_$receiver() {
481+
| val arr = $factory("a", "b")
482+
| val pair = arr.toVararg()
483+
|
484+
| val (first, rest) = pair
485+
| expectString(first, *rest)
486+
|
487+
| expect(pair) {
488+
| this.first.toEqual("a")
489+
| second.asList().toContainExactly("b")
490+
| }
491+
| }
492+
""".trimMargin()
493+
).appendLine().appendLine()
494+
}
485495

486496
primitiveTypes.forEach { (type, arrayTypeUpper) ->
487497
val arrayType = arrayTypeUpper.first().lowercase() + arrayTypeUpper.drop(1)
@@ -513,23 +523,28 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
513523
| }
514524
""".trimMargin()
515525
).appendLine().appendLine()
516-
listOf("Iterable" to "listOf", "Sequence" to "sequenceOf").forEach { (receiver, factory) ->
526+
527+
listOf(
528+
"Array" to "arrayOf",
529+
"Iterable" to "listOf",
530+
"Sequence" to "sequenceOf"
531+
).forEach { (receiver, factory) ->
517532
toVarargTest.append(
518533
"""
519-
| @Test
520-
| fun toVararg_${receiver}_of_${type}_returns_$arrayType() {
521-
| val arr: $receiver<$type> = ${factory}($value1, $value2)
522-
| val pair = arr.toVararg()
523-
|
524-
| val (first, rest) = pair
525-
| expect$type(first, *rest)
526-
|
527-
| expect(pair) {
528-
| this.first.toEqual($value1)
529-
| second.asList().toContainExactly($value2)
530-
| }
531-
| }
532-
""".trimMargin()
534+
| @Test
535+
| fun toVararg_${receiver}_of_${type}_returns_$arrayType() {
536+
| val arr: $receiver<$type> = ${factory}($value1, $value2)
537+
| val pair = arr.toVararg()
538+
|
539+
| val (first, rest) = pair
540+
| expect$type(first, *rest)
541+
|
542+
| expect(pair) {
543+
| this.first.toEqual($value1)
544+
| second.asList().toContainExactly($value2)
545+
| }
546+
| }
547+
""".trimMargin()
533548
).appendLine().appendLine()
534549
}
535550
}

src/commonMain/generated/kotlin/ch/tutteli/kbox/toVararg.kt

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
// --------------------------------------------------------------------------------------------------------------------
55
package ch.tutteli.kbox
66

7-
import kotlin.jvm.JvmName/**
7+
import kotlin.jvm.JvmName
8+
9+
/**
810
* Splits this [Array] into the first element and the rest as `Array<out T>`.
911
*
1012
* This way you can pass it to a function which expects `x: T, vararg otherX: T`.
@@ -14,6 +16,26 @@ import kotlin.jvm.JvmName/**
1416
inline fun <reified T> Array<out T>.toVararg(): Pair<T, Array<out T>> =
1517
first() to drop(1).toList().toTypedArray()
1618

19+
/**
20+
* Splits this [Iterable] into the first element and the rest as `Array<out T>`.
21+
*
22+
* This way you can pass it to a function which expects `x: T, vararg otherX: T`.
23+
*
24+
* @since 3.1.0
25+
*/
26+
inline fun <reified T> Iterable<T>.toVararg(): Pair<T, Array<out T>> =
27+
first() to drop(1).toList().toTypedArray()
28+
29+
/**
30+
* Splits this [Sequence] into the first element and the rest as `Array<out T>`.
31+
*
32+
* This way you can pass it to a function which expects `x: T, vararg otherX: T`.
33+
*
34+
* @since 3.1.0
35+
*/
36+
inline fun <reified T> Sequence<T>.toVararg(): Pair<T, Array<out T>> =
37+
first() to drop(1).toList().toTypedArray()
38+
1739
/**
1840
* Splits this [Iterable] into the first element and the rest as [BooleanArray].
1941
*
@@ -32,7 +54,6 @@ fun Iterable<Boolean>.toVararg(): Pair<Boolean, BooleanArray> =
3254
*
3355
* @since 3.1.0
3456
*/
35-
@JvmName("toVarargBoolean")
3657
fun Array<Boolean>.toVararg(): Pair<Boolean, BooleanArray> =
3758
first() to drop(1).toBooleanArray()
3859

@@ -54,7 +75,6 @@ fun Sequence<Boolean>.toVararg(): Pair<Boolean, BooleanArray> =
5475
*
5576
* @since 3.1.0
5677
*/
57-
@JvmName("toVarargBoolean")
5878
fun BooleanArray.toVararg(): Pair<Boolean, BooleanArray> =
5979
first() to drop(1).toBooleanArray()
6080

@@ -76,7 +96,6 @@ fun Iterable<Byte>.toVararg(): Pair<Byte, ByteArray> =
7696
*
7797
* @since 3.1.0
7898
*/
79-
@JvmName("toVarargByte")
8099
fun Array<Byte>.toVararg(): Pair<Byte, ByteArray> =
81100
first() to drop(1).toByteArray()
82101

@@ -98,7 +117,6 @@ fun Sequence<Byte>.toVararg(): Pair<Byte, ByteArray> =
98117
*
99118
* @since 3.1.0
100119
*/
101-
@JvmName("toVarargByte")
102120
fun ByteArray.toVararg(): Pair<Byte, ByteArray> =
103121
first() to drop(1).toByteArray()
104122

@@ -120,7 +138,6 @@ fun Iterable<Char>.toVararg(): Pair<Char, CharArray> =
120138
*
121139
* @since 3.1.0
122140
*/
123-
@JvmName("toVarargChar")
124141
fun Array<Char>.toVararg(): Pair<Char, CharArray> =
125142
first() to drop(1).toCharArray()
126143

@@ -142,7 +159,6 @@ fun Sequence<Char>.toVararg(): Pair<Char, CharArray> =
142159
*
143160
* @since 3.1.0
144161
*/
145-
@JvmName("toVarargChar")
146162
fun CharArray.toVararg(): Pair<Char, CharArray> =
147163
first() to drop(1).toCharArray()
148164

@@ -164,7 +180,6 @@ fun Iterable<Short>.toVararg(): Pair<Short, ShortArray> =
164180
*
165181
* @since 3.1.0
166182
*/
167-
@JvmName("toVarargShort")
168183
fun Array<Short>.toVararg(): Pair<Short, ShortArray> =
169184
first() to drop(1).toShortArray()
170185

@@ -186,7 +201,6 @@ fun Sequence<Short>.toVararg(): Pair<Short, ShortArray> =
186201
*
187202
* @since 3.1.0
188203
*/
189-
@JvmName("toVarargShort")
190204
fun ShortArray.toVararg(): Pair<Short, ShortArray> =
191205
first() to drop(1).toShortArray()
192206

@@ -208,7 +222,6 @@ fun Iterable<Int>.toVararg(): Pair<Int, IntArray> =
208222
*
209223
* @since 3.1.0
210224
*/
211-
@JvmName("toVarargInt")
212225
fun Array<Int>.toVararg(): Pair<Int, IntArray> =
213226
first() to drop(1).toIntArray()
214227

@@ -230,7 +243,6 @@ fun Sequence<Int>.toVararg(): Pair<Int, IntArray> =
230243
*
231244
* @since 3.1.0
232245
*/
233-
@JvmName("toVarargInt")
234246
fun IntArray.toVararg(): Pair<Int, IntArray> =
235247
first() to drop(1).toIntArray()
236248

@@ -252,7 +264,6 @@ fun Iterable<Long>.toVararg(): Pair<Long, LongArray> =
252264
*
253265
* @since 3.1.0
254266
*/
255-
@JvmName("toVarargLong")
256267
fun Array<Long>.toVararg(): Pair<Long, LongArray> =
257268
first() to drop(1).toLongArray()
258269

@@ -274,7 +285,6 @@ fun Sequence<Long>.toVararg(): Pair<Long, LongArray> =
274285
*
275286
* @since 3.1.0
276287
*/
277-
@JvmName("toVarargLong")
278288
fun LongArray.toVararg(): Pair<Long, LongArray> =
279289
first() to drop(1).toLongArray()
280290

@@ -296,7 +306,6 @@ fun Iterable<Float>.toVararg(): Pair<Float, FloatArray> =
296306
*
297307
* @since 3.1.0
298308
*/
299-
@JvmName("toVarargFloat")
300309
fun Array<Float>.toVararg(): Pair<Float, FloatArray> =
301310
first() to drop(1).toFloatArray()
302311

@@ -318,7 +327,6 @@ fun Sequence<Float>.toVararg(): Pair<Float, FloatArray> =
318327
*
319328
* @since 3.1.0
320329
*/
321-
@JvmName("toVarargFloat")
322330
fun FloatArray.toVararg(): Pair<Float, FloatArray> =
323331
first() to drop(1).toFloatArray()
324332

@@ -340,7 +348,6 @@ fun Iterable<Double>.toVararg(): Pair<Double, DoubleArray> =
340348
*
341349
* @since 3.1.0
342350
*/
343-
@JvmName("toVarargDouble")
344351
fun Array<Double>.toVararg(): Pair<Double, DoubleArray> =
345352
first() to drop(1).toDoubleArray()
346353

@@ -362,7 +369,6 @@ fun Sequence<Double>.toVararg(): Pair<Double, DoubleArray> =
362369
*
363370
* @since 3.1.0
364371
*/
365-
@JvmName("toVarargDouble")
366372
fun DoubleArray.toVararg(): Pair<Double, DoubleArray> =
367373
first() to drop(1).toDoubleArray()
368374

src/commonMain/generated/kotlin/ch/tutteli/kbox/tupleFlatten.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
// --------------------------------------------------------------------------------------------------------------------
55
package ch.tutteli.kbox
66

7-
import kotlin.jvm.JvmName/**
7+
import kotlin.jvm.JvmName
8+
9+
/**
810
* Flattens a [List] of [Pair]<T, T> into a `List<T>`.
911
*
1012
* Kotlin will automatically infer the least upper bound type in case your component types A1, A2, ...

0 commit comments

Comments
 (0)