Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Change inheriting valued tags to override #1955

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions hledger-lib/Hledger/Data/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import Control.Monad.State.Strict (StateT)
import Data.Char (toUpper, isDigit)
import Data.Default (Default(..))
import Data.Foldable (toList)
import Data.List ((\\), find, foldl', sortBy, union, intercalate)
import Data.List ((\\), find, foldl', sortBy, intercalate)
import Data.List.Extra (nubSort)
import qualified Data.Map.Strict as M
import Data.Maybe (catMaybes, fromMaybe, mapMaybe, maybeToList)
Expand Down 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 @@ -372,7 +372,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
47 changes: 47 additions & 0 deletions hledger/test/query-tag.test
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,50 @@ $ 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be more tests for the combination of transaction/posting and account tags:

Suggested change
a
a
# 24. Transactions can override the tags of used accounts
<
account a ; atag:A
account a:b ; atag:B
2023-01-01 Test
; atag:C
(a:b) 10 €
$ hledger -f- bal --pivot=atag
10 € C
--------------------
10 €
# 25. Postings can override the tags of their accounts
<
account a ; atag:A
account a:b ; atag:B
2023-01-01 Test
; atag:C
(a:b) 10 € ; atag:D
$ hledger -f- bal --pivot=atag
10 € D
--------------------
10 €

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added your tests as suggested and I'm thinking of more tests that should be relevant to this ordering with multi-valued tags. Thank you very much for taking the time to look into some tests!


# 24. Transactions can override the tags of used accounts
<
account a ; atag:A
account a:b ; atag:B
2023-01-01 Test
; atag:C
(a:b) 10 €

$ hledger -f- bal --pivot=atag
10 € C
--------------------
10 €

# 25. Postings can override the tags of their accounts
<
account a ; atag:A
account a:b ; atag:B

2023-01-01 Test
; atag:C
(a:b) 10 € ; atag:D

$ hledger -f- bal --pivot=atag
10 € D
--------------------
10 €