From 53b3e2bd94ef8d08a40376485e9942f57d09dfdd Mon Sep 17 00:00:00 2001 From: Jesse Rosenthal Date: Fri, 12 Oct 2018 10:01:39 -0400 Subject: [PATCH] journal: split up the parts of journalFinalise, and use them as needed. `journalFinalise` is only used in the `parseAndFinaliseJournal` functions, but it needs to be run differently at different stages when transaction modifiers are applied. This change breaks it into smaller functions, and uses those smaller parts in `parseAndFinaliseJournal` as needed. --- hledger-lib/Hledger/Data/Journal.hs | 20 ++++++++++++++++++++ hledger-lib/Hledger/Read/Common.hs | 20 ++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 7fa18490a..95193f325 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -22,6 +22,9 @@ module Hledger.Data.Journal ( journalCommodityStyles, journalConvertAmountsToCost, journalFinalise, + journalReverse, + journalSetTime, + journalSetFilePath, journalPivot, -- * Filtering filterJournalTransactions, @@ -519,6 +522,23 @@ filterJournalTransactionsByAccount apats j@Journal{jtxns=ts} = j{jtxns=filter tm -} +journalReverse :: Journal -> Journal +journalReverse j = + j {jfiles = reverse $ jfiles j + ,jdeclaredaccounts = reverse $ jdeclaredaccounts j + ,jtxns = reverse $ jtxns j + ,jtxnmodifiers = reverse $ jtxnmodifiers j + ,jperiodictxns = reverse $ jperiodictxns j + ,jmarketprices = reverse $ jmarketprices j + } + +journalSetTime :: ClockTime -> Journal -> Journal +journalSetTime t j = j{ jlastreadtime = t } + +journalSetFilePath :: FilePath -> Text -> Journal -> Journal +journalSetFilePath path txt j = j {jfiles = (path,txt) : jfiles j} + + -- | Do post-parse processing on a parsed journal to make it ready for -- use. Reverse parsed data to normal order, standardise amount -- formats, check/ensure that transactions are balanced, and maybe diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 00286c4f5..6df8890c4 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -293,13 +293,21 @@ parseAndFinaliseJournal' parser iopts f txt = do -- be false pending modifiers) and we don't reorder the second -- time. If we are only running once, we reorder and follow the -- options for checking assertions. - let runFin :: Bool -> Bool -> (ParsedJournal -> Either String Journal) - runFin reorder ignore = journalFinalise t f txt reorder ignore - fj = if auto_ iopts && (not . null . jtxnmodifiers) pj + let fj = if auto_ iopts && (not . null . jtxnmodifiers) pj then applyTransactionModifiers <$> - runFin True False pj >>= - runFin False (not $ ignore_assertions_ iopts) - else runFin True (not $ ignore_assertions_ iopts) pj + (journalBalanceTransactions False $ + journalReverse $ + journalApplyCommodityStyles pj) >>= + (\j -> journalBalanceTransactions (not $ ignore_assertions_ iopts) $ + journalSetTime t $ + journalSetFilePath f txt $ + j) + else journalBalanceTransactions (not $ ignore_assertions_ iopts) $ + journalReverse $ + journalApplyCommodityStyles $ + journalSetTime t $ + journalSetFilePath f txt $ + pj in case fj of Right j -> return j