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:
		
							parent
							
								
									0f2a31bea7
								
							
						
					
					
						commit
						a6a73e36e3
					
				| @ -21,10 +21,8 @@ module Hledger.Data.Journal ( | |||||||
|   commodityStylesFromAmounts, |   commodityStylesFromAmounts, | ||||||
|   journalCommodityStyles, |   journalCommodityStyles, | ||||||
|   journalConvertAmountsToCost, |   journalConvertAmountsToCost, | ||||||
|   journalFinalise, |  | ||||||
|   journalReverse, |   journalReverse, | ||||||
|   journalSetTime, |   journalSetLastReadTime, | ||||||
|   journalSetFilePath, |  | ||||||
|   journalPivot, |   journalPivot, | ||||||
|   -- * Filtering |   -- * Filtering | ||||||
|   filterJournalTransactions, |   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 :: Journal -> Journal | ||||||
| journalReverse j = | journalReverse j = | ||||||
|   j {jfiles            = reverse $ jfiles j |   j {jfiles            = reverse $ jfiles j | ||||||
| @ -532,31 +533,9 @@ journalReverse j = | |||||||
|     ,jmarketprices     = reverse $ jmarketprices j |     ,jmarketprices     = reverse $ jmarketprices j | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| journalSetTime :: ClockTime -> Journal -> Journal | -- | Set this journal's last read time, ie when its files were last read. | ||||||
| journalSetTime t j = j{ jlastreadtime = t } | journalSetLastReadTime :: ClockTime -> Journal -> Journal | ||||||
| 
 | journalSetLastReadTime 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') |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| journalNumberAndTieTransactions = journalTieTransactions . journalNumberTransactions | journalNumberAndTieTransactions = journalTieTransactions . journalNumberTransactions | ||||||
|  | |||||||
| @ -262,13 +262,21 @@ parseAndFinaliseJournal parser iopts f txt = do | |||||||
|         -- be false pending modifiers) and we don't reorder the second |         -- be false pending modifiers) and we don't reorder the second | ||||||
|         -- time. If we are only running once, we reorder and follow |         -- time. If we are only running once, we reorder and follow | ||||||
|         -- the options for checking assertions. |         -- the options for checking assertions. | ||||||
|         let runFin :: Bool -> Bool -> (ParsedJournal -> Either String Journal) |         let fj = if auto_ iopts && (not . null . jtxnmodifiers) pj | ||||||
|             runFin reorder ignore = journalFinalise t f txt reorder ignore |  | ||||||
|             fj = if auto_ iopts && (not . null . jtxnmodifiers) pj |  | ||||||
|                  then applyTransactionModifiers <$> |                  then applyTransactionModifiers <$> | ||||||
|                       runFin True False pj >>= |                       (journalBalanceTransactions False $ | ||||||
|                       runFin False (not $ ignore_assertions_ iopts) |                        journalReverse $ | ||||||
|                  else runFin True (not $ ignore_assertions_ iopts) pj |                        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 |         in | ||||||
|           case fj of |           case fj of | ||||||
|             Right j -> return j |             Right j -> return j | ||||||
| @ -299,14 +307,14 @@ parseAndFinaliseJournal' parser iopts f txt = do | |||||||
|                      journalReverse $ |                      journalReverse $ | ||||||
|                      journalApplyCommodityStyles pj) >>= |                      journalApplyCommodityStyles pj) >>= | ||||||
|                     (\j -> journalBalanceTransactions (not $ ignore_assertions_ iopts) $ |                     (\j -> journalBalanceTransactions (not $ ignore_assertions_ iopts) $ | ||||||
|                            journalSetTime t $ |                            journalAddFile (f, txt) $ | ||||||
|                            journalSetFilePath f txt $ |                            journalSetLastReadTime t $ | ||||||
|                            j) |                            j) | ||||||
|                else journalBalanceTransactions (not $ ignore_assertions_ iopts) $ |                else journalBalanceTransactions (not $ ignore_assertions_ iopts) $ | ||||||
|                     journalReverse $ |                     journalReverse $ | ||||||
|  |                     journalAddFile (f, txt) $ | ||||||
|                     journalApplyCommodityStyles $ |                     journalApplyCommodityStyles $ | ||||||
|                     journalSetTime t $ |                     journalSetLastReadTime t $ | ||||||
|                     journalSetFilePath f txt $ |  | ||||||
|                     pj |                     pj | ||||||
|       in |       in | ||||||
|         case fj of |         case fj of | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user