gnunet-svn
[Top][All Lists]
Advanced

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

[libeufin] branch master updated: avoid Float to represent money.


From: gnunet
Subject: [libeufin] branch master updated: avoid Float to represent money.
Date: Wed, 04 Dec 2019 23:17:34 +0100

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

marcello pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new d943e18  avoid Float to represent money.
d943e18 is described below

commit d943e1820abe29b7efbd2a8e6e66f0eb4d6504cd
Author: Marcello Stanisci <address@hidden>
AuthorDate: Wed Dec 4 23:16:59 2019 +0100

    avoid Float to represent money.
---
 .../src/main/kotlin/tech/libeufin/sandbox/DB.kt    | 38 ++++++++++++++++++++--
 .../src/main/kotlin/tech/libeufin/sandbox/Main.kt  | 11 ++++---
 sandbox/src/main/python/libeufin-cli               |  1 -
 sandbox/src/test/kotlin/DbTest.kt                  | 26 +++++++--------
 4 files changed, 53 insertions(+), 23 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
index e10e1e2..4b7002d 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
@@ -32,6 +32,8 @@ const val EBICS_HOST_ID_MAX_LENGTH = 10
 const val EBICS_USER_ID_MAX_LENGTH = 10
 const val EBICS_PARTNER_ID_MAX_LENGTH = 10
 const val EBICS_SYSTEM_ID_MAX_LENGTH = 10
+const val MAX_ID_LENGTH = 21 // enough to contain IBANs
+const val MAX_SUBJECT_LENGTH = 140 // okay?
 
 /**
  * All the states to give a subscriber.
@@ -90,6 +92,39 @@ fun Blob.toByteArray(): ByteArray {
     return this.binaryStream.readAllBytes()
 }
 
+object BankTransactionsTable : IntIdTable() {
+
+    /* Using varchar to store the IBAN - or possibly other formats
+     * - from the counterpart.  */
+    val counterpart = varchar("counterpart", MAX_ID_LENGTH)
+    val amountSign = integer("amountSign").check { (it eq 1) or (it eq -1)}
+    val amountValue = integer("amountValue").check { GreaterEqOp(it, 
intParam(0)) }
+    val amountFraction = integer("amountFraction").check { LessOp(it, 
intParam(100)) }
+    val subject = varchar("subject", MAX_SUBJECT_LENGTH)
+    val date = date("date")
+    val localCustomer = reference("localCustomer", BankCustomersTable)
+}
+
+class BankTransactionEntity(id: EntityID<Int>) : IntEntity(id) {
+
+    companion object : 
IntEntityClass<BankTransactionEntity>(BankTransactionsTable)
+
+    /* the id of the local customer involved in this transaction,
+    * either as the credit or the debit part; makes lookups easier */
+    var localCustomer by BankCustomerEntity referencedOn 
BankTransactionsTable.localCustomer
+
+    /* keeping as strings, as to allow hosting IBANs and/or other
+    * unobvious formats.  */
+    var counterpart by BankTransactionsTable.counterpart
+
+    var subject by BankTransactionsTable.subject
+    var date by BankTransactionsTable.date
+
+    var amountValue by BankTransactionsTable.amountValue
+    var amountFraction by BankTransactionsTable.amountFraction
+    var amountSign by BankTransactionsTable.amountSign
+}
+
 /**
  * This table information *not* related to EBICS, for all
  * its customers.
@@ -97,15 +132,14 @@ fun Blob.toByteArray(): ByteArray {
 object BankCustomersTable : IntIdTable() {
     // Customer ID is the default 'id' field provided by the constructor.
     val name = varchar("name", CUSTOMER_NAME_MAX_LENGTH).primaryKey()
-    val balance = float("balance")
 }
 
 class BankCustomerEntity(id: EntityID<Int>) : IntEntity(id) {
     companion object : IntEntityClass<BankCustomerEntity>(BankCustomersTable)
     var name by BankCustomersTable.name
-    var balance by BankCustomersTable.balance
 }
 
+
 /**
  * This table stores RSA public keys of subscribers.
  */
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
index 0426d08..74a0745 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -130,7 +130,6 @@ fun main() {
 
         val customerEntity = BankCustomerEntity.new {
             name = "Mina"
-            balance = 0.toFloat()
             }
 
 
@@ -168,17 +167,19 @@ fun main() {
         routing {
 
             get("/{id}/balance") {
-                val (name, balanceFloat) = transaction {
-                    val tmp = findCustomer(call.parameters["id"])
-                    Pair(tmp.name, tmp.balance)
+                val (name, balance) = transaction {
+                    val tmp: BankCustomerEntity = 
findCustomer(call.parameters["id"])
+                    Pair(tmp.name, 0) // fixme/todo!
                 }
+
                 call.respond(
                     CustomerBalance(
                     name = name,
-                    balance = "EUR:${balanceFloat}"
+                    balance = "EUR:${balance}"
                     )
                 )
             }
+
             get("/") {
                 call.respondText("Hello LibEuFin!\n", ContentType.Text.Plain)
             }
diff --git a/sandbox/src/main/python/libeufin-cli 
b/sandbox/src/main/python/libeufin-cli
index 18bae3f..3a00644 100755
--- a/sandbox/src/main/python/libeufin-cli
+++ b/sandbox/src/main/python/libeufin-cli
@@ -28,7 +28,6 @@ def cli(ctx, base_url, bank_base_url):
 def ebics():
     pass
 
-
 @ebics.command(help="Restore private keys backup.")
 @click.pass_obj
 @click.option(
diff --git a/sandbox/src/test/kotlin/DbTest.kt 
b/sandbox/src/test/kotlin/DbTest.kt
index 1744e33..681ac9d 100644
--- a/sandbox/src/test/kotlin/DbTest.kt
+++ b/sandbox/src/test/kotlin/DbTest.kt
@@ -4,6 +4,7 @@ import org.jetbrains.exposed.exceptions.ExposedSQLException
 import org.jetbrains.exposed.sql.Database
 import org.jetbrains.exposed.sql.SchemaUtils
 import org.jetbrains.exposed.sql.transactions.transaction
+import org.joda.time.DateTime
 import org.junit.Test
 import kotlin.test.assertFailsWith
 import kotlin.test.assertTrue
@@ -17,27 +18,22 @@ class DbTest {
 
         transaction {
 
+            SchemaUtils.create(BankTransactionsTable)
             SchemaUtils.create(BankCustomersTable)
-            SchemaUtils.create(EbicsSubscribersTable)
-            SchemaUtils.create(EbicsSubscriberPublicKeysTable)
 
             val customer = BankCustomerEntity.new {
-                name = "username"
-                balance = Float.MIN_VALUE
+                name = "employee"
             }
 
-            val row = EbicsSubscriberEntity.new {
-                userId = "user id"
-                partnerId = "partner id"
-                nextOrderID = 0
-                state = SubscriberState.NEW
-                bankCustomer = customer
+            val ledgerEntry = BankTransactionEntity.new {
+                amountSign = 1
+                amountValue = 5
+                amountFraction = 0
+                counterpart = "IBAN"
+                subject = "Salary"
+                date = DateTime.now()
+                localCustomer = customer
             }
-
-            customer.balance = 100.toFloat()
-
-            logger.info("${row.bankCustomer.balance}")
-            assertTrue(row.bankCustomer.balance.equals(100.toFloat()))
         }
     }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

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