Skip to content

Commit 8a00c45

Browse files
committed
add a1 and mapA1 (a2..) to Pair and Triple
simplify code-generation
1 parent 89a6dfd commit 8a00c45

File tree

18 files changed

+411
-149
lines changed

18 files changed

+411
-149
lines changed

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

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ fun getTupleName(numOfValues: Int) = when (numOfValues) {
2828
else -> "Tuple$numOfValues"
2929
}
3030

31-
fun getArgName(numOfValues: Int, index: Int) = when (index) {
32-
1 -> if (numOfValues <= 3) "first" else "a$index"
33-
2 -> if (numOfValues <= 3) "second" else "a$index"
34-
3 -> if (numOfValues <= 3) "third" else "a$index"
35-
else -> "a$index"
31+
fun getArgName(argNum: Int) = when (argNum) {
32+
1 -> "first"
33+
2 -> "second"
34+
3 -> "third"
35+
else -> error("should only be used for Pair and Triple")
3636
}
3737

38+
3839
fun withOrdinalIndicator(index: Int) = index.toString() + when (index) {
3940
1 -> "st"
4041
2 -> "nd"
@@ -106,7 +107,7 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
106107
val tupleName = getTupleName(upperNumber)
107108
val tupleLike = createStringBuilder(packageName)
108109
val tuple = createStringBuilder(packageName)
109-
val properties = numbers.joinToString(", ") { getArgName(upperNumber, it) }
110+
val properties = numbers.joinToString(", ") { "a$it" }
110111

111112
tupleLike.append(
112113
"""
@@ -154,7 +155,7 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
154155
| *
155156
| * ${numbers.joinToString("\n * ") { "@param A$it The type of the ${withOrdinalIndicator(it)} component of this $tupleName." }}
156157
| *
157-
| * ${numbers.joinToString("\n * ") { "@param a$it the ${withOrdinalIndicator(it)} component of this $tupleName." }}
158+
| * ${numbers.joinToString("\n * ") { "@property a$it the ${withOrdinalIndicator(it)} component of this $tupleName." }}
158159
| *
159160
| * @since 2.0.0
160161
| */
@@ -294,7 +295,7 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
294295

295296
if (upperNumber2 > 1) {
296297
val properties2 = numbers2.joinToString(", ") {
297-
"$tupleNameParamLowercase.${getArgName(upperNumber2, it)}"
298+
"$tupleNameParamLowercase.a$it"
298299
}
299300

300301
tupleGlue.append(
@@ -319,29 +320,36 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
319320
numbers.forEach { argNum ->
320321
val modifiedTypeArgs = numbers.joinToString(", ") { "A$it" + if (it == argNum) "New" else "" }
321322
val args = numbers.joinToString(", ") { arg ->
322-
getArgName(upperNumber, arg).let {
323+
"a$arg".let {
323324
if (arg == argNum) "transform($it)" else it
324325
}
325326
}
326327

327-
val argNameToMap = getArgName(upperNumber, argNum)
328-
val argNameCapitalized = argNameToMap.replaceFirstChar {
329-
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
328+
fun appendMap(argNameToMap: String, argNameCapitalized: String) =
329+
tupleMap.append(
330+
"""
331+
|/**
332+
|* Maps [$tupleName.$argNameToMap] with the given [transform] function and returns a new [$tupleName].
333+
|*
334+
|* @since 2.0.0
335+
|*/
336+
|fun <$typeArgs, A${argNum}New> $tupleName<$typeArgs>.map$argNameCapitalized(
337+
| transform: (A$argNum) -> A${argNum}New
338+
|): $tupleName<$modifiedTypeArgs> =
339+
| $tupleName($args)
340+
|
341+
""".trimMargin()
342+
).appendLine()
343+
344+
appendMap("a$argNum", "A$argNum")
345+
346+
if (upperNumber <= 3) {
347+
val argNameToMap = getArgName(argNum)
348+
val argNameCapitalized = argNameToMap.replaceFirstChar {
349+
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
350+
}
351+
appendMap(argNameToMap, argNameCapitalized)
330352
}
331-
tupleMap.append(
332-
"""
333-
|/**
334-
|* Maps [$tupleName.$argNameToMap] with the given [transform] function and returns a new [$tupleName].
335-
|*
336-
|* @since 2.0.0
337-
|*/
338-
|fun <$typeArgs, A${argNum}New> $tupleName<$typeArgs>.map$argNameCapitalized(
339-
| transform: (A$argNum) -> A${argNum}New
340-
|): $tupleName<$modifiedTypeArgs> =
341-
| $tupleName($args)
342-
|
343-
""".trimMargin()
344-
).appendLine()
345353
}
346354
}
347355

@@ -1052,7 +1060,7 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
10521060
val valsAsArgs = numbers.joinToString(", ") { "a$it" }
10531061
val tupleCreation = """$tupleName($valsAsArgs)"""
10541062
fun sameFeatureCheck(num: Int, indent: String) = (1..num).joinToString("\n$indent") {
1055-
"feature { f(it::${getArgName(num, it)}) }.toBeTheInstance(a${it})"
1063+
"feature { f(it::a$it) }.toBeTheInstance(a${it})"
10561064
}
10571065

10581066
val tupleMapTest = createStringBuilder("$packageName.map")
@@ -1090,20 +1098,16 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
10901098
).appendLine().appendLine()
10911099

10921100
numbers.forEach { argNum ->
1093-
val argNameToMap = getArgName(upperNumber, argNum)
1094-
val argNameCapitalized = argNameToMap.replaceFirstChar {
1095-
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
1096-
}
1097-
listOf(1, 2, 3).withIndex()
10981101

10991102
val tupleListResult = "$tupleName(${
11001103
argValuesNotMapped.take(upperNumber).withIndex().joinToString(", ") { (index, value) ->
11011104
if (index + 1 == argNum) value else "a${index + 1}"
11021105
}
11031106
})"
11041107

1105-
tupleMapTest.append(
1106-
"""
1108+
fun appendMapTest(argNameCapitalized: String) =
1109+
tupleMapTest.append(
1110+
"""
11071111
| @Test
11081112
| fun map${argNameCapitalized}__identity__returns_equal_$tupleName() {
11091113
| $vals
@@ -1126,14 +1130,29 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
11261130
| ) {
11271131
| toEqual($tupleListResult)
11281132
| ${
1129-
numbers.filter { it != argNum }.joinToString("\n ") {
1130-
"feature { f(it::${getArgName(upperNumber, it)}) }.toBeTheInstance(a${it})"
1133+
numbers.filter { it != argNum }.joinToString("\n ") {
1134+
"feature { f(it::a$it) }.toBeTheInstance(a${it})"
1135+
}
11311136
}
1132-
}
11331137
| }
11341138
| }
11351139
""".trimMargin()
1136-
).appendLine().appendLine()
1140+
).appendLine().appendLine()
1141+
1142+
appendMapTest("A$argNum")
1143+
1144+
if(upperNumber <= 3){
1145+
val argNameToMap = getArgName(argNum)
1146+
val argNameCapitalized = argNameToMap.replaceFirstChar {
1147+
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
1148+
}
1149+
appendMapTest(argNameCapitalized)
1150+
}
1151+
1152+
1153+
1154+
1155+
11371156
}
11381157

11391158
toTupleTest.append(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ package ch.tutteli.kbox
1212
* @param A3 The type of the 3rd component of this Tuple4.
1313
* @param A4 The type of the 4th component of this Tuple4.
1414
*
15-
* @param a1 the 1st component of this Tuple4.
16-
* @param a2 the 2nd component of this Tuple4.
17-
* @param a3 the 3rd component of this Tuple4.
18-
* @param a4 the 4th component of this Tuple4.
15+
* @property a1 the 1st component of this Tuple4.
16+
* @property a2 the 2nd component of this Tuple4.
17+
* @property a3 the 3rd component of this Tuple4.
18+
* @property a4 the 4th component of this Tuple4.
1919
*
2020
* @since 2.0.0
2121
*/

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ package ch.tutteli.kbox
1313
* @param A4 The type of the 4th component of this Tuple5.
1414
* @param A5 The type of the 5th component of this Tuple5.
1515
*
16-
* @param a1 the 1st component of this Tuple5.
17-
* @param a2 the 2nd component of this Tuple5.
18-
* @param a3 the 3rd component of this Tuple5.
19-
* @param a4 the 4th component of this Tuple5.
20-
* @param a5 the 5th component of this Tuple5.
16+
* @property a1 the 1st component of this Tuple5.
17+
* @property a2 the 2nd component of this Tuple5.
18+
* @property a3 the 3rd component of this Tuple5.
19+
* @property a4 the 4th component of this Tuple5.
20+
* @property a5 the 5th component of this Tuple5.
2121
*
2222
* @since 2.0.0
2323
*/

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ package ch.tutteli.kbox
1414
* @param A5 The type of the 5th component of this Tuple6.
1515
* @param A6 The type of the 6th component of this Tuple6.
1616
*
17-
* @param a1 the 1st component of this Tuple6.
18-
* @param a2 the 2nd component of this Tuple6.
19-
* @param a3 the 3rd component of this Tuple6.
20-
* @param a4 the 4th component of this Tuple6.
21-
* @param a5 the 5th component of this Tuple6.
22-
* @param a6 the 6th component of this Tuple6.
17+
* @property a1 the 1st component of this Tuple6.
18+
* @property a2 the 2nd component of this Tuple6.
19+
* @property a3 the 3rd component of this Tuple6.
20+
* @property a4 the 4th component of this Tuple6.
21+
* @property a5 the 5th component of this Tuple6.
22+
* @property a6 the 6th component of this Tuple6.
2323
*
2424
* @since 2.0.0
2525
*/

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ package ch.tutteli.kbox
1515
* @param A6 The type of the 6th component of this Tuple7.
1616
* @param A7 The type of the 7th component of this Tuple7.
1717
*
18-
* @param a1 the 1st component of this Tuple7.
19-
* @param a2 the 2nd component of this Tuple7.
20-
* @param a3 the 3rd component of this Tuple7.
21-
* @param a4 the 4th component of this Tuple7.
22-
* @param a5 the 5th component of this Tuple7.
23-
* @param a6 the 6th component of this Tuple7.
24-
* @param a7 the 7th component of this Tuple7.
18+
* @property a1 the 1st component of this Tuple7.
19+
* @property a2 the 2nd component of this Tuple7.
20+
* @property a3 the 3rd component of this Tuple7.
21+
* @property a4 the 4th component of this Tuple7.
22+
* @property a5 the 5th component of this Tuple7.
23+
* @property a6 the 6th component of this Tuple7.
24+
* @property a7 the 7th component of this Tuple7.
2525
*
2626
* @since 2.0.0
2727
*/

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ package ch.tutteli.kbox
1616
* @param A7 The type of the 7th component of this Tuple8.
1717
* @param A8 The type of the 8th component of this Tuple8.
1818
*
19-
* @param a1 the 1st component of this Tuple8.
20-
* @param a2 the 2nd component of this Tuple8.
21-
* @param a3 the 3rd component of this Tuple8.
22-
* @param a4 the 4th component of this Tuple8.
23-
* @param a5 the 5th component of this Tuple8.
24-
* @param a6 the 6th component of this Tuple8.
25-
* @param a7 the 7th component of this Tuple8.
26-
* @param a8 the 8th component of this Tuple8.
19+
* @property a1 the 1st component of this Tuple8.
20+
* @property a2 the 2nd component of this Tuple8.
21+
* @property a3 the 3rd component of this Tuple8.
22+
* @property a4 the 4th component of this Tuple8.
23+
* @property a5 the 5th component of this Tuple8.
24+
* @property a6 the 6th component of this Tuple8.
25+
* @property a7 the 7th component of this Tuple8.
26+
* @property a8 the 8th component of this Tuple8.
2727
*
2828
* @since 2.0.0
2929
*/

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ package ch.tutteli.kbox
1717
* @param A8 The type of the 8th component of this Tuple9.
1818
* @param A9 The type of the 9th component of this Tuple9.
1919
*
20-
* @param a1 the 1st component of this Tuple9.
21-
* @param a2 the 2nd component of this Tuple9.
22-
* @param a3 the 3rd component of this Tuple9.
23-
* @param a4 the 4th component of this Tuple9.
24-
* @param a5 the 5th component of this Tuple9.
25-
* @param a6 the 6th component of this Tuple9.
26-
* @param a7 the 7th component of this Tuple9.
27-
* @param a8 the 8th component of this Tuple9.
28-
* @param a9 the 9th component of this Tuple9.
20+
* @property a1 the 1st component of this Tuple9.
21+
* @property a2 the 2nd component of this Tuple9.
22+
* @property a3 the 3rd component of this Tuple9.
23+
* @property a4 the 4th component of this Tuple9.
24+
* @property a5 the 5th component of this Tuple9.
25+
* @property a6 the 6th component of this Tuple9.
26+
* @property a7 the 7th component of this Tuple9.
27+
* @property a8 the 8th component of this Tuple9.
28+
* @property a9 the 9th component of this Tuple9.
2929
*
3030
* @since 2.0.0
3131
*/

0 commit comments

Comments
 (0)