Skip to content

Commit

Permalink
Change inheriting valued tags to override
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Chris Lemaire committed Dec 5, 2022
1 parent 934f0e3 commit 1bae3f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
9 changes: 7 additions & 2 deletions hledger-lib/Hledger/Data/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion hledger-lib/Hledger/Data/Posting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 20 additions & 0 deletions hledger/test/query-tag.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1bae3f9

Please sign in to comment.