From bb366931558f85cc900009d45a53141d165d0742 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 31 Jul 2018 08:57:46 +0100 Subject: [PATCH] lib: refactor, txnTieKnot automatically --- hledger-lib/Hledger/Data/TransactionModifier.hs | 13 ++++++++----- hledger-lib/Hledger/Read/Common.hs | 7 +++---- hledger/Hledger/Cli/Commands/Rewrite.hs | 6 ++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/hledger-lib/Hledger/Data/TransactionModifier.hs b/hledger-lib/Hledger/Data/TransactionModifier.hs index 22f31dee5..a15d53b83 100644 --- a/hledger-lib/Hledger/Data/TransactionModifier.hs +++ b/hledger-lib/Hledger/Data/TransactionModifier.hs @@ -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') -- 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 diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 739356bd5..28fcb2a92 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -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. diff --git a/hledger/Hledger/Cli/Commands/Rewrite.hs b/hledger/Hledger/Cli/Commands/Rewrite.hs index a417a2eb0..a07a7e3b8 100755 --- a/hledger/Hledger/Cli/Commands/Rewrite.hs +++ b/hledger/Hledger/Cli/Commands/Rewrite.hs @@ -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'