com.ancientlightstudios.quarkus.kotlin.openapi.PlainDeserializationExtensions.kt Maven / Gradle / Ivy
package com.ancientlightstudios.quarkus.kotlin.openapi
import java.math.BigDecimal
import java.math.BigInteger
import java.util.*
@Suppress("unused")
fun Maybe.emptyStringAsNull(): Maybe = onNotNull {
if (value.isEmpty()) {
success(null)
} else {
this@emptyStringAsNull
}
}
@Suppress("unused")
fun Maybe.emptyArrayAsNull(): Maybe = onNotNull {
if (value.isEmpty()) {
success(null)
} else {
this@emptyArrayAsNull
}
}
@Suppress("unused")
fun Maybe.asString(): Maybe = this
@Suppress("unused")
fun Maybe.asInt(): Maybe = this.mapNotNull("is not an int") { it.toInt() }
@Suppress("unused")
fun Maybe.asUInt(): Maybe = this.mapNotNull("is not an uint") { it.toUInt() }
@Suppress("unused")
fun Maybe.asLong(): Maybe = this.mapNotNull("is not a long") { it.toLong() }
@Suppress("unused")
fun Maybe.asULong(): Maybe = this.mapNotNull("is not an ulong") { it.toULong() }
@Suppress("unused")
fun Maybe.asBigInteger(): Maybe = this.mapNotNull("is not a valid integer") { it.toBigInteger() }
@Suppress("unused")
fun Maybe.asFloat(): Maybe = this.mapNotNull("is not a float") { it.toFloat() }
@Suppress("unused")
fun Maybe.asDouble(): Maybe = this.mapNotNull("is not a double") { it.toDouble() }
fun Maybe.asBigDecimal(): Maybe = this.mapNotNull("is not a valid decimal") { it.toBigDecimal() }
@Suppress("unused")
fun Maybe.asBoolean(): Maybe = this.mapNotNull("is not a boolean") {
it.toBooleanStrict()
when (val value = it.lowercase()) {
"true" -> true
"false" -> false
else -> throw IllegalArgumentException()
}
}
@Suppress("unused")
fun Maybe.asByteArray(): Maybe = this.mapNotNull("is not a valid base64 encoded byte array") {
Base64.getDecoder().decode(it)
}