switch

open fun <R : Any, T> switch(vararg cases: ConditionalValueOperators.Case<R, T>, default: Value<R, T>? = null): Value<R, T>(source)
@JvmName(name = "switchByField")
open fun <R : Any, T> switch(vararg cases: ConditionalValueOperators.Case<R, T>, default: Field<R, T>): Value<R, T>(source)
@JvmName(name = "switchByProperty")
open fun <R : Any, T> switch(vararg cases: ConditionalValueOperators.Case<R, T>, default: KProperty1<R, T>): Value<R, T>(source)
inline fun <R : Any, T> switch(vararg cases: ConditionalValueOperators.Case<R, T>, default: T): Value<R, T>(source)

Selects one value based on multiple conditions.

Example

class User(
val name: String,
val score: Int,
val role: String,
val bonus: Int?,
)

users.updateManyWithPipeline {
set {
User::bonus set switch(
User::role eq "GUEST" then 5,
User::role eq "EMPLOYEE" then 6,
User::role eq "ADMIN" then 7,
default = -1,
)
}
}

External resources

See also

Specify a single condition.

Keyword to declare the different cases.