Finish removing journalFinalise (#903)

* journal: Get rid of `journalFinalise` and use granular functions

Complete the process started in 53b3e2bd. This gets rid of the
`journalFinalise` function and uses the smaller steps, in order to
have more granular control.

* journal: Change order of operations in finalization

We want to make sure that we add the filepath after the order is
reversed, so the added filepath is on the head and not the tail (as it
would be if it were reversed after it was added).

* journal: Refine granular finalization functions

This commit fixes two of the granular finalization functions:

1. Rename `journalSetTime` to `journalSetLastReadTime` and improve
   documentation.

2. Remove `journalSetFilePath`. It's redundant with `journalAddFile`
   currently in `Hledger.Read.Common`. The only difference between the
   functions is where the file is added (we keep the one in which it
   is added to the tail), so we change the position vis-a-vis
   reversal.
This commit is contained in:
Jesse Rosenthal 2018-10-16 11:51:51 -04:00 committed by Simon Michael
parent 0f2a31bea7
commit a6a73e36e3
2 changed files with 25 additions and 38 deletions

View File

@ -21,10 +21,8 @@ module Hledger.Data.Journal (
commodityStylesFromAmounts,
journalCommodityStyles,
journalConvertAmountsToCost,
journalFinalise,
journalReverse,
journalSetTime,
journalSetFilePath,
journalSetLastReadTime,
journalPivot,
-- * Filtering
filterJournalTransactions,
@ -522,6 +520,9 @@ filterJournalTransactionsByAccount apats j@Journal{jtxns=ts} = j{jtxns=filter tm
-}
-- | Reverse parsed data to normal order. This is used for post-parse
-- processing, since data is added to the head of the list during
-- parsing.
journalReverse :: Journal -> Journal
journalReverse j =
j {jfiles = reverse $ jfiles j
@ -532,31 +533,9 @@ journalReverse 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
-- check balance assertions.
journalFinalise :: ClockTime -> FilePath -> Text -> Bool -> Bool -> ParsedJournal -> Either String Journal
journalFinalise t path txt reorder assrt j@Journal{jfiles=fs} =
let j' = if reorder
then j {jfiles = (path,txt) : reverse fs
,jlastreadtime = t
,jdeclaredaccounts = reverse $ jdeclaredaccounts j
,jtxns = reverse $ jtxns j -- NOTE: see addTransaction
,jtxnmodifiers = reverse $ jtxnmodifiers j -- NOTE: see addTransactionModifier
,jperiodictxns = reverse $ jperiodictxns j -- NOTE: see addPeriodicTransaction
,jmarketprices = reverse $ jmarketprices j -- NOTE: see addMarketPrice
}
else j
in journalTieTransactions <$>
(journalBalanceTransactions assrt $ journalApplyCommodityStyles j')
-- | Set this journal's last read time, ie when its files were last read.
journalSetLastReadTime :: ClockTime -> Journal -> Journal
journalSetLastReadTime t j = j{ jlastreadtime = t }
journalNumberAndTieTransactions = journalTieTransactions . journalNumberTransactions

View File

@ -262,13 +262,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) $
journalAddFile (f, txt) $
journalSetLastReadTime t $
j)
else journalBalanceTransactions (not $ ignore_assertions_ iopts) $
journalReverse $
journalAddFile (f, txt) $
journalApplyCommodityStyles $
journalSetLastReadTime t $
pj
in
case fj of
Right j -> return j
@ -299,14 +307,14 @@ parseAndFinaliseJournal' parser iopts f txt = do
journalReverse $
journalApplyCommodityStyles pj) >>=
(\j -> journalBalanceTransactions (not $ ignore_assertions_ iopts) $
journalSetTime t $
journalSetFilePath f txt $
journalAddFile (f, txt) $
journalSetLastReadTime t $
j)
else journalBalanceTransactions (not $ ignore_assertions_ iopts) $
journalReverse $
journalAddFile (f, txt) $
journalApplyCommodityStyles $
journalSetTime t $
journalSetFilePath f txt $
journalSetLastReadTime t $
pj
in
case fj of