libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit bb46f75d6f0429d805d9d0b9d3bc23d9263c073f
parent 51ce46aefe50265f625a0140adc602e012d6745c
Author: MS <ms@taler.net>
Date:   Tue, 20 Dec 2022 17:20:39 +0100

parse also signed decimals

Diffstat:
Mutil/src/main/kotlin/DBTypes.kt | 4----
Mutil/src/main/kotlin/amounts.kt | 8+++++---
Mutil/src/main/kotlin/strings.kt | 2+-
3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/util/src/main/kotlin/DBTypes.kt b/util/src/main/kotlin/DBTypes.kt @@ -28,10 +28,6 @@ import java.math.RoundingMode const val SCALE_TWO = 2 const val NUMBER_MAX_DIGITS = 20 class BadAmount(badValue: Any?) : Exception("Value '${badValue}' is not a valid amount") - -/** - * Any number can become an Amount IF it does NOT need to be rounded to comply to the scale == 2. - */ typealias Amount = BigDecimal class AmountColumnType : ColumnType() { diff --git a/util/src/main/kotlin/amounts.kt b/util/src/main/kotlin/amounts.kt @@ -23,8 +23,10 @@ import io.ktor.http.* */ val re = Regex("^([0-9]+(\\.[0-9]+)?)$") +val reWithSign = Regex("^-?([0-9]+(\\.[0-9]+)?)$") -fun validatePlainAmount(plainAmount: String): Boolean { +fun validatePlainAmount(plainAmount: String, withSign: Boolean = false): Boolean { + if (withSign) return reWithSign.matches(plainAmount) return re.matches(plainAmount) } @@ -44,8 +46,8 @@ fun parseAmountAsString(amount: String): Pair<String, String?> { } fun parseAmount(amount: String): AmountWithCurrency { - val match = Regex("([A-Z]+):([0-9]+(\\.[0-9]+)?)").find(amount) ?: throw - UtilError(HttpStatusCode.BadRequest, "invalid amount: $amount") + val match = Regex("([A-Z]+):([0-9]+(\\.[0-9]+)?)").find(amount) ?: + throw UtilError(HttpStatusCode.BadRequest, "invalid amount: $amount") val (currency, number) = match.destructured return AmountWithCurrency(currency, Amount(number)) } diff --git a/util/src/main/kotlin/strings.kt b/util/src/main/kotlin/strings.kt @@ -104,7 +104,7 @@ data class AmountWithCurrency( ) fun parseDecimal(decimalStr: String): BigDecimal { - if(!validatePlainAmount(decimalStr)) + if(!validatePlainAmount(decimalStr, withSign = true)) throw UtilError( HttpStatusCode.BadRequest, "Bad string amount given: $decimalStr",