From aa3807e157e8e3b1a7c08c979f7b1173e279725b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 11 Apr 2022 09:18:23 -1000 Subject: [PATCH] ref: journalAccountTypes --- hledger-lib/Hledger/Data/Journal.hs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 6901534a6..222dbea97 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -428,6 +428,8 @@ letterPairs :: String -> [String] letterPairs (a:b:rest) = [a,b] : letterPairs (b:rest) letterPairs _ = [] +-- Older account type code + -- queries for standard account types -- | Get a query for accounts of the specified types in this journal. @@ -538,7 +540,7 @@ journalConversionAccount = . M.findWithDefault [] Conversion . jdeclaredaccounttypes --- Newer account type functionality. +-- Newer account type code. journalAccountType :: Journal -> AccountName -> Maybe AccountType journalAccountType Journal{jaccounttypes} = accountNameType jaccounttypes @@ -553,19 +555,21 @@ journalAccountTypes :: Journal -> M.Map AccountName AccountType journalAccountTypes j = M.fromList [(a,acctType) | (a, Just (acctType,_)) <- flatten t'] where 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) + t' = settypes Nothing t :: Tree (AccountName, Maybe (AccountType, Bool)) where - mtype = M.lookup a declaredtypes <|> minferred - minferred = if maybe False snd mparenttype - then mparenttype - else (,False) <$> accountNameInferType a <|> mparenttype - declaredtypes = (,True) <$> journalDeclaredAccountTypes j + 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 + where + declaredtypes = (,True) <$> journalDeclaredAccountTypes j + minferred = if maybe False snd mparenttype + then mparenttype + else (,False) <$> accountNameInferType a <|> mparenttype -- | Build a map of the account types explicitly declared. journalDeclaredAccountTypes :: Journal -> M.Map AccountName AccountType