Skip to content

Commit b97578d

Browse files
committed
wip
1 parent d0d9da6 commit b97578d

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

pub/functora/src/money/Functora/Money.hs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Functora.Money
2020
fundsMoneyAmount,
2121
fundsCurrencyCode,
2222
unJsonRational,
23-
unJsonSignedMoney,
23+
unJsonMoney,
2424
unJsonUnsignedMoneyBOS,
2525
unJsonUnsignedMoneyGOL,
2626
CurrencyCode (..),
@@ -121,7 +121,7 @@ instance (TestEquality (Sing :: k -> Type)) => Eq (SomeMoney k tags) where
121121
deriving stock instance Show (SomeMoney k tags)
122122

123123
newMoney ::
124-
forall sig tags.
124+
forall tags sig.
125125
( MoneyTags sig tags
126126
) =>
127127
Ratio (MoneyRep sig) ->
@@ -232,10 +232,17 @@ deriving stock instance (MoneyTags sig tags) => Read (Funds tags)
232232
deriving stock instance (MoneyTags sig tags) => Data (Funds tags)
233233

234234
unJsonRational :: A.Decoder Rational
235-
unJsonRational = toRational <$> A.scientific
236-
237-
unJsonSignedMoney :: (MoneyTags 'Signed tags) => A.Decoder (Money tags)
238-
unJsonSignedMoney = Money <$> unJsonRational
235+
unJsonRational =
236+
toRational <$> A.scientific
237+
238+
unJsonMoney :: forall tags sig. (MoneyTags sig tags) => A.Decoder (Money tags)
239+
unJsonMoney = do
240+
rat <- unJsonRational
241+
case sing :: Sing sig of
242+
SSigned -> pure $ newMoney rat
243+
SUnsigned ->
244+
either (fail . inspect) (pure . newMoney)
245+
$ tryFrom @Rational @(Ratio Natural) rat
239246

240247
unJsonUnsignedMoneyBOS ::
241248
forall tags.

pub/functora/src/rates/Functora/Rates.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ tryFetchQuotesPerBase cur uri = tryMarket $ do
246246
createdAt <- A.at ["date"] A.day
247247
quotesMap <-
248248
A.at [fromString . from @Text @String $ unCurrencyCode cur]
249-
$ A.mapStrict unJsonSignedMoney
249+
$ A.mapStrict unJsonMoney
250250
pure
251251
QuotesPerBaseAt
252252
{ quotesPerBaseQuotesMap = Map.mapKeys CurrencyCode quotesMap,

pub/functora/src/sql/Functora/SqlOrphan.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ instance (MoneyTags sig tags) => PersistField (Money tags) where
8888
PersistRational x ->
8989
case sing :: Sing sig of
9090
SSigned ->
91-
pure $ newMoney @sig @tags x
91+
pure $ newMoney x
9292
SUnsigned ->
93-
bimap (const failure) (newMoney @sig @tags) $
93+
bimap (const failure) newMoney $
9494
tryFrom @Rational @(Ratio Natural) x
9595
_ ->
9696
Left failure

pub/functora/test/Functora/RatesSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ spec = do
6161
tryMarket
6262
$ getQuote
6363
( Funds
64-
(newMoney @'Signed 1)
64+
(newMoney 1)
6565
(CurrencyCode "btc")
6666
)
6767
quoteCurrency

0 commit comments

Comments
 (0)