Skip to content

Commit 95713da

Browse files
committed
Else functions can be infix
Infix not as useful in this case because using it requires an explicit "this", but some might prefer that syntax.
1 parent d5afd69 commit 95713da

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ class KSearchedCaseDSL : KElseDSL {
3838
.withSubCriteria(subCriteria)
3939
.withThenValue(thenValue)
4040
.build())
41-
4241
}
4342

44-
override fun `else`(column: BasicColumn) {
43+
override infix fun `else`(column: BasicColumn) {
4544
this.elseValue = column
4645
}
4746
}
@@ -53,7 +52,7 @@ class SearchedCaseCriteriaCollector : GroupingCriteriaCollector(), KThenDSL {
5352
field = value
5453
}
5554

56-
override fun then(column: BasicColumn) {
55+
override infix fun then(column: BasicColumn) {
5756
thenValue = column
5857
}
5958
}
@@ -67,26 +66,26 @@ class KSimpleCaseDSL<T : Any> : KElseDSL {
6766
internal val whenConditions = mutableListOf<SimpleCaseWhenCondition<T>>()
6867

6968
fun `when`(firstCondition: VisitableCondition<T>, vararg subsequentConditions: VisitableCondition<T>) =
70-
SimpleCaseThenGatherer {
69+
SimpleCaseThenGatherer { thenValue ->
7170
val allConditions = buildList {
7271
add(firstCondition)
7372
addAll(subsequentConditions)
7473
}
7574

76-
whenConditions.add(ConditionBasedWhenCondition(allConditions, it))
75+
whenConditions.add(ConditionBasedWhenCondition(allConditions, thenValue))
7776
}
7877

7978
fun `when`(firstValue: T, vararg subsequentValues: T) =
80-
SimpleCaseThenGatherer {
79+
SimpleCaseThenGatherer { thenValue ->
8180
val allConditions = buildList {
8281
add(firstValue)
8382
addAll(subsequentValues)
8483
}
8584

86-
whenConditions.add(BasicWhenCondition(allConditions, it))
85+
whenConditions.add(BasicWhenCondition(allConditions, thenValue))
8786
}
8887

89-
override fun `else`(column: BasicColumn) {
88+
override infix fun `else`(column: BasicColumn) {
9089
this.elseValue = column
9190
}
9291
}
@@ -122,25 +121,25 @@ interface KThenDSL {
122121
}
123122

124123
interface KElseDSL {
125-
fun `else`(value: String) {
124+
infix fun `else`(value: String) {
126125
`else`(stringConstant(value))
127126
}
128127

129-
fun `else`(value: Boolean) {
128+
infix fun `else`(value: Boolean) {
130129
`else`(constant<String>(value.toString()))
131130
}
132131

133-
fun `else`(value: Int) {
132+
infix fun `else`(value: Int) {
134133
`else`(constant<String>(value.toString()))
135134
}
136135

137-
fun `else`(value: Long) {
136+
infix fun `else`(value: Long) {
138137
`else`(constant<String>(value.toString()))
139138
}
140139

141-
fun `else`(value: Double) {
140+
infix fun `else`(value: Double) {
142141
`else`(constant<String>(value.toString()))
143142
}
144143

145-
fun `else`(column: BasicColumn)
144+
infix fun `else`(column: BasicColumn)
146145
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ 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
}
@@ -901,9 +901,9 @@ class KCaseExpressionTest {
901901
case {
902902
`when` {
903903
id isEqualTo 22
904-
then("'yes'")
904+
this then "yes"
905905
}
906-
`else`("Fred")
906+
this `else` "Fred"
907907
`else`("Wilma")
908908
}
909909
}.withMessage(Messages.getString("ERROR.42"))
@@ -915,8 +915,8 @@ class KCaseExpressionTest {
915915
case {
916916
`when` {
917917
id isEqualTo 22
918-
then("'yes'")
919-
then("'no'")
918+
then("yes")
919+
then("no")
920920
}
921921
}
922922
}.withMessage(Messages.getString("ERROR.41"))

0 commit comments

Comments
 (0)