gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] 01/04: parse also signed decimals


From: gnunet
Subject: [libeufin] 01/04: parse also signed decimals
Date: Tue, 20 Dec 2022 17:29:40 +0100

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository libeufin.

commit bb46f75d6f0429d805d9d0b9d3bc23d9263c073f
Author: MS <ms@taler.net>
AuthorDate: Tue Dec 20 17:20:39 2022 +0100

    parse also signed decimals
---
 util/src/main/kotlin/DBTypes.kt | 4 ----
 util/src/main/kotlin/amounts.kt | 8 +++++---
 util/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
index ec3dc505..c38e63cf 100644
--- 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
index 5528a35b..1d157de6 100644
--- 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
index 85e09cb2..3b94b517 100644
--- 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",

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]