From 1bae3f9c6afaca4e342b6bfe4d805d97a0abe09b Mon Sep 17 00:00:00 2001 From: Chris Lemaire Date: Sat, 3 Dec 2022 19:52:29 +0100 Subject: [PATCH] Change inheriting valued tags to override Old behaviour of inheriting tags with values was that tags added to, for instance, a posting, would be added to the tags, possibly overriding the tags of the transaction. Or, in other words, the transaction tags were added to the posting tags, as there was no sense of overriding a tag. The new behaviour is that tags are now overridden when a lower level re-uses that tag name. For instance, when defining a transaction with tag t:v and posting with tag t:v2, only t:v2 remains on the posting, overriding the transaction tag. Perhaps it would be desirable to add an option for additive tags over overriding tags, as it may well be useful at times to add to tags in the parent. --- hledger-lib/Hledger/Data/Journal.hs | 9 +++++++-- hledger-lib/Hledger/Data/Posting.hs | 7 ++++++- hledger/test/query-tag.test | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 63703d256b5e..8a2dbc94814b 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -404,9 +404,14 @@ journalAccountTags Journal{jdeclaredaccounttags} a = M.findWithDefault [] a jdec -- | Which tags are in effect for this account, including tags inherited from parent accounts ? journalInheritedAccountTags :: Journal -> AccountName -> [Tag] journalInheritedAccountTags j a = - foldl' (\ts a' -> ts `union` journalAccountTags j a') [] as + fst $ foldl' + (\(ts, nms) (ts', nms') -> + (ts <> filter (\(nm, _) -> nm `S.notMember` nms) ts', nms `S.union` nms')) + (ats, S.fromList $ fst <$> ats) + asts where - as = a : parentAccountNames a + ats = journalAccountTags j a + asts = (\ts -> (ts, S.fromList $ fst <$> ts)) <$> journalAccountTags j <$> parentAccountNames a -- PERF: cache in journal ? -- | Find up to N most similar and most recent transactions matching diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index c212fd71475b..4c96cb6c2621 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -365,7 +365,12 @@ postingStatus Posting{pstatus=s, ptransaction=mt} = case s of -- | Tags for this posting including any inherited from its parent transaction. postingAllTags :: Posting -> [Tag] -postingAllTags p = ptags p ++ maybe [] ttags (ptransaction p) +postingAllTags p = ptags p + ++ filter + (\(nm, _) -> nm `S.notMember` pTagNames) + (maybe [] ttags (ptransaction p)) + where + pTagNames = S.fromList (fst <$> ptags p) -- | Tags for this transaction including any from its postings. transactionAllTags :: Transaction -> [Tag] diff --git a/hledger/test/query-tag.test b/hledger/test/query-tag.test index 3c887d3cc809..ec16b2429a2c 100644 --- a/hledger/test/query-tag.test +++ b/hledger/test/query-tag.test @@ -214,3 +214,23 @@ $ hledger -f- bal -N tag:type=a # 21. $ hledger -f- reg -w80 tag:type=a 2022-01-01 (a:aa) 1 1 + +# 22. Postings can override the tags of their parents +< +2022-11-17 Aldi + ; concerns: me + Assets -30 € + Costs:Food 20 € + Loaned 10 € ; concerns: you + +$ hledger -f- reg tag:concerns=me --pivot=concerns +2022-11-17 Aldi me -30 € -30 € + me 20 € -10 € + +# 23. Accounts can override the tags of their parents +< +account a ; atag:A +account a:b ; atag:B + +$ hledger -f- accounts tag:atag=a +a