mirror of
https://github.com/simonmichael/hledger.git
synced 2024-11-09 00:15:48 +03:00
lib: refactor, txnTieKnot automatically
This commit is contained in:
parent
572f5a4b31
commit
bb36693155
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user