From 36599b5fb53734e93d828e30a33ae6978d4b6245 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Mon, 8 Feb 2021 14:50:31 +0700 Subject: [PATCH] Fixed and updated for advanced parse with json and add provider for exchange rate for money config --- .../kotlin/com/cubetiqs/money/MoneyConfig.kt | 32 +++++++++++++++++-- .../cubetiqs/money/MoneyExchangeProvider.kt | 3 +- src/test/kotlin/MoneyTests.kt | 23 ++++++++----- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt b/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt index 16b7ada..a9918cd 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt @@ -50,9 +50,14 @@ object MoneyConfig { val rates = config.split(getProperties().deliSplit) rates.map { i -> - val temp = i.split(getProperties().deliEqual) + val temp = i + // remove the quote from string + .replace("\"", "") + .split(getProperties().deliEqual) if (temp.size == 2) { - val currency = temp[0].toUpperCase().trim() + val currency = temp[0] + .toUpperCase() + .trim() val value = temp[1].toDouble() if (MoneyConfig.config.containsKey(currency)) { MoneyConfig.config.replace(currency, value) @@ -74,6 +79,29 @@ object MoneyConfig { } } + fun appendRate(provider: MoneyExchangeProvider) = apply { + val currency = provider.getCurrency() + val rate = provider.getRate() + this.appendRate(currency, rate) + } + + /** + * Json Format must be, example below + * + * { + * "USD": 1, + * "EUR": 0.99, + * "...": ... + * } + */ + fun fromJson(configJson: String, clearAllStates: Boolean = false) { + val transformValues = configJson + .removePrefix("{") + .removeSuffix("}") + + parse(transformValues, clearAllStates) + } + // all currencies with its rate fun getConfig() = config diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyExchangeProvider.kt b/src/main/kotlin/com/cubetiqs/money/MoneyExchangeProvider.kt index 96547b2..df953ab 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyExchangeProvider.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyExchangeProvider.kt @@ -1,5 +1,6 @@ package com.cubetiqs.money interface MoneyExchangeProvider { - fun getRate(currency: StdMoney.Currency): Double + fun getCurrency(): String + fun getRate(): Double } \ No newline at end of file diff --git a/src/test/kotlin/MoneyTests.kt b/src/test/kotlin/MoneyTests.kt index 7e2854b..951835c 100644 --- a/src/test/kotlin/MoneyTests.kt +++ b/src/test/kotlin/MoneyTests.kt @@ -12,16 +12,15 @@ class MoneyTests { // .setDeliSplit(',') // .build() - val properties = buildMoneyConfigProperties { - setDeliEqual(':') - setDeliSplit(',') - } - applyMoneyConfig { - setProperties(properties) + setProperties(buildMoneyConfigProperties { + setDeliEqual(':') + setDeliSplit(',') + }) // parse("USD:1,KHR:4000") - appendRate("usd", 1.0) - appendRate("khr", 4000.0) + // appendRate("usd", 1.0) + // appendRate("khr", 4000.0) + fromJson(MyBatchRates.getJsonRates()) } // Is valid for money config? @@ -33,4 +32,12 @@ class MoneyTests { // Is correct exchange? Assert.assertEquals(8000.0, moneyKhr.getMoneyValue(), 0.0) } + + object MyBatchRates { + fun getJsonRates(): String { + return """ + {"USD": 1.0,"KHR": 4000.0} + """.trimIndent() + } + } } \ No newline at end of file