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.Types | ||||||
| import Hledger.Data.Dates | import Hledger.Data.Dates | ||||||
| import Hledger.Data.Amount | import Hledger.Data.Amount | ||||||
|  | import Hledger.Data.Transaction | ||||||
| import Hledger.Query | import Hledger.Query | ||||||
| import Hledger.Utils.UTF8IOCompat (error') | import Hledger.Utils.UTF8IOCompat (error') | ||||||
| -- import Hledger.Utils.Debug | -- import Hledger.Utils.Debug | ||||||
| @ -32,10 +33,12 @@ import Hledger.Utils.UTF8IOCompat (error') | |||||||
| -- >>> import Hledger.Data.Journal | -- >>> import Hledger.Data.Journal | ||||||
| 
 | 
 | ||||||
| -- | Converts a 'TransactionModifier' and a 'Query' to a  | -- | Converts a 'TransactionModifier' and a 'Query' to a  | ||||||
| -- 'Transaction'-transforming function. The query allows injection of | -- 'Transaction'-transforming function, which applies the modification(s) | ||||||
| -- additional restrictions on which postings to modify.  | -- specified by the TransactionModifier. Currently this means adding automated | ||||||
| -- The transformer function will not call 'txnTieKnot', you will | -- postings when certain other postings - specified by the TransactionModifier, | ||||||
| -- probably want to call that after using it. | -- 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]} | -- >>> transactionModifierToFunction Any (TransactionModifier "" ["pong" `post` usd 2]) nulltransaction{tpostings=["ping" `post` usd 1]} | ||||||
| -- 0000/01/01 | -- 0000/01/01 | ||||||
| @ -61,7 +64,7 @@ import Hledger.Utils.UTF8IOCompat (error') | |||||||
| -- <BLANKLINE> | -- <BLANKLINE> | ||||||
| transactionModifierToFunction :: Query -> TransactionModifier -> (Transaction -> Transaction) | transactionModifierToFunction :: Query -> TransactionModifier -> (Transaction -> Transaction) | ||||||
| transactionModifierToFunction q mt =  | 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 |   where | ||||||
|     q' = simplifyQuery $ And [q, tmParseQuery mt (error' "a transaction modifier's query cannot depend on current date")] |     q' = simplifyQuery $ And [q, tmParseQuery mt (error' "a transaction modifier's query cannot depend on current date")] | ||||||
|     mods = map tmPostingToFunction $ tmpostings mt |     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. | -- | Generate Automatic postings and add them to the current journal. | ||||||
| generateAutomaticPostings :: Journal -> Journal | generateAutomaticPostings :: Journal -> Journal | ||||||
| generateAutomaticPostings j = j { jtxns = map modifier $ jtxns j } | generateAutomaticPostings j = j { jtxns = map applyallmodifiers $ jtxns j } | ||||||
|   where |   where | ||||||
|     modifier = foldr (flip (.) . transactionModifierToFunction') id mtxns |     applyallmodifiers =  | ||||||
|     transactionModifierToFunction' = fmap txnTieKnot . transactionModifierToFunction Q.Any |       foldr (flip (.) . transactionModifierToFunction Q.Any) id (jtxnmodifiers j) | ||||||
|     mtxns = jtxnmodifiers j |  | ||||||
| 
 | 
 | ||||||
| -- | Given a megaparsec ParsedJournal parser, input options, file | -- | Given a megaparsec ParsedJournal parser, input options, file | ||||||
| -- path and file content: parse and post-process a Journal, or give an error. | -- 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 |   modifier <- transactionModifierFromOpts rawopts | ||||||
|   -- create re-writer |   -- create re-writer | ||||||
|   let modifiers = modifier : jtxnmodifiers j |   let modifiers = modifier : jtxnmodifiers j | ||||||
|       -- Note that some query matches require transaction. Thus modifiers |       applyallmodifiers = foldr (flip (.) . transactionModifierToFunction q) id modifiers | ||||||
|       -- pipeline should include txnTieKnot on every step. |  | ||||||
|       modifier' = foldr (flip (.) . fmap txnTieKnot . transactionModifierToFunction q) id modifiers |  | ||||||
|   -- rewrite matched transactions |   -- rewrite matched transactions | ||||||
|   let j' = j{jtxns=map modifier' ts} |   let j' = j{jtxns=map applyallmodifiers ts} | ||||||
|   -- run the print command, showing all transactions |   -- run the print command, showing all transactions | ||||||
|   outputFromOpts rawopts opts{reportopts_=ropts{query_=""}} j j' |   outputFromOpts rawopts opts{reportopts_=ropts{query_=""}} j j' | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user