Skip to content

Commit 65da441

Browse files
committed
Simple case "then" functions can be infix
1 parent ecf4bc3 commit 65da441

File tree

2 files changed

+25
-45
lines changed

2 files changed

+25
-45
lines changed

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/CaseDSLs.kt

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,66 +66,59 @@ class KSimpleCaseDSL<T : Any> : KElseDSL {
6666
}
6767
internal val whenConditions = mutableListOf<SimpleCaseWhenCondition<T>>()
6868

69-
fun `when`(firstCondition: VisitableCondition<T>, vararg subsequentConditions: VisitableCondition<T>,
70-
completer: SimpleCaseThenGatherer.() -> Unit) =
71-
SimpleCaseThenGatherer().apply(completer).run {
69+
fun `when`(firstCondition: VisitableCondition<T>, vararg subsequentConditions: VisitableCondition<T>) =
70+
SimpleCaseThenGatherer {
7271
val allConditions = buildList {
7372
add(firstCondition)
7473
addAll(subsequentConditions)
7574
}
7675

77-
whenConditions.add(ConditionBasedWhenCondition(allConditions, thenValue))
76+
whenConditions.add(ConditionBasedWhenCondition(allConditions, it))
7877
}
7978

80-
fun `when`(firstValue: T, vararg subsequentValues: T, completer: SimpleCaseThenGatherer.() -> Unit) =
81-
SimpleCaseThenGatherer().apply(completer).run {
79+
fun `when`(firstValue: T, vararg subsequentValues: T) =
80+
SimpleCaseThenGatherer {
8281
val allConditions = buildList {
8382
add(firstValue)
8483
addAll(subsequentValues)
8584
}
8685

87-
whenConditions.add(BasicWhenCondition(allConditions, thenValue))
86+
whenConditions.add(BasicWhenCondition(allConditions, it))
8887
}
8988

9089
override fun `else`(column: BasicColumn) {
9190
this.elseValue = column
9291
}
9392
}
9493

95-
class SimpleCaseThenGatherer: KThenDSL {
96-
internal var thenValue: BasicColumn? = null
97-
private set(value) {
98-
assertNull(field, "ERROR.41") //$NON-NLS-1$
99-
field = value
100-
}
101-
102-
override fun then(column: BasicColumn) {
103-
thenValue = column
94+
class SimpleCaseThenGatherer(private val consumer: (BasicColumn) -> Unit): KThenDSL {
95+
override infix fun then(column: BasicColumn) {
96+
consumer.invoke(column)
10497
}
10598
}
10699

107100
interface KThenDSL {
108-
fun then(value: String) {
101+
infix fun then(value: String) {
109102
then(stringConstant(value))
110103
}
111104

112-
fun then(value: Boolean) {
105+
infix fun then(value: Boolean) {
113106
then(constant<String>(value.toString()))
114107
}
115108

116-
fun then(value: Int) {
109+
infix fun then(value: Int) {
117110
then(constant<String>(value.toString()))
118111
}
119112

120-
fun then(value: Long) {
113+
infix fun then(value: Long) {
121114
then(constant<String>(value.toString()))
122115
}
123116

124-
fun then(value: Double) {
117+
infix fun then(value: Double) {
125118
then(constant<String>(value.toString()))
126119
}
127120

128-
fun then(column: BasicColumn)
121+
infix fun then(column: BasicColumn)
129122
}
130123

131124
interface KElseDSL {

src/test/kotlin/examples/kotlin/animal/data/KCaseExpressionTest.kt

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class KCaseExpressionTest {
457457
val selectStatement = select(
458458
animalName,
459459
case(animalName) {
460-
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
460+
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) then "yes"
461461
`else`("no")
462462
} `as` "IsAFox"
463463
) {
@@ -511,7 +511,7 @@ class KCaseExpressionTest {
511511
val selectStatement = select(
512512
animalName,
513513
case(animalName) {
514-
`when` ("Artic fox", "Red fox") { then("yes") }
514+
`when` ("Artic fox", "Red fox") then "yes"
515515
`else`("no")
516516
} `as` "IsAFox"
517517
) {
@@ -565,7 +565,7 @@ class KCaseExpressionTest {
565565
val selectStatement = select(
566566
animalName,
567567
case(animalName) {
568-
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(true) }
568+
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) then true
569569
`else`(false)
570570
} `as` "IsAFox"
571571
) {
@@ -619,7 +619,7 @@ class KCaseExpressionTest {
619619
val selectStatement = select(
620620
animalName,
621621
case(animalName) {
622-
`when` ("Artic fox", "Red fox") { then(true) }
622+
`when` ("Artic fox", "Red fox") then true
623623
`else`(false)
624624
} `as` "IsAFox"
625625
) {
@@ -673,7 +673,7 @@ class KCaseExpressionTest {
673673
val selectStatement = select(
674674
animalName,
675675
case(animalName) {
676-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
676+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then "yes"
677677
} `as` "IsAFox"
678678
) {
679679
from(animalData)
@@ -720,7 +720,7 @@ class KCaseExpressionTest {
720720
val selectStatement = select(
721721
animalName,
722722
case(animalName) {
723-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(cast { "It's a fox" `as` "VARCHAR(30)" })}
723+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then cast { "It's a fox" `as` "VARCHAR(30)" }
724724
`else`("It's not a fox")
725725
} `as` "IsAFox"
726726
) {
@@ -763,7 +763,7 @@ class KCaseExpressionTest {
763763
val selectStatement = select(
764764
animalName,
765765
case(animalName) {
766-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1L) }
766+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then 1L
767767
`else`(2L)
768768
} `as` "IsAFox"
769769
) {
@@ -806,7 +806,7 @@ class KCaseExpressionTest {
806806
val selectStatement = select(
807807
animalName,
808808
case(animalName) {
809-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1.1) }
809+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then 1.1
810810
`else`(2.2)
811811
} `as` "IsAFox"
812812
) {
@@ -849,7 +849,7 @@ class KCaseExpressionTest {
849849
val selectStatement = select(
850850
animalName,
851851
case(animalName) {
852-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1.1) }
852+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then 1.1
853853
`else`(cast { 2.2 `as` "DOUBLE" })
854854
} `as` "IsAFox"
855855
) {
@@ -888,26 +888,13 @@ class KCaseExpressionTest {
888888
fun testInvalidDoubleElseSimple() {
889889
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
890890
case(animalName) {
891-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("'yes'") }
891+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then "'yes'"
892892
`else`("Fred")
893893
`else`("Wilma")
894894
}
895895
}.withMessage(Messages.getString("ERROR.42"))
896896
}
897897

898-
@Test
899-
fun testInvalidDoubleThenSimple() {
900-
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
901-
case(animalName) {
902-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) {
903-
then("'yes'")
904-
then("no")
905-
}
906-
`else`("Fred")
907-
}
908-
}.withMessage(Messages.getString("ERROR.41"))
909-
}
910-
911898
@Test
912899
fun testInvalidDoubleElseSearched() {
913900
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {

0 commit comments

Comments
 (0)