lib: refactor, txnTieKnot automatically

This commit is contained in:
Simon Michael 2018-07-31 08:57:46 +01:00
parent 572f5a4b31
commit bb36693155
3 changed files with 13 additions and 13 deletions

View File

@ -21,6 +21,7 @@ import Data.Time.Calendar
import Hledger.Data.Types
import Hledger.Data.Dates
import Hledger.Data.Amount
import Hledger.Data.Transaction
import Hledger.Query
import Hledger.Utils.UTF8IOCompat (error')
-- import Hledger.Utils.Debug
@ -32,10 +33,12 @@ import Hledger.Utils.UTF8IOCompat (error')
-- >>> import Hledger.Data.Journal
-- | Converts a 'TransactionModifier' and a 'Query' to a
-- 'Transaction'-transforming function. The query allows injection of
-- additional restrictions on which postings to modify.
-- The transformer function will not call 'txnTieKnot', you will
-- probably want to call that after using it.
-- 'Transaction'-transforming function, which applies the modification(s)
-- specified by the TransactionModifier. Currently this means adding automated
-- postings when certain other postings - specified by the TransactionModifier,
-- and additionally limited by the extra query, if it's not 'Any' - are present.
-- The postings of the transformed transaction will reference it, as usual
-- ('txnTieKnot').
--
-- >>> transactionModifierToFunction Any (TransactionModifier "" ["pong" `post` usd 2]) nulltransaction{tpostings=["ping" `post` usd 1]}
-- 0000/01/01
@ -61,7 +64,7 @@ import Hledger.Utils.UTF8IOCompat (error')
-- <BLANKLINE>
transactionModifierToFunction :: Query -> TransactionModifier -> (Transaction -> Transaction)
transactionModifierToFunction q mt =
\t@(tpostings -> ps) -> t { tpostings = generatePostings ps } -- TODO add modifier txn comment/tags ?
\t@(tpostings -> ps) -> txnTieKnot t{ tpostings=generatePostings ps } -- TODO add modifier txn comment/tags ?
where
q' = simplifyQuery $ And [q, tmParseQuery mt (error' "a transaction modifier's query cannot depend on current date")]
mods = map tmPostingToFunction $ tmpostings mt

View File

@ -206,11 +206,10 @@ journalSourcePos p p' = JournalSourcePos (sourceName p) (fromIntegral . unPos $
-- | Generate Automatic postings and add them to the current journal.
generateAutomaticPostings :: Journal -> Journal
generateAutomaticPostings j = j { jtxns = map modifier $ jtxns j }
generateAutomaticPostings j = j { jtxns = map applyallmodifiers $ jtxns j }
where
modifier = foldr (flip (.) . transactionModifierToFunction') id mtxns
transactionModifierToFunction' = fmap txnTieKnot . transactionModifierToFunction Q.Any
mtxns = jtxnmodifiers j
applyallmodifiers =
foldr (flip (.) . transactionModifierToFunction Q.Any) id (jtxnmodifiers j)
-- | Given a megaparsec ParsedJournal parser, input options, file
-- path and file content: parse and post-process a Journal, or give an error.

View File

@ -181,11 +181,9 @@ rewrite opts@CliOpts{rawopts_=rawopts,reportopts_=ropts} j@Journal{jtxns=ts} = d
modifier <- transactionModifierFromOpts rawopts
-- create re-writer
let modifiers = modifier : jtxnmodifiers j
-- Note that some query matches require transaction. Thus modifiers
-- pipeline should include txnTieKnot on every step.
modifier' = foldr (flip (.) . fmap txnTieKnot . transactionModifierToFunction q) id modifiers
applyallmodifiers = foldr (flip (.) . transactionModifierToFunction q) id modifiers
-- rewrite matched transactions
let j' = j{jtxns=map modifier' ts}
let j' = j{jtxns=map applyallmodifiers ts}
-- run the print command, showing all transactions
outputFromOpts rawopts opts{reportopts_=ropts{query_=""}} j j'