From d82370d10b8fdbd59d6984aa5d1152815d9e1fe4 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Tue, 17 Apr 2018 23:58:53 +0100 Subject: [PATCH] lib, app, web: remove differences between command line and journal file aliases --- hledger-lib/Hledger/Data/Journal.hs | 14 ----------- hledger-lib/Hledger/Read.hs | 1 - hledger-lib/Hledger/Read/JournalReader.hs | 15 +++++++++--- hledger-ui/Hledger/UI/Main.hs | 1 - hledger-web/Hledger/Web/Main.hs | 1 - hledger/Hledger/Cli/CliOptions.hs | 6 ----- hledger/Hledger/Cli/Utils.hs | 3 +-- tests/misc/account-aliases.test | 30 +++++++++++++++++++++++ 8 files changed, 43 insertions(+), 28 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 8e62a0a52..0447fb472 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -16,7 +16,6 @@ module Hledger.Data.Journal ( addModifierTransaction, addPeriodicTransaction, addTransaction, - journalApplyAliases, journalBalanceTransactions, journalApplyCommodityStyles, commodityStylesFromAmounts, @@ -485,19 +484,6 @@ filterJournalTransactionsByAccount apats j@Journal{jtxns=ts} = j{jtxns=filter tm -} --- | Apply additional account aliases (eg from the command-line) to all postings in a journal. -journalApplyAliases :: [AccountAlias] -> Journal -> Journal -journalApplyAliases aliases j@Journal{jtxns=ts} = - -- (if null aliases - -- then id - -- else (dbgtrace $ - -- "applying additional command-line aliases:\n" - -- ++ chomp (unlines $ map (" "++) $ lines $ ppShow aliases))) $ - j{jtxns=map dotransaction ts} - where - dotransaction t@Transaction{tpostings=ps} = t{tpostings=map doposting ps} - doposting p@Posting{paccount=a} = p{paccount= accountNameApplyAliases aliases a} - -- | Do post-parse processing on a parsed journal to make it ready for -- use. Reverse parsed data to normal order, canonicalise amount -- formats, check/ensure that transactions are balanced, and maybe diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index c4277b156..462f8eab3 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -25,7 +25,6 @@ module Hledger.Read ( readJournal', -- * Re-exported - JournalReader.accountaliasp, JournalReader.postingp, module Hledger.Read.Common, diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 861a3a0ef..58641a836 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -63,8 +63,7 @@ module Hledger.Read.JournalReader ( -- numberp, statusp, emptyorcommentlinep, - followingcommentp, - accountaliasp + followingcommentp -- * Tests ,tests_Hledger_Read_JournalReader @@ -120,7 +119,17 @@ reader = Reader -- | Parse and post-process a "Journal" from hledger's journal file -- format, or give an error. parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal -parse = parseAndFinaliseJournal journalp +parse iopts = parseAndFinaliseJournal journalp' iopts + where + journalp' = do + -- reverse parsed aliases to ensure that they are applied in order given on commandline + mapM_ addAccountAlias (reverse $ aliasesFromOpts iopts) + journalp + +-- | Get the account name aliases from options, if any. +aliasesFromOpts :: InputOpts -> [AccountAlias] +aliasesFromOpts = map (\a -> fromparse $ runParser accountaliasp ("--alias "++quoteIfNeeded a) $ T.pack a) + . aliases_ --- * parsers --- ** journal diff --git a/hledger-ui/Hledger/UI/Main.hs b/hledger-ui/Hledger/UI/Main.hs index 8f019d28f..b4b6fa5f3 100644 --- a/hledger-ui/Hledger/UI/Main.hs +++ b/hledger-ui/Hledger/UI/Main.hs @@ -78,7 +78,6 @@ withJournalDoUICommand uopts@UIOpts{cliopts_=copts} cmd = do let fn = cmd uopts . pivotByOpts copts . anonymiseByOpts copts - . journalApplyAliases (aliasesFromOpts copts) <=< journalApplyValue (reportopts_ copts) <=< journalAddForecast copts either error' fn ej diff --git a/hledger-web/Hledger/Web/Main.hs b/hledger-web/Hledger/Web/Main.hs index fe91c176f..fe9d36d0d 100644 --- a/hledger-web/Hledger/Web/Main.hs +++ b/hledger-web/Hledger/Web/Main.hs @@ -63,7 +63,6 @@ withJournalDo' opts@WebOpts {cliopts_ = cliopts} cmd = do let fn = cmd opts . pivotByOpts cliopts . anonymiseByOpts cliopts - . journalApplyAliases (aliasesFromOpts cliopts) <=< journalApplyValue (reportopts_ cliopts) <=< journalAddForecast cliopts readJournalFile def f >>= either error' fn diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 2a4ec382d..56e3b2172 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -44,7 +44,6 @@ module Hledger.Cli.CliOptions ( -- possibly these should move into argsToCliOpts -- * CLI option accessors -- | These do the extra processing required for some options. - aliasesFromOpts, journalFilePathFromOpts, rulesFilePathFromOpts, outputFileFromOpts, @@ -484,11 +483,6 @@ getHledgerCliOpts mode' = do -- CliOpts accessors --- | Get the account name aliases from options, if any. -aliasesFromOpts :: CliOpts -> [AccountAlias] -aliasesFromOpts = map (\a -> fromparse $ runParser accountaliasp ("--alias "++quoteIfNeeded a) $ T.pack a) - . aliases_ . inputopts_ - -- | Get the (tilde-expanded, absolute) journal file path from -- 1. options, 2. an environment variable, or 3. the default. -- Actually, returns one or more file paths. There will be more diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index 55721c8ca..31e78aca1 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -70,7 +70,6 @@ withJournalDo opts cmd = do let f = cmd opts . pivotByOpts opts . anonymiseByOpts opts - . journalApplyAliases (aliasesFromOpts opts) <=< journalApplyValue (reportopts_ opts) <=< journalAddForecast opts either error' f ej @@ -161,7 +160,7 @@ writeOutput opts s = do journalReload :: CliOpts -> IO (Either String Journal) journalReload opts = do journalpaths <- journalFilePathFromOpts opts - ((pivotByOpts opts . journalApplyAliases (aliasesFromOpts opts)) <$>) <$> + (pivotByOpts opts <$>) <$> readJournalFiles (inputopts_ opts) journalpaths -- | Re-read the option-specified journal file(s), but only if any of diff --git a/tests/misc/account-aliases.test b/tests/misc/account-aliases.test index 7faea5e4c..2814f06c6 100644 --- a/tests/misc/account-aliases.test +++ b/tests/misc/account-aliases.test @@ -122,6 +122,36 @@ alias E=F >>>2 >>>=0 +# alias options +hledger -f- balance --alias=cc=credit-card --alias=b=bank +<<< +01/01 Opening balances + (bank) 100 + (credit-card) -10 + +01/02 + expenses 5 + cc + +01/03 + expenses 5 + b + +01/04 Credit card charge + cc = 0 + b + +01/05 + expenses 5 + b = 75 +>>> + 75 bank + 15 expenses +-------------------- + 90 +>>>2 +>>>=0 + # query will search both origin and substitution in alias hledger -f- reg '^a$' '^b$' <<<