From a16c88b1b15577bf03244e0954d2ec12c5b7059b Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Thu, 3 Feb 2022 15:38:45 +1100 Subject: [PATCH] fix: Make sure cash and conversion account types are correctly determined when there are no explicit declarations. --- hledger-lib/Hledger/Read/Common.hs | 28 +++++++++++++++------------- hledger/test/query-type.test | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 392a208ef..140e9f0b4 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -349,20 +349,22 @@ journalAddAccountTypes j = j{jaccounttypes = journalAccountTypes j} -- | Build a map of all known account types, explicitly declared -- or inferred from the account's parent or name. journalAccountTypes :: Journal -> M.Map AccountName AccountType -journalAccountTypes j = - let - t = accountNameTreeFrom $ journalAccountNames j :: Tree AccountName - t' = settypes Nothing t :: Tree (AccountName, Maybe AccountType) - in - M.fromList [(a,t) | (a, Just t) <- flatten t'] +journalAccountTypes j = M.fromList [(a,acctType) | (a, Just (acctType,_)) <- flatten t'] where - -- Map from the top of the account tree down to the leaves, - -- propagating account types downward. - settypes :: Maybe AccountType -> Tree AccountName -> Tree (AccountName, Maybe AccountType) - settypes mparenttype (Node a subs) = - let mtype = M.lookup a declaredtypes <|> mparenttype <|> accountNameInferType a - in Node (a, mtype) (map (settypes mtype) subs) - declaredtypes = journalDeclaredAccountTypes j + t = accountNameTreeFrom $ journalAccountNames j :: Tree AccountName + t' = settypes Nothing t :: Tree (AccountName, Maybe (AccountType, Bool)) + -- Map from the top of the account tree down to the leaves, propagating + -- account types downward. Keep track of whether the account is declared + -- (True), in which case the parent account should be preferred, or merely + -- inferred (False), in which case the inferred type should be preferred. + settypes :: Maybe (AccountType, Bool) -> Tree AccountName -> Tree (AccountName, Maybe (AccountType, Bool)) + settypes mparenttype (Node a subs) = Node (a, mtype) (map (settypes mtype) subs) + where + mtype = M.lookup a declaredtypes <|> minferred + minferred = if maybe False snd mparenttype + then mparenttype + else (,False) <$> accountNameInferType a <|> mparenttype + declaredtypes = (,True) <$> journalDeclaredAccountTypes j -- | Build a map of the account types explicitly declared. journalDeclaredAccountTypes :: Journal -> M.Map AccountName AccountType diff --git a/hledger/test/query-type.test b/hledger/test/query-type.test index 0a808b07b..7efaa1b3f 100644 --- a/hledger/test/query-type.test +++ b/hledger/test/query-type.test @@ -75,3 +75,23 @@ account liabilities $ hledger -f- accounts type:a assets + +# 11. type: can identify cash accounts in the default case +< +account assets +account assets:cash + +$ hledger -f- accounts type:c +assets:cash + +# 12. type: can identify conversion accounts in the default case +< +account equity +account equity:conversion +account equity:trading +account equity:trade + +$ hledger -f- accounts type:v +equity:conversion +equity:trading +equity:trade